PNA.fi koodi

juvenes.pl 5.7KB

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