123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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 $/; <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'};
- my $Html = $LunchMenu->{'Html'};
- if (length($Html) > 0) {
- my @Menus = split('<p>', $Html);
- foreach my $SetMenu (@Menus) {
- my @Meals = split('<br />', $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;
|