Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 215 lines (177 sloc) 7.765 kB
e41c1af @aslakknutsen Add Awstruct::Extension::Lanyrd::Export
aslakknutsen authored
1 require 'vpim/icalendar'
2
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
3 ##
4 # Lanyrd is an Awestruct extension module for interacting with lanyrd.com
5 # to retrieve conference session listings, speakers and related info.
8318595 @aslakknutsen Add Lanyrd extension with session Search support
aslakknutsen authored
6
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
7 module Awestruct::Extensions::Lanyrd
8
9 ##
10 # Awestruct::Extensions::Lanyrd::Search retrieves sessions from
11 # lanyrd.com that match the given search term.
12 #
13 # This class is loaded as an extension in the Awestruct pipeline. The
14 # constructor accepts a search term that is used to search for sessions.
15 #
16 # extension Awestruct::Extensions::Lanyrd::Search.new('arquillian')
17 #
18 # This extension performs the following work:
19 #
20 # * search for the specified term using an HTTP request
21 # * parse the HTML result for pagination and fetch the paged results
22 # * parse all pages for sessions and fetch the session details
23 # * drop any sessions prior to the current date or with a missing date
24 # * store an array of OpenStruct objects representing sessions in site.sessions
25 #
26 # Each OpenStruct session object contains the following properties:
27 #
28 # * title (title of session, type: String)
29 # * description (abstract from session page, type: String (HTML))
30 # * detail_url (session detail page at lanyrd.com, type: URL (absolute))
31 # * start_datetime (start time of session, type: DateTime)
32 # * end_datetime (end time of session, type: DateTime)
33 # * timezone (canonical timezone of session, type: String)
34 # * event (conference name, type: String)
35 # * event_url (conference URL, type: URL (absolute))
36 # * speaker_names (full name of all speakers, type: Array[String])
3824dea @mojavelinux major overhaul of user identities
mojavelinux authored
37 # * speakers (username and name of all speakers, type: Hash[username, name])
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
38 #
39 # Author:: Aslak Knutsen, Dan Allen
185691b @mojavelinux new guides, fix to guide toc, ref doc links, notes
mojavelinux authored
40 # TODO:: retrieve the detail url and image url for the speaker, don't download detail pages of old sessions
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
41
42 class Search
8318595 @aslakknutsen Add Lanyrd extension with session Search support
aslakknutsen authored
43
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
44 def initialize(term)
45 @base = 'http://lanyrd.com'
46 @term = term
47 @since = DateTime.now
48 end
49
50 def execute(site)
51 @lanyrd_tmp = tmp(site.tmp_dir, 'lanyrd')
8318595 @aslakknutsen Add Lanyrd extension with session Search support
aslakknutsen authored
52
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
53 # add &topic=#{@term} to limit sessions to those that have been tagged with the term
a83a2e7 @mojavelinux use cached date as date of session update in ics file
mojavelinux authored
54 # context=future only works for conferences atm...waiting for session search support
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
55 search_url = "#{@base}/search/?type=session&q=#{@term}"
56
57 sessions = []
58
59 pages = []
60
61 page1 = Hpricot(getOrCache(File.join(@lanyrd_tmp, "search-#{@term}-1.html"), search_url))
62 pages << page1
63
64 extract_pages(page1, pages)
65
66 pages.each do |page|
67 extract_sessions(page, sessions)
68 end
69
70 site.sessions = sessions.sort_by{|session| session.start_datetime}
71 end
72
73 # Find all Pages in a 'root' Page
74 def extract_pages(root, pages)
75 root.search('div[@class*=pagination]') do |p|
76 p.search('li') do |entry|
77 a = entry.at('a')
78 if a
79 pageinated_url = "#{@base}#{a.attributes['href']}"
8318595 @aslakknutsen Add Lanyrd extension with session Search support
aslakknutsen authored
80
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
81 pageX = Hpricot(getOrCache(File.join(@lanyrd_tmp, "search-#{@term}-#{a.inner_text}.html"), pageinated_url))
82 pages << pageX
8318595 @aslakknutsen Add Lanyrd extension with session Search support
aslakknutsen authored
83 end
84 end
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
85 end
86 end
87
88 # Find all sessions in Page
89 def extract_sessions(page, sessions)
90 page.search('li[@class*=s-session]').each do |raw|
8318595 @aslakknutsen Add Lanyrd extension with session Search support
aslakknutsen authored
91
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
92 event_link = raw.search('h3').at('a')
93
94 session = OpenStruct.new
95 session.title = event_link.inner_text
96
97 session_detail_url = "#{@base}#{event_link.attributes['href']}"
a83a2e7 @mojavelinux use cached date as date of session update in ics file
mojavelinux authored
98 session.slug = event_link.attributes['href'].split('/').last
99 session_detail = Hpricot(getOrCache(File.join(@lanyrd_tmp, "session-#{session.slug}.html"), session_detail_url))
100 session.updated = File.mtime(File.join(@lanyrd_tmp, "session-#{session.slug}.html"))
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
101
102 session_meta_node = session_detail.at('div[@class=session-meta]')
103 timezone_node = session_detail.at('abbr[@class=timezone]')
104 if timezone_node
105 session.timezone = timezone_node.inner_text
106 end
107 dtstart_node = session_meta_node.at('abbr[@class=dtstart]')
108 if dtstart_node
109 session.start_datetime = DateTime.parse [dtstart_node.attributes['title'], session.timezone].join(' ')
110 end
111
112 # skip old events and events with no date
113 if session.start_datetime and session.start_datetime >= @since
114 dtend_node = session_meta_node.at('abbr[@class=dtend]')
115 if dtend_node
116 session.end_datetime = DateTime.parse [dtend_node.attributes['title'], session.timezone].join(' ')
117 end
118
119 session.description = session_detail.search('div[@class*=abstract]').inner_html
120 session.detail_url = session_detail_url
185691b @mojavelinux new guides, fix to guide toc, ref doc links, notes
mojavelinux authored
121
122 session.event_location = session_detail.at('p[@class=location]').inner_text.split(/\//).map{|l| l.strip}.reverse.join(', ')
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
123
124 raw.search('p[@class*=meta]').each do |meta|
125 type = meta.search('strong').inner_html
126 meta.search('strong').remove()
127 session.event = meta.inner_text.strip if type.eql? 'Event'
128 session.event_url = "#{@base}#{meta.at('a').attributes['href']}" if type.eql? 'Event'
129 session.speaker_names = []
3824dea @mojavelinux major overhaul of user identities
mojavelinux authored
130 session.speakers = []
8318595 @aslakknutsen Add Lanyrd extension with session Search support
aslakknutsen authored
131
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
132 if type.eql? 'Speakers'
133 session.speaker_names = meta.inner_text.strip.split(', ')
3824dea @mojavelinux major overhaul of user identities
mojavelinux authored
134 meta.search('a').each do |speaker_node|
135 name = speaker_node.inner_text.strip
136 username = speaker_node.attributes['href'].match(/\/profile\/([^\/]*)/)[1]
453e259 @mojavelinux add lanyrd.url to identity (best place for now)
mojavelinux authored
137 # QUESTION add identities for speakers if not exist?
3824dea @mojavelinux major overhaul of user identities
mojavelinux authored
138 session.speakers << {'name' => name, 'username' => username }
139 end
8318595 @aslakknutsen Add Lanyrd extension with session Search support
aslakknutsen authored
140 end
141
142 end
07d05f0 @mojavelinux enhance lanyrd ext, styling, add events to homepage
mojavelinux authored
143
144 sessions << session
8318595 @aslakknutsen Add Lanyrd extension with session Search support
aslakknutsen authored
145 end
146 end
147 end
e41c1af @aslakknutsen Add Awstruct::Extension::Lanyrd::Export
aslakknutsen authored
148 end
149
150 ##
151 # Awestruct::Extensions::Lanyrd::Export exports sessions retrived by the Search extension
152 # as a ical stream.
153 #
154 # This class is loaded as an extension in the Awestruct pipeline. The
155 # constructor accepts a output_path to where the ical stream should be exported.
156 #
157 # extension Awestruct::Extensions::Lanyrd::Export.new('/invation/events/ical.ics')
158 #
159 # This extension performs the following work:
160 #
161 # * read all site.sessions
162 # * write out a ical using the vpim library
163 #
164 # Author:: Aslak Knutsen
165 class Export
166
167 def initialize(output_path)
168 @output_path = output_path
169 end
170
171 def execute(site)
172 if site.sessions
173
174 cal = Vpim::Icalendar.create2
175 site.sessions.each do |session|
176
177 cal.add_event do |e|
a83a2e7 @mojavelinux use cached date as date of session update in ics file
mojavelinux authored
178 e.dtstart Time.parse session.start_datetime.to_s
179 e.dtend Time.parse session.end_datetime.to_s
180 e.summary session.title
e41c1af @aslakknutsen Add Awstruct::Extension::Lanyrd::Export
aslakknutsen authored
181 e.description session.description
a83a2e7 @mojavelinux use cached date as date of session update in ics file
mojavelinux authored
182 e.categories ['SESSION']
183 e.url session.detail_url
184 e.set_text 'LOCATION', session.event
185 e.sequence 0
186 e.access_class 'PUBLIC'
187
188 e.created session.updated
189 e.lastmod session.updated
e41c1af @aslakknutsen Add Awstruct::Extension::Lanyrd::Export
aslakknutsen authored
190
191 e.organizer do |o|
192 o.cn = session.event
193 o.uri = session.event_url
194 end
195
196 session.speaker_names do |speaker|
197 attendee = Vpim::Icalendar::Address.create(speaker)
198 attendee.rsvp = true
199 e.add_attendee attendee
200 end
201 end
202 end
203
204 input_page = File.join( File.dirname(__FILE__), 'lanyrd.export.haml' )
205 page = site.engine.load_page( input_page )
206 page.date = page.timestamp unless page.timestamp.nil?
207 page.output_path = @output_path
208 page.session_ical = cal.encode
209 site.pages << page
210
211 end
212 end
8318595 @aslakknutsen Add Lanyrd extension with session Search support
aslakknutsen authored
213 end
214 end
Something went wrong with that request. Please try again.