PNA.fi koodi

juvenes.pl 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. use vars qw(@day_names);
  2. use JSON;
  3. # last two in order are $kitchen_info_id, $menu_type_id
  4. my @restaurant_info = (
  5. [ "(TaY) Yliopiston Ravintola", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola.aspx", "M", "left", 13, 60 ],
  6. [ "(TaY) Yliopiston Ravintola / VegeBar", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola.aspx", "", "left", 13, 5 ],
  7. [ "(TaY) Café Campus", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/caf%C3%A9campus.aspx", "", "left", 130019, 23 ],
  8. [ "(TaY) Café Pinni", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/caf%C3%A9lunchpinni.aspx", "M", "middle", 130016, 60 ],
  9. [ "(TAYS) Arvo", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/arvo.aspx", "M", "left", 5, 60 ],
  10. [ "(TAYS) Café Lea (Fusion Kitchen)", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/cafélea.aspx", "M", "left", 50026, 3 ],
  11. [ "(TAYS) Café Lea (My Salad)", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/cafélea.aspx", "M", "left", 50026, 76 ],
  12. [ "(TTY) Newton", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/newton.aspx", "", "left", 6, 60],
  13. [ "(TTY) Café Konehuone / Såås bar", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9konehuone.aspx", "", "left", 60038, 77],
  14. [ "(TTY) Café Konehuone / Fusion Kitchen", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9konehuone.aspx", "", "middle", 60038, 3 ],
  15. [ "(TAMK) Ziberia", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/ziberia.aspx", "", "middle", 11, 60 ],
  16. # [ "(TAMK) Frenckell", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/frenckell.aspx", "", "middle", 33, 60 ],
  17. [ "(TAMK) Frenckell / Såås bar", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/frenckell.aspx", "", "middle", 33, 77 ]
  18. );
  19. my @restaurants;
  20. my ($open_hours, $day_id, $cur_title);
  21. my ($cur_food, @cur_day_foods, @week_foods);
  22. sub finish_food {
  23. chomp $cur_food;
  24. if ($cur_food =~ /Liha paniini.*tai Kasvis paniini/i && $cur_title eq $pinni_title) {
  25. # you get this every day, ignore
  26. } else {
  27. push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne "");
  28. }
  29. $cur_food = "";
  30. }
  31. sub finish_day {
  32. push @week_foods, [@cur_day_foods];
  33. @cur_day_foods = ();
  34. $day_id = $day_id + 1;
  35. }
  36. sub get_juvenes_restaurants {
  37. my $use_old = shift;
  38. my $count = 0;
  39. # Loops restraurants
  40. foreach my $i (@restaurant_info) {
  41. my @info = @{$i};
  42. my $kitchen = $info[4];
  43. my $menutype = $info[5];
  44. $title = $info[0];
  45. $cur_title = $title;
  46. $open_hours = "";
  47. @week_foods = ();
  48. my $week = `date +%V`;
  49. chomp($week);
  50. # Loop weekdays
  51. for (my $weekday = 1; $weekday < 7; $weekday++) {
  52. my $temp_fname = "juvenes$count-$weekday.temp.js";
  53. my $url = "http://www.juvenes.fi/DesktopModules/Talents.LunchMenu/LunchMenuServices.asmx/GetMenuByWeekday?KitchenId=$kitchen&MenuTypeId=$menutype&Week=$week&Weekday=$weekday&lang='fi'&format=json";
  54. if (!-f $temp_fname || !$use_old) {
  55. system("rm -f $temp_fname && wget -q --timeout=10 -O $temp_fname.tmp \"$url\" && mv $temp_fname.tmp $temp_fname");
  56. }
  57. if (-f $temp_fname) {
  58. open(FILE, $temp_fname);
  59. my $jsonp = do { local $/; <FILE> };
  60. close(FILE);
  61. # the file is encapsulated in ({"d": json}); so we have to double parse it
  62. my $data = from_json(substr($jsonp, 1, -2));
  63. unless ($data->{'d'} eq 'null') {
  64. $data = from_json($data->{'d'});
  65. my $mealoptions = $data->{'MealOptions'};
  66. # loop different meals
  67. foreach my $meal_info (@$mealoptions) {
  68. if ($meal_info->{'ForceMajeure'} ne '') {
  69. $cur_food = $meal_info->{'ForceMajeure'};
  70. } else {
  71. my $menuitems = $meal_info->{'MenuItems'};
  72. # loops different foods in a meal
  73. foreach my $food_info (@$menuitems) {
  74. $cur_food .= "\n" if $cur_food ne "";
  75. my $name = $food_info->{'Name'};
  76. $name =~ s/^\*//;
  77. $cur_food .= $name;
  78. $cur_food .= " (" . $food_info->{'Diets'} . ")" if $food_info->{'Diets'};
  79. }
  80. }
  81. if ($cur_food ne "-") {
  82. finish_food();
  83. } else {
  84. $cur_food = "";
  85. }
  86. }
  87. }
  88. finish_day();
  89. }
  90. }
  91. push @restaurants, [ $title, $open_hours, $week, [ @week_foods ], \@info ];
  92. $count++;
  93. }
  94. return @restaurants;
  95. }
  96. 1;