Skip to content

Commit c78f810

Browse files
committed
Update Frank and Frary menu scraping for 2021
1 parent c9397a6 commit c78f810

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

lib/tasks/menu_import.rake

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,22 @@ namespace :menu_import do
245245

246246
# Pomona's system is batshit crazy (user inputted text in Google Docs),
247247
# so we're just going to scrape from their website instead
248-
browser = Watir::Browser.new :chrome, headless: true
248+
browser = Watir::Browser.new :chrome, headless: true, :args => [ "--no-sandbox" ]
249249
browser.goto 'www.pomona.edu/administration/dining/menus/frary'
250250

251-
menu = browser.div(:class => 'pom-accordion')
251+
# The website was updated during COVID, so the structure is slightly different
252+
# e.g. no need to click this button
253+
# browser.button(class: 'accordion__header').click
254+
255+
menu = browser.div(class: ['accordion', 'js-accordion'])
256+
# menu.each do |e|
257+
# menu = e
258+
# end
252259
meal_type = ''
253260
station = ''
254261
meal_menu = Menu
255262

256-
menu_panels = menu.h3s(:class => 'ui-accordion-header')
263+
menu_panels = menu.buttons(:class => 'accordion__header')
257264
panel_count = 1 # the next panel to open
258265

259266
# Map the successive divs to be pairs of {day, menu}
@@ -265,7 +272,7 @@ namespace :menu_import do
265272
# meal type (h2)
266273
# station (h3)
267274
# menu items (div)
268-
pair[:menu].children.each do |div|
275+
pair[:menu].div.children.each do |div|
269276
if div.tag_name == "h2"
270277
meal_type = div.text.downcase
271278
if (pair[:day] == 'saturday' || pair[:day] == 'sunday') && meal_type == 'breakfast'
@@ -282,12 +289,12 @@ namespace :menu_import do
282289
)
283290
elsif div.tag_name == "div" && div.class_name == "nutrition-menu-section"
284291
div.children.each do |menu_item|
285-
MenuItem.create(:name => menu_item.p.text, :station => station, :menu => meal_menu)
292+
MenuItem.find_or_create_by(:name => menu_item.p.text, :station => station, :menu => meal_menu)
286293
end
287294
end
288295
end
289296
# simulate click to open the next panel after parsing the current one
290-
menu_panels[panel_count].fire_event('click') unless panel_count > 6
297+
menu_panels[panel_count].fire_event('click') unless panel_count >= menu_panels.length
291298
panel_count += 1
292299
end
293300

@@ -306,15 +313,19 @@ namespace :menu_import do
306313

307314
# Pomona's system is batshit crazy (user inputted text in Google Docs),
308315
# so we're just going to scrape from their website instead
309-
browser = Watir::Browser.new :chrome, headless: true
316+
browser = Watir::Browser.new :chrome, headless: true, :args => [ "--no-sandbox" ]
310317
browser.goto 'www.pomona.edu/administration/dining/menus/frank'
311318

312-
menu = browser.div(:class => 'pom-accordion')
319+
# The website was updated during COVID, so the structure is slightly different
320+
# e.g. class names have been slightly changed
321+
# e.g. Frank is now open on Fridays sometimes
322+
323+
menu = browser.div(class: ['accordion', 'js-accordion'])
313324
meal_type = ''
314325
station = ''
315326
meal_menu = Menu
316327

317-
menu_panels = menu.h3s(:class => 'ui-accordion-header')
328+
menu_panels = menu.buttons(:class => 'accordion__header')
318329
panel_count = 1 # the next panel to open
319330

320331
# Map the successive divs to be pairs of {day, menu}
@@ -326,31 +337,33 @@ namespace :menu_import do
326337
# meal type (h2)
327338
# station (h3)
328339
# menu items (div)
329-
unless pair[:day] == 'friday' || pair[:day] == 'saturday'
330-
pair[:menu].children.each do |div|
331-
if div.tag_name == "h2"
332-
meal_type = div.text.downcase
333-
if pair[:day] == 'sunday' && meal_type == 'breakfast'
334-
meal_type = 'brunch'
335-
end
336-
elsif div.tag_name == "h3"
337-
station = div.text
338-
hours = _get_pomona_hours('Frank', Date.strptime(pair[:day], '%A').wday, meal_type)
339-
meal_menu = Menu.find_or_create_by(
340-
:day => pair[:day],
341-
:dining_hall => :frank,
342-
:meal_type => meal_type,
343-
:hours => hours
344-
)
345-
elsif div.tag_name == "div" && div.class_name == "nutrition-menu-section"
346-
div.children.each do |menu_item|
347-
MenuItem.create(:name => menu_item.p.text, :station => station, :menu => meal_menu)
348-
end
340+
pair[:menu].div.children.each do |div|
341+
if div.tag_name == "p"
342+
# <p> is used to denote that Frank is closed that day
343+
next
344+
end
345+
if div.tag_name == "h2"
346+
meal_type = div.text.downcase
347+
if pair[:day] == 'sunday' && meal_type == 'breakfast'
348+
meal_type = 'brunch'
349+
end
350+
elsif div.tag_name == "h3"
351+
station = div.text
352+
hours = _get_pomona_hours('Frank', Date.strptime(pair[:day], '%A').wday, meal_type)
353+
meal_menu = Menu.find_or_create_by(
354+
:day => pair[:day],
355+
:dining_hall => :frank,
356+
:meal_type => meal_type,
357+
:hours => hours
358+
)
359+
elsif div.tag_name == "div" && div.class_name == "nutrition-menu-section"
360+
div.children.each do |menu_item|
361+
MenuItem.find_or_create_by(:name => menu_item.p.text, :station => station, :menu => meal_menu)
349362
end
350363
end
351364
end
352365
# simulate click to open the next panel after parsing the current one
353-
menu_panels[panel_count].fire_event('click') unless panel_count > 6
366+
menu_panels[panel_count].fire_event('click') unless panel_count >= menu_panels.length
354367
panel_count += 1
355368
end
356369

@@ -369,7 +382,7 @@ namespace :menu_import do
369382

370383
# Pomona's system is batshit crazy (user inputted text in Google Docs),
371384
# so we're just going to scrape from their website instead
372-
browser = Watir::Browser.new :chrome, headless: true
385+
browser = Watir::Browser.new :chrome, headless: true, :args => [ "--no-sandbox" ]
373386
browser.goto 'www.pomona.edu/administration/dining/menus/oldenborg'
374387

375388
menu = browser.div(:class => 'pom-accordion')
@@ -494,4 +507,4 @@ namespace :menu_import do
494507
end
495508
end
496509
end
497-
end
510+
end

0 commit comments

Comments
 (0)