PNA.fi koodi

juvenes.pl 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. use vars qw(@day_names);
  2. use JSON;
  3. my $pinni_title = "(TaY) Café Pinni";
  4. my $bio_title = "(TAYS) Bio";
  5. my $kliininen_title = "(TAYS) Arvo";
  6. my $kliininen_fusion_title = "(TAYS) Arvo Fusion Kitchen";
  7. my $zip_salaattibaari_title = "(TTY) Zip Salaattibaari";
  8. # last two in order are $kitchen_info_id, $menu_type_id
  9. my @restaurant_info = (
  10. [ "(TaY) Yliopiston Ravintola", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola.aspx", "M", "left", 13, 60 ],
  11. [ "(TaY) Yliopiston Ravintola / Rohee Xtra", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola/roheextra.aspx", "M", "left", 13, 74 ],
  12. [ "(TaY) Yliopiston Ravintola / Salaattibaari", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola/salaattibaari.aspx", "", "left", 13, 5 ],
  13. [ "(TaY) Fusion Kitchen", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola/fusionkitchen.aspx", "", "left", 13, 3 ],
  14. [ $pinni_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/caf%C3%A9lunchpinni.aspx", "M", "middle", 130016, 60 ],
  15. [ $bio_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/medicabio.aspx", "M", "left", 5, 60 ],
  16. [ $kliininen_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/medicaarvo.aspx", "M", "left", 50026, 60 ],
  17. [ $kliininen_fusion_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/medicaarvo/fusionkitchen.aspx", "M", "left", 50026, 3 ],
  18. [ "(TTY) Newton", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/newton.aspx", "", "left", 6, 60],
  19. [ "(TTY) Newton / Rohee Xtra", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/newton/roheextra.aspx", "", "left", 6, 74],
  20. [ "(TTY) Café Konehuone", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9konehuone.aspx", "", "middle", 60038, 77 ],
  21. [ "(TTY) Café Konehuone / Fusion Kitchen", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9konehuone/fusionkitchen.aspx", "", "middle", 60038, 3 ],
  22. [ "(TAMK) Ziberia", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/henkil%C3%B6st%C3%B6ravintolat/ziberia.aspx", "", "middle", 11, 60 ]
  23. );
  24. my @restaurants;
  25. my ($open_hours, $day_id, $cur_title);
  26. my ($cur_food, @cur_day_foods, @week_foods);
  27. sub finish_food {
  28. chomp $cur_food;
  29. if ($cur_food =~ /Liha paniini.*tai Kasvis paniini/i && $cur_title eq $pinni_title) {
  30. # you get this every day, ignore
  31. } else {
  32. push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne "");
  33. }
  34. $cur_food = "";
  35. }
  36. sub finish_day {
  37. push @week_foods, [@cur_day_foods];
  38. @cur_day_foods = ();
  39. $day_id = $day_id + 1;
  40. }
  41. sub can_merge_bio_kliininen {
  42. my $day = shift;
  43. my $bio_foods = "";
  44. my $kliininen_foods = "";
  45. my $food_dest;
  46. foreach my $r (@restaurants) {
  47. my ($title, $open_hours, $week, $week_foods_ref) = @{$r};
  48. if ($title eq $bio_title) {
  49. $food_dest = \$bio_foods;
  50. } elsif ($title eq $kliininen_title) {
  51. $food_dest = \$kliininen_foods;
  52. } else {
  53. next;
  54. }
  55. my @week_foods = @{$week_foods_ref};
  56. foreach my $food (@{$week_foods[$day]}) {
  57. ${$food_dest} .= "$food\n";
  58. }
  59. }
  60. return $bio_foods eq $kliininen_foods;
  61. }
  62. sub try_merge_bio_kliininen {
  63. my ($title_ref, $day) = @_;
  64. my $title = $$title_ref;
  65. if ($title eq $bio_title && can_merge_bio_kliininen($day)) {
  66. $$title_ref .= " + Kliininen";
  67. } elsif ($title eq $kliininen_title && can_merge_bio_kliininen($day)) {
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. sub get_juvenes_restaurants {
  73. my $use_old = shift;
  74. my $count = 0;
  75. # Loops restraurants
  76. foreach my $i (@restaurant_info) {
  77. my @info = @{$i};
  78. my $kitchen = $info[4];
  79. my $menutype = $info[5];
  80. $title = $info[0];
  81. $cur_title = $title;
  82. $open_hours = "";
  83. @week_foods = ();
  84. my $week = `date +%V`;
  85. chomp($week);
  86. # Loop weekdays
  87. for (my $weekday = 1; $weekday < 7; $weekday++) {
  88. my $temp_fname = "juvenes$count-$weekday.temp.js";
  89. my $url = "http://www.juvenes.fi/DesktopModules/Talents.LunchMenu/LunchMenuServices.asmx/GetMenuByWeekday?KitchenId=$kitchen&MenuTypeId=$menutype&Week=$week&Weekday=$weekday&lang='fi'&format=json";
  90. if (!-f $temp_fname || !$use_old) {
  91. system("rm -f $temp_fname && wget -q --timeout=10 -O $temp_fname.tmp \"$url\" && mv $temp_fname.tmp $temp_fname");
  92. }
  93. if (-f $temp_fname) {
  94. open(FILE, $temp_fname);
  95. my $jsonp = do { local $/; <FILE> };
  96. close(FILE);
  97. # the file is encapsulated in ({"d": json}); so we have to double parse it
  98. my $data = from_json(substr($jsonp, 1, -2));
  99. unless ($data->{'d'} eq 'null') {
  100. $data = from_json($data->{'d'});
  101. my $mealoptions = $data->{'MealOptions'};
  102. # loop different meals
  103. foreach my $meal_info (@$mealoptions) {
  104. if ($meal_info->{'ForceMajeure'} ne '') {
  105. $cur_food = $meal_info->{'ForceMajeure'};
  106. } else {
  107. my $menuitems = $meal_info->{'MenuItems'};
  108. # loops different foods in a meal
  109. foreach my $food_info (@$menuitems) {
  110. $cur_food .= "\n" if $cur_food ne "";
  111. my $name = $food_info->{'Name'};
  112. $name =~ s/^\*//;
  113. $cur_food .= $name;
  114. $cur_food .= " (" . $food_info->{'Diets'} . ")" if $food_info->{'Diets'};
  115. }
  116. }
  117. if ($cur_food ne "-") {
  118. finish_food();
  119. } else {
  120. $cur_food = "";
  121. }
  122. }
  123. }
  124. finish_day();
  125. }
  126. }
  127. push @restaurants, [ $title, $open_hours, $week, [ @week_foods ], \@info ];
  128. $count++;
  129. }
  130. return @restaurants;
  131. }
  132. 1;