use vars qw(@day_names); use POSIX qw(strftime); my @restaurant_info = ( [ "(TaY) Sodexo Linna", "http://www.sodexo.fi/linna", "", "right", 92], #[ "(TTY) Sodexo Erkkeri", "http://www.sodexo.fi/erkkeri", "", "left", 100] ); my ($cur_text, $cur_title, $parse_func, $day_id, $week); my (@cur_day_foods, @week_foods); sub sodexo_finish_food { chomp $cur_food; push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne ""); $cur_food = ""; } sub sodexo_finish_day { push @week_foods, [@cur_day_foods]; @cur_day_foods = (); $day_id = $day_id + 1; } sub utf8_to_8859 { $_ = shift; s/ä/ä/g; s/ö/ö/g; s/Ä/Ä/g; s/Ö/Ö/g; return $_; } sub get_sodexo_restaurants { my $use_old = shift; my $count = 0; # Loops restraurants foreach my $i (@restaurant_info) { my @info = @{$i}; my $kitchen = $info[4]; $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++) { # Get current unix timestamp and week day $s = strftime "%s", localtime; $v = strftime "%w", localtime; # Calculate current weekday $s = $s + ($weekday - $v) * 86400; $timestr = strftime "%Y/%m/%d", localtime($s); my $url = "http://www.sodexo.fi/ruokalistat/output/daily_json/$kitchen/$timestr/fi"; my $temp_fname = "sodexo$count-$weekday.temp.js"; if (!-f $temp_fname || !$use_old) { system("wget -q --timeout=10 -O $temp_fname.tmp \"$url\" && mv $temp_fname.tmp $temp_fname"); } if (-f $temp_fname) { open(FILE, $temp_fname); my $json = do { local $/; }; close(FILE); # the file is encapsulated in ({"d": json}); so we have to double parse it my $data = from_json($json); my $courses = $data->{'courses'}; # loop different meals foreach my $course_info (@$courses) { if ($course_info->{'category'} ne 'Aamupala') { $cur_food = $course_info->{'title_fi'}; $cur_food .= " (" . $course_info->{'properties'} . ")" if $course_info->{'properties'}; } sodexo_finish_food(); } sodexo_finish_day(); } } push @restaurants, [ $title, $open_hours, $week, [ @week_foods ], \@info ]; $count++; } return @restaurants; } 1;