1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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" ],
- [ "(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 $/; <FILE> };
- 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'};
- 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;
|