use vars qw(@day_names); use JSON; my @restaurant_info = ( [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "http://www.amica.fi/modules/json/json/Index?costNumber=0815" ], [ "(TTY) Ravintola Reaktori", "http://www.amica.fi/reaktori", "", "middle", "http://www.amica.fi/modules/json/json/Index?costNumber=0812" ] ); 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 $MenusForDays = $data->{'MenusForDays'}; foreach my $MenuForDay (@$MenusForDays) { my $SetMenus = $MenuForDay->{'SetMenus'}; foreach my $SetMenu (@$SetMenus) { my $Components = $SetMenu->{'Components'}; foreach my $Component (@$Components) { $cur_food .= "\n" if $cur_food ne ""; $cur_food .= $Component; } 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]}&firstDay=$first_day&lastDay=$last_day&language=fi"; 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;