use vars qw(@day_names); use JSON; my @restaurant_info = ( [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=7381" ], [ "(TaY) Tampereen normaalikoulun ravintola", "http://www.amica.fi/tampereennormaalikoulu", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=6655" ], [ "(TTY) Ravintola Reaktori", "http://www.amica.fi/reaktori", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=69171" ] ); sub utf8_to_8859 { $_ = shift; s/ä/ä/g; s/ö/ö/g; s/Ä/Ä/g; s/Ö/Ö/g; return $_; } sub parse_amica { my ($fname, $info_ref) = @_; open(FILE, $fname); my $json = do { local $/; }; close(FILE); my $title = @{$info_ref}[0]; my $week = `date +%V`; my @cur_day_foods = (); my @week_foods = (); my $data = from_json($json); my $LunchMenus = $data->{'LunchMenus'}; foreach my $LunchMenu (@$LunchMenus) { my $SetMenus = $LunchMenu->{'SetMenus'}; my $Html = $LunchMenu->{'Html'}; if (length($Html) > 0) { my @Menus = split('

', $Html); foreach my $SetMenu (@Menus) { my @Meals = split('
', $SetMenu); foreach my $Meal (@Meals) { my @parts = split('\\(', $Meal); $cur_food .= "\n" if $cur_food ne ""; $cur_food .= @parts[0]; my @Diets = split(",\s*", substr(@parts[1], 0, index(@parts[1], ')'))); $cur_food .= ' (' . join(', ', @Diets) . ')' if @Diets; } push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne ""); $cur_food = ""; } } else { foreach my $SetMenu (@$SetMenus) { my $Meals = $SetMenu->{'Meals'}; foreach my $Meal (@$Meals) { $cur_food .= "\n" if $cur_food ne ""; $cur_food .= $Meal->{'Name'}; my $Diets = $Meal->{'Diets'}; $cur_food .= ' (' . join(', ', @$Diets) . ')'; } push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne ""); $cur_food = ""; } } push @week_foods, [@cur_day_foods]; @cur_day_foods = (); } return [ $title, "", $week, [ @week_foods ], $info_ref ]; } sub get_amica_restaurant { my $use_old = shift; my $count = 0; my @restaurants = (); my $weekDay = `date +%w`; chomp($weekDay); my $first_day = $weekDay == 1 ? `date --date="today" +%Y-%m-%d` : `date --date="last monday" +%Y-%m-%d`; chomp($first_day); my $last_day = $weekDay == 0 ? `date --date="today" +%Y-%m-%d` : `date --date="next sunday" +%Y-%m-%d`; chomp($last_day); foreach my $i (@restaurant_info) { my @info = @{$i}; my $temp_fname = "amica$count.temp.html"; my $url = "${info[4]}&weekDate=$first_day"; if (!-f $temp_fname || !$use_old) { system("wget -q --timeout=10 -O $temp_fname.tmp '$url' && mv $temp_fname.tmp $temp_fname") if ($url ne ""); } if (-f $temp_fname) { $info[4] = $url; push @restaurants, parse_amica($temp_fname, \@info); } $count++; } return @restaurants; } 1;