PNA.fi koodi

juvenes.pl 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. my @restaurant_info = (
  9. [ "(TaY) Yliopiston Ravintola", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola.aspx", "M", "left", 13, 60 ],
  10. [ "(TaY) Yliopiston Ravintola / Salaattibaari", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola/salaattibaari.aspx", "", "left", 13, 5 ],
  11. [ "(TaY) Fusion Kitchen", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/yliopistonravintola/fusionkitchen.aspx", "", "left", 13, 3 ],
  12. [ $pinni_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tayp%C3%A4%C3%A4kampus/caf%C3%A9lunchpinni.aspx", "M", "middle", 31, 60 ],
  13. [ $bio_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/medicabio.aspx", "M", "left", 5, 60 ],
  14. [ $kliininen_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/medicaarvo.aspx", "M", "left", 27, 60 ],
  15. [ $kliininen_fusion_title, "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/taykaupinkampus/medicaarvo/fusionkitchen.aspx", "M", "left", 27, 3 ],
  16. [ "(TTY) Newton", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/newton.aspx", "", "left", 6, 60],
  17. [ "(TTY) Zip", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/zip.aspx", "", "right", 12, 60 ],
  18. [ "(TTY) Edison", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/edison.aspx", "", "middle", 2, 60 ],
  19. [ "(TTY) Pastabaari", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9pastabaarivoltti.aspx", "", "middle", 26, 11 ],
  20. [ "(TTY) Fast Voltti", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/caf%C3%A9fastvoltti.aspx", "", "middle", 2, 4 ],
  21. [ "(TTY) Fusion Kitchen", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/ttykampus/newton/fusionkitchen.aspx", "", "left", 6, 3 ],
  22. [ "(TAMK) Dot", "http://www.juvenes.fi/fi-fi/ravintolatjakahvilat/opiskelijaravintolat/tamkdot.aspx", "", "middle", 15, 1 ]
  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("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. my $menuitems = $meal_info->{'MenuItems'};
  105. # loops different foods in a meal
  106. foreach my $food_info (@$menuitems) {
  107. $cur_food .= "\n" if $cur_food ne "";
  108. my $name = $food_info->{'Name'};
  109. $name =~ s/^\*//;
  110. $cur_food .= $name;
  111. $cur_food .= " (" . $food_info->{'Diets'} . ")" if $food_info->{'Diets'};
  112. }
  113. finish_food();
  114. }
  115. }
  116. finish_day();
  117. }
  118. }
  119. push @restaurants, [ $title, $open_hours, $week, [ @week_foods ], \@info ];
  120. $count++;
  121. }
  122. return @restaurants;
  123. }
  124. 1;