|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'icalendar' |
| 4 | +require 'nokogiri' |
| 5 | +require 'open-uri' |
| 6 | +require 'tzinfo' |
| 7 | + |
| 8 | +module Ingestors |
| 9 | + # Reads from direct .ics or Indico (event or category) URLs, .xml sitemaps, and .txt sitemaps. |
| 10 | + class IndicoIngestor < Ingestor |
| 11 | + include Ingestors::Concerns::SitemapHelpers |
| 12 | + |
| 13 | + def self.config |
| 14 | + { |
| 15 | + key: 'indico', |
| 16 | + title: 'Indico / .ics file', |
| 17 | + category: :events |
| 18 | + } |
| 19 | + end |
| 20 | + |
| 21 | + def read(source_url) |
| 22 | + @token = Rails.application.config.secrets.indico_api_token |
| 23 | + @verbose = false |
| 24 | + sources = parse_sitemap(source_url) |
| 25 | + return if sources.nil? |
| 26 | + |
| 27 | + sources.each do |url| |
| 28 | + process_url(url) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + private |
| 33 | + |
| 34 | + # Modifies the given URL to the ics export. |
| 35 | + # Loops into each event to process it. |
| 36 | + # Note: One .ics file can have multiple events. |
| 37 | + def process_url(url) |
| 38 | + export_url = to_export(url) |
| 39 | + raise 'Not an indico link' if export_url.nil? |
| 40 | + |
| 41 | + content = open_url(export_url, raise: true, token: @token).set_encoding('utf-8') |
| 42 | + events = Icalendar::Event.parse(content) |
| 43 | + raise 'Not found' if events.nil? || events.empty? |
| 44 | + |
| 45 | + events.each do |e| |
| 46 | + process_calevent(e) |
| 47 | + end |
| 48 | + rescue StandardError => e |
| 49 | + @messages << "Process file url[#{export_url}] failed with: #{e.message}" |
| 50 | + end |
| 51 | + |
| 52 | + # 1. If the path already ends with '/events.ics', return as-is. |
| 53 | + # 2. If the host includes 'indico', ensures the path ends with '/events.ics'. |
| 54 | + # This method never mutates the original URL string. |
| 55 | + # Returns the updated URL string or nil if input is blank. |
| 56 | + def to_export(url) |
| 57 | + return nil if url.blank? |
| 58 | + |
| 59 | + uri = URI.parse(url) |
| 60 | + path = uri.path.to_s |
| 61 | + |
| 62 | + if path.match?(%r{/(event|events)\.ics\z}) |
| 63 | + uri.to_s |
| 64 | + elsif indico_page?(uri) |
| 65 | + ensure_events_ics_path(uri) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + def indico_page?(uri) |
| 70 | + # Either checks in host, e.g., 'indico.myinstitution.com' |
| 71 | + return true if uri.host&.include?('indico') |
| 72 | + |
| 73 | + # Or checks in meta tags |
| 74 | + html = open_url(uri, raise: true) |
| 75 | + doc = Nokogiri::HTML(html) |
| 76 | + content = doc.at('meta[property="og:site_name"]')&.[]('content') |
| 77 | + content&.match?(/indico/i) |
| 78 | + end |
| 79 | + |
| 80 | + # Ensures the Indico URL ends with '/events.ics' |
| 81 | + def ensure_events_ics_path(uri) |
| 82 | + paths = uri.path.split('/') |
| 83 | + uri.path = "#{paths[0..2].join('/')}/" |
| 84 | + if paths[1] == 'event' |
| 85 | + uri.path = File.join(uri.path, 'event.ics') |
| 86 | + elsif paths[1] == 'category' |
| 87 | + uri.path = File.join(uri.path, 'events.ics') |
| 88 | + end |
| 89 | + uri.to_s |
| 90 | + end |
| 91 | + |
| 92 | + # Builds the OpenStruct event and adds it in event. |
| 93 | + def process_calevent(calevent) |
| 94 | + event_to_add = OpenStruct.new.tap do |event| |
| 95 | + assign_basic_info(event, calevent) |
| 96 | + assign_time_info(event, calevent) |
| 97 | + assign_location_info(event, calevent) |
| 98 | + end |
| 99 | + add_event(event_to_add) |
| 100 | + rescue StandardError => e |
| 101 | + Rails.logger.error("#{e.class}: #{e.message}") |
| 102 | + end |
| 103 | + |
| 104 | + # Assigns to event: url, title, description, keywords. |
| 105 | + def assign_basic_info(event, calevent) |
| 106 | + event.url = calevent.url.to_s |
| 107 | + event.title = calevent.summary.to_s |
| 108 | + event.description = calevent.description.to_s |
| 109 | + event.keywords = calevent.categories.flatten |
| 110 | + event.contact = calevent.contact.join(', ') |
| 111 | + end |
| 112 | + |
| 113 | + # Assigns to event: start, end, timezone. |
| 114 | + def assign_time_info(event, calevent) |
| 115 | + event.start = calevent.dtstart&.to_time unless calevent.dtstart.nil? |
| 116 | + event.end = calevent.dtend&.to_time unless calevent.dtend.nil? |
| 117 | + event.timezone = get_tzid(calevent.dtstart) |
| 118 | + end |
| 119 | + |
| 120 | + # Assigns to event: venue, online, city. |
| 121 | + def assign_location_info(event, calevent) |
| 122 | + location = calevent.location |
| 123 | + return if location.blank? |
| 124 | + |
| 125 | + event.venue = location.to_s |
| 126 | + event.online = calevent.description.include?('zoom') |
| 127 | + event.presence = calevent.description.include?('zoom') ? :hybrid : :onsite # can do best, but sufficient for now |
| 128 | + end |
| 129 | + |
| 130 | + # Extracts the timezone identifier (TZID) from an iCalendar event's dtstart field. |
| 131 | + # Handles whether tzid shows up as an Array or a single string |
| 132 | + def get_tzid(dtstart) |
| 133 | + return nil unless dtstart.respond_to?(:ical_params) |
| 134 | + |
| 135 | + tzid = dtstart.ical_params['tzid'] |
| 136 | + return nil if tzid.nil? |
| 137 | + |
| 138 | + tzid.is_a?(Array) ? tzid.first.to_s : tzid.to_s |
| 139 | + end |
| 140 | + end |
| 141 | +end |
0 commit comments