|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | require 'icalendar' |
2 | 4 | require 'nokogiri' |
3 | 5 | require 'open-uri' |
4 | 6 | require 'tzinfo' |
5 | 7 |
|
6 | 8 | module Ingestors |
| 9 | + # Reads from direct ical / .ics / Indico (event or category) URLs, .xml sitemaps, and .txt sitemaps. |
7 | 10 | class IcalIngestor < Ingestor |
| 11 | + include Ingestors::Concerns::SitemapHelpers |
| 12 | + include Ingestors::Concerns::IcalIngestorExportUrl |
| 13 | + |
8 | 14 | def self.config |
9 | 15 | { |
10 | 16 | key: 'ical', |
11 | | - title: 'iCalendar', |
| 17 | + title: 'iCalendar / Indico / .ics File', |
12 | 18 | category: :events |
13 | 19 | } |
14 | 20 | end |
15 | 21 |
|
16 | | - def read(url) |
17 | | - unless url.nil? |
18 | | - if url.to_s.downcase.end_with? 'sitemap.xml' |
19 | | - process_sitemap url |
20 | | - else |
21 | | - process_icalendar url |
22 | | - end |
| 22 | + def read(source_url) |
| 23 | + @verbose = false |
| 24 | + sources = get_sources(source_url) |
| 25 | + return if sources.nil? |
| 26 | + |
| 27 | + sources.each do |url| |
| 28 | + process_url(url) |
23 | 29 | end |
24 | 30 | end |
25 | 31 |
|
26 | 32 | private |
27 | 33 |
|
28 | | - def process_sitemap(url) |
29 | | - # find urls for individual icalendar files |
30 | | - begin |
31 | | - sitemap = Nokogiri::XML.parse(open_url(url, raise: true)) |
32 | | - locs = sitemap.xpath('/ns:urlset/ns:url/ns:loc', { |
33 | | - 'ns' => 'http://www.sitemaps.org/schemas/sitemap/0.9' |
34 | | - }) |
35 | | - locs.each do |loc| |
36 | | - process_icalendar(loc.text) |
37 | | - end |
38 | | - rescue Exception => e |
39 | | - @messages << "Extract from sitemap[#{url}] failed with: #{e.message}" |
| 34 | + # Modifies the given URL to the ics or ical export. |
| 35 | + # Loops into each Ical event to process it. |
| 36 | + # Note: One .ics file can have multiple Ical events. |
| 37 | + def process_url(url) |
| 38 | + export_url = to_export(url) |
| 39 | + events = Icalendar::Event.parse(open_url(export_url, raise: true).set_encoding('utf-8')) |
| 40 | + events.each do |e| |
| 41 | + process_calevent(e) |
40 | 42 | end |
| 43 | + rescue StandardError => e |
| 44 | + @messages << "Process file url[#{export_url}] failed with: #{e.message}" |
| 45 | + end |
41 | 46 |
|
42 | | - # finished |
43 | | - nil |
| 47 | + # Builds the OpenStruct event and adds it in event. |
| 48 | + def process_calevent(calevent) |
| 49 | + event_to_add = OpenStruct.new.tap do |event| |
| 50 | + assign_basic_info(event, calevent) |
| 51 | + assign_time_info(event, calevent) |
| 52 | + assign_location_info(event, calevent.location) |
| 53 | + end |
| 54 | + add_event(event_to_add) |
| 55 | + rescue StandardError => e |
| 56 | + @messages << "Process iCalendar failed with: #{e.message}" |
44 | 57 | end |
45 | 58 |
|
46 | | - def process_icalendar(url) |
47 | | - # process individual ics file |
48 | | - query = '?ical=true' |
| 59 | + # Assigns to event: url, title, description, keywords. |
| 60 | + def assign_basic_info(event, calevent) |
| 61 | + event.url = calevent.url.to_s |
| 62 | + event.title = calevent.summary.to_s |
| 63 | + event.description = process_description calevent.description |
| 64 | + event.keywords = process_keywords(calevent.categories) |
| 65 | + end |
49 | 66 |
|
50 | | - begin |
51 | | - # append query (if required) |
52 | | - file_url = url |
53 | | - file_url << query unless url.to_s.downcase.ends_with? query |
| 67 | + # Assigns to event: start, end, timezone. |
| 68 | + def assign_time_info(event, calevent) |
| 69 | + event.start = calevent.dtstart&.to_time unless calevent.dtstart.nil? |
| 70 | + event.end = calevent.dtend&.to_time unless calevent.dtend.nil? |
| 71 | + event.timezone = get_tzid(calevent.dtstart) |
| 72 | + end |
54 | 73 |
|
55 | | - # process file |
56 | | - events = Icalendar::Event.parse(open_url(file_url, raise: true).set_encoding('utf-8')) |
| 74 | + # Assigns to event: venue, online, city. |
| 75 | + def assign_location_info(event, location) |
| 76 | + return if location.blank? || !location.present? |
57 | 77 |
|
58 | | - # process each event |
59 | | - events.each do |e| |
60 | | - process_event(e) |
61 | | - end |
62 | | - rescue Exception => e |
63 | | - @messages << "Process file url[#{file_url}] failed with: #{e.message}" |
64 | | - end |
| 78 | + event.venue = location.to_s |
| 79 | + event.online = location.downcase.include?('online') |
| 80 | + event.city, event.postcode, event.country = process_location(location) |
| 81 | + end |
65 | 82 |
|
66 | | - # finished |
67 | | - nil |
| 83 | + # Removes all `<br />` tags and converts HTML to MD. |
| 84 | + def process_description(input) |
| 85 | + return input if input.nil? |
| 86 | + |
| 87 | + desc = input.to_s.gsub('', '<br />') |
| 88 | + convert_description(desc) |
68 | 89 | end |
69 | 90 |
|
70 | | - def process_event(calevent) |
71 | | - # puts "calevent: #{calevent.inspect}" |
72 | | - begin |
73 | | - # set fields |
74 | | - event = OpenStruct.new |
75 | | - event.url = calevent.url.to_s |
76 | | - event.title = calevent.summary.to_s |
77 | | - event.description = process_description calevent.description |
78 | | - |
79 | | - # puts "\n\ncalevent.description = #{calevent.description}" |
80 | | - # puts "\n\n... converted = #{event.description}" |
81 | | - |
82 | | - event.end = calevent.dtend&.to_time |
83 | | - unless calevent.dtstart.nil? |
84 | | - dtstart = calevent.dtstart |
85 | | - event.start = dtstart&.to_time |
86 | | - tzid = dtstart.ical_params['tzid'] |
87 | | - event.timezone = tzid.first.to_s if !tzid.nil? and tzid.size > 0 |
88 | | - end |
89 | | - |
90 | | - event.venue = calevent.location.to_s |
91 | | - if calevent.location.downcase.include?('online') |
92 | | - event.online = true |
93 | | - event.city = nil |
94 | | - event.postcode = nil |
95 | | - event.country = nil |
96 | | - else |
97 | | - location = convert_location(calevent.location) |
98 | | - event.city = location['suburb'] unless location['suburb'].nil? |
99 | | - event.country = location['country'] unless location['country'].nil? |
100 | | - event.postcode = location['postcode'] unless location['postcode'].nil? |
101 | | - end |
102 | | - event.keywords = [] |
103 | | - unless calevent.categories.nil? or calevent.categories.first.nil? |
104 | | - cats = calevent.categories.first |
105 | | - if cats.is_a?(Icalendar::Values::Array) |
106 | | - cats.each do |item| |
107 | | - event.keywords << item.to_s.lstrip |
108 | | - end |
109 | | - else |
110 | | - event.keywords << cats.to_s.strip |
111 | | - end |
112 | | - end |
113 | | - |
114 | | - # store event |
115 | | - @events << event |
116 | | - rescue Exception => e |
117 | | - @messages << "Process iCalendar failed with: #{e.message}" |
118 | | - end |
| 91 | + # Extracts the timezone identifier (TZID) from an iCalendar event's dtstart field. |
| 92 | + # Handles whether tzid shows up as an Array or a single string |
| 93 | + def get_tzid(dtstart) |
| 94 | + return nil unless dtstart.respond_to?(:ical_params) |
119 | 95 |
|
120 | | - # finished |
121 | | - nil |
| 96 | + tzid = dtstart.ical_params['tzid'] |
| 97 | + return nil if tzid.nil? |
| 98 | + |
| 99 | + tzid.is_a?(Array) ? tzid.first.to_s : tzid.to_s |
122 | 100 | end |
123 | 101 |
|
124 | | - def process_description(input) |
125 | | - return input if input.nil? |
| 102 | + # Returns an array of 3 location characteristics: suburb, postcode, country |
| 103 | + # Everything is nil if location.blank or location is online |
| 104 | + def process_location(location) |
| 105 | + return [nil, nil, nil] if location.blank? |
| 106 | + |
| 107 | + if location.to_s.downcase.include?('online') |
| 108 | + [nil, nil, nil] |
| 109 | + else |
| 110 | + [ |
| 111 | + location['suburb'], |
| 112 | + location['postcode'], |
| 113 | + location['country'] |
| 114 | + ] |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + # Returns keywords from the `CATEGORIES` ICal field |
| 119 | + def process_keywords(categories) |
| 120 | + return [] if categories.blank? |
126 | 121 |
|
127 | | - convert_description(input.to_s.gsub(/\R/, '<br />')) |
| 122 | + categories.flatten.compact.map { |cat| cat.to_s.strip } |
128 | 123 | end |
129 | 124 | end |
130 | 125 | end |
0 commit comments