PNA.fi koodi

amica.pl 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. use vars qw(@day_names);
  2. use JSON;
  3. my @restaurant_info = (
  4. [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=7381" ],
  5. [ "(TTY) Ravintola Reaktori", "http://www.amica.fi/reaktori", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=69171" ]
  6. );
  7. sub utf8_to_8859 {
  8. $_ = shift;
  9. s/ä/ä/g;
  10. s/ö/ö/g;
  11. s/Ä/Ä/g;
  12. s/Ö/Ö/g;
  13. return $_;
  14. }
  15. sub parse_amica {
  16. my ($fname, $info_ref) = @_;
  17. open(FILE, $fname);
  18. my $json = do { local $/; <FILE> };
  19. close(FILE);
  20. my $title = @{$info_ref}[0];
  21. my $week = `date +%V`;
  22. my @cur_day_foods = ();
  23. my @week_foods = ();
  24. my $data = from_json($json);
  25. my $LunchMenus = $data->{'LunchMenus'};
  26. foreach my $LunchMenu (@$LunchMenus) {
  27. my $SetMenus = $LunchMenu->{'SetMenus'};
  28. foreach my $SetMenu (@$SetMenus) {
  29. my $Meals = $SetMenu->{'Meals'};
  30. foreach my $Meal (@$Meals) {
  31. $cur_food .= "\n" if $cur_food ne "";
  32. $cur_food .= $Meal->{'Name'};
  33. my $Diets = $Meal->{'Diets'};
  34. $cur_food .= ' (' . join(', ', @$Diets) . ')';
  35. }
  36. push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne "");
  37. $cur_food = "";
  38. }
  39. push @week_foods, [@cur_day_foods];
  40. @cur_day_foods = ();
  41. }
  42. return [ $title, "", $week, [ @week_foods ], $info_ref ];
  43. }
  44. sub get_amica_restaurant {
  45. my $use_old = shift;
  46. my $count = 0;
  47. my @restaurants = ();
  48. my $weekDay = `date +%w`;
  49. chomp($weekDay);
  50. my $first_day = $weekDay == 1
  51. ? `date --date="today" +%Y-%m-%d`
  52. : `date --date="last monday" +%Y-%m-%d`;
  53. chomp($first_day);
  54. my $last_day = $weekDay == 0
  55. ? `date --date="today" +%Y-%m-%d`
  56. : `date --date="next sunday" +%Y-%m-%d`;
  57. chomp($last_day);
  58. foreach my $i (@restaurant_info) {
  59. my @info = @{$i};
  60. my $temp_fname = "amica$count.temp.html";
  61. my $url = "${info[4]}&weekDate=$first_day";
  62. if (!-f $temp_fname || !$use_old) {
  63. system("wget -q --timeout=10 -O $temp_fname.tmp '$url' && mv $temp_fname.tmp $temp_fname") if ($url ne "");
  64. }
  65. if (-f $temp_fname) {
  66. $info[4] = $url;
  67. push @restaurants, parse_amica($temp_fname, \@info);
  68. }
  69. $count++;
  70. }
  71. return @restaurants;
  72. }
  73. 1;