PNA.fi koodi

amica.pl 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. [ "(TaY) Tampereen normaalikoulun ravintola", "http://www.amica.fi/tampereennormaalikoulu", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=6655" ],
  6. [ "(TTY) Ravintola Reaktori", "http://www.amica.fi/reaktori", "", "middle", "http://www.amica.fi/api/restaurant/menu/week?language=fi&restaurantPageId=69171" ]
  7. );
  8. sub utf8_to_8859 {
  9. $_ = shift;
  10. s/ä/ä/g;
  11. s/ö/ö/g;
  12. s/Ä/Ä/g;
  13. s/Ö/Ö/g;
  14. return $_;
  15. }
  16. sub parse_amica {
  17. my ($fname, $info_ref) = @_;
  18. open(FILE, $fname);
  19. my $json = do { local $/; <FILE> };
  20. close(FILE);
  21. my $title = @{$info_ref}[0];
  22. my $week = `date +%V`;
  23. my @cur_day_foods = ();
  24. my @week_foods = ();
  25. my $data = from_json($json);
  26. my $LunchMenus = $data->{'LunchMenus'};
  27. foreach my $LunchMenu (@$LunchMenus) {
  28. my $SetMenus = $LunchMenu->{'SetMenus'};
  29. my $Html = $LunchMenu->{'Html'};
  30. if (length($Html) > 0) {
  31. my @Menus = split('<p>', $Html);
  32. foreach my $SetMenu (@Menus) {
  33. my @Meals = split('<br />', $SetMenu);
  34. foreach my $Meal (@Meals) {
  35. my @parts = split('\\(', $Meal);
  36. $cur_food .= "\n" if $cur_food ne "";
  37. $cur_food .= @parts[0];
  38. my @Diets = split(",\s*", substr(@parts[1], 0, index(@parts[1], ')')));
  39. $cur_food .= ' (' . join(', ', @Diets) . ')' if @Diets;
  40. }
  41. push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne "");
  42. $cur_food = "";
  43. }
  44. } else {
  45. foreach my $SetMenu (@$SetMenus) {
  46. my $Meals = $SetMenu->{'Meals'};
  47. foreach my $Meal (@$Meals) {
  48. $cur_food .= "\n" if $cur_food ne "";
  49. $cur_food .= $Meal->{'Name'};
  50. my $Diets = $Meal->{'Diets'};
  51. $cur_food .= ' (' . join(', ', @$Diets) . ')';
  52. }
  53. push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne "");
  54. $cur_food = "";
  55. }
  56. }
  57. push @week_foods, [@cur_day_foods];
  58. @cur_day_foods = ();
  59. }
  60. return [ $title, "", $week, [ @week_foods ], $info_ref ];
  61. }
  62. sub get_amica_restaurant {
  63. my $use_old = shift;
  64. my $count = 0;
  65. my @restaurants = ();
  66. my $weekDay = `date +%w`;
  67. chomp($weekDay);
  68. my $first_day = $weekDay == 1
  69. ? `date --date="today" +%Y-%m-%d`
  70. : `date --date="last monday" +%Y-%m-%d`;
  71. chomp($first_day);
  72. my $last_day = $weekDay == 0
  73. ? `date --date="today" +%Y-%m-%d`
  74. : `date --date="next sunday" +%Y-%m-%d`;
  75. chomp($last_day);
  76. foreach my $i (@restaurant_info) {
  77. my @info = @{$i};
  78. my $temp_fname = "amica$count.temp.html";
  79. my $url = "${info[4]}&weekDate=$first_day";
  80. if (!-f $temp_fname || !$use_old) {
  81. system("wget -q --timeout=10 -O $temp_fname.tmp '$url' && mv $temp_fname.tmp $temp_fname") if ($url ne "");
  82. }
  83. if (-f $temp_fname) {
  84. $info[4] = $url;
  85. push @restaurants, parse_amica($temp_fname, \@info);
  86. }
  87. $count++;
  88. }
  89. return @restaurants;
  90. }
  91. 1;