use vars qw(@day_names); use JSON; my $pinni_title = "(TaY) Café Pinni"; my $bio_title = "(TAYS) Bio"; my $kliininen_title = "(TAYS) Arvo"; my $kliininen_fusion_title = "(TAYS) Arvo Fusion Kitchen"; my $zip_salaattibaari_title = "(TTY) Zip Salaattibaari"; # last two in order are $kitchen_info_id, $menu_type_id my @restaurant_info = ( [ "(TaY) Yliopiston Ravintola", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola.aspx", "M", "left", 13, 60 ], [ "(TaY) Yliopiston Ravintola / Rohee Xtra", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola/roheextra.aspx", "M", "left", 13, 74 ], [ "(TaY) Yliopiston Ravintola / Salaattibaari", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola/salaattibaari.aspx", "", "left", 13, 5 ], [ "(TaY) Fusion Kitchen", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola/fusionkitchen.aspx", "", "left", 13, 3 ], [ $pinni_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/caf%C3%A9lunchpinni.aspx", "M", "middle", 130016, 60 ], [ $bio_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/medicabio.aspx", "M", "left", 5, 60 ], [ $kliininen_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/medicaarvo.aspx", "M", "left", 50026, 60 ], [ $kliininen_fusion_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/medicaarvo/fusionkitchen.aspx", "M", "left", 50026, 3 ], [ "(TTY) Newton", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/newton.aspx", "", "left", 6, 60], [ "(TTY) Newton / Rohee Xtra", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/newton/roheextra.aspx", "", "left", 6, 74], [ "(TTY) Fusion Kitchen", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/newton/fusionkitchen.aspx", "", "left", 6, 3 ], [ "(TTY) Café Konehuone", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9konehuone.aspx", "", "left", 60038, 77 ], [ "(TAMK) Ziberia", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/ziberia.aspx", "", "middle", 11, 60 ] ); my @restaurants; my ($open_hours, $day_id, $cur_title); my ($cur_food, @cur_day_foods, @week_foods); sub finish_food { chomp $cur_food; if ($cur_food =~ /Liha paniini.*tai Kasvis paniini/i && $cur_title eq $pinni_title) { # you get this every day, ignore } else { push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne ""); } $cur_food = ""; } sub finish_day { push @week_foods, [@cur_day_foods]; @cur_day_foods = (); $day_id = $day_id + 1; } sub can_merge_bio_kliininen { my $day = shift; my $bio_foods = ""; my $kliininen_foods = ""; my $food_dest; foreach my $r (@restaurants) { my ($title, $open_hours, $week, $week_foods_ref) = @{$r}; if ($title eq $bio_title) { $food_dest = \$bio_foods; } elsif ($title eq $kliininen_title) { $food_dest = \$kliininen_foods; } else { next; } my @week_foods = @{$week_foods_ref}; foreach my $food (@{$week_foods[$day]}) { ${$food_dest} .= "$food\n"; } } return $bio_foods eq $kliininen_foods; } sub try_merge_bio_kliininen { my ($title_ref, $day) = @_; my $title = $$title_ref; if ($title eq $bio_title && can_merge_bio_kliininen($day)) { $$title_ref .= " + Kliininen"; } elsif ($title eq $kliininen_title && can_merge_bio_kliininen($day)) { return 1; } return 0; } sub get_juvenes_restaurants { my $use_old = shift; my $count = 0; # Loops restraurants foreach my $i (@restaurant_info) { my @info = @{$i}; my $kitchen = $info[4]; my $menutype = $info[5]; $title = $info[0]; $cur_title = $title; $open_hours = ""; @week_foods = (); my $week = `date +%V`; chomp($week); # Loop weekdays for (my $weekday = 1; $weekday < 7; $weekday++) { my $temp_fname = "juvenes$count-$weekday.temp.js"; my $url = "http://www.juvenes.fi/DesktopModules/Talents.LunchMenu/LunchMenuServices.asmx/GetMenuByWeekday?KitchenId=$kitchen&MenuTypeId=$menutype&Week=$week&Weekday=$weekday&lang='fi'&format=json"; if (!-f $temp_fname || !$use_old) { system("rm -f $temp_fname && wget -q --timeout=10 -O $temp_fname.tmp \"$url\" && mv $temp_fname.tmp $temp_fname"); } if (-f $temp_fname) { open(FILE, $temp_fname); my $jsonp = do { local $/; }; close(FILE); # the file is encapsulated in ({"d": json}); so we have to double parse it my $data = from_json(substr($jsonp, 1, -2)); unless ($data->{'d'} eq 'null') { $data = from_json($data->{'d'}); my $mealoptions = $data->{'MealOptions'}; # loop different meals foreach my $meal_info (@$mealoptions) { if ($meal_info->{'ForceMajeure'} ne '') { $cur_food = $meal_info->{'ForceMajeure'}; } else { my $menuitems = $meal_info->{'MenuItems'}; # loops different foods in a meal foreach my $food_info (@$menuitems) { $cur_food .= "\n" if $cur_food ne ""; my $name = $food_info->{'Name'}; $name =~ s/^\*//; $cur_food .= $name; $cur_food .= " (" . $food_info->{'Diets'} . ")" if $food_info->{'Diets'}; } } if ($cur_food ne "-") { finish_food(); } else { $cur_food = ""; } } } finish_day(); } } push @restaurants, [ $title, $open_hours, $week, [ @week_foods ], \@info ]; $count++; } return @restaurants; } 1;