PNA.fi koodi

sodexo.pl 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. use vars qw(@day_names);
  2. use POSIX qw(strftime);
  3. my @restaurant_info = (
  4. #[ "(TaY) Sodexo Linna", "http://www.sodexo.fi/fi-FI/linna/lounas/", "right" ],
  5. #[ "(TTY) Sodexo Erkkeri", "http://www.sodexo.fi/fi-FI/erkkeri/lounas/", "left" ]
  6. [ "(TaY) Sodexo Linna", "http://www.sodexo.fi/linna", "", "right", 92],
  7. [ "(TTY) Sodexo Erkkeri", "http://www.sodexo.fi/erkkeri", "", "left", 100]
  8. );
  9. my ($cur_text, $cur_title, $parse_func, $day_id, $week);
  10. my (@cur_day_foods, @week_foods);
  11. sub sodexo_finish_food {
  12. chomp $cur_food;
  13. push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne "");
  14. $cur_food = "";
  15. }
  16. sub sodexo_finish_day {
  17. push @week_foods, [@cur_day_foods];
  18. @cur_day_foods = ();
  19. $day_id = $day_id + 1;
  20. }
  21. sub utf8_to_8859 {
  22. $_ = shift;
  23. s/ä/ä/g;
  24. s/ö/ö/g;
  25. s/Ä/Ä/g;
  26. s/Ö/Ö/g;
  27. return $_;
  28. }
  29. sub get_sodexo_restaurants {
  30. my $use_old = shift;
  31. my $count = 0;
  32. # Loops restraurants
  33. foreach my $i (@restaurant_info) {
  34. my @info = @{$i};
  35. my $kitchen = $info[4];
  36. $title = $info[0];
  37. $cur_title = $title;
  38. $open_hours = "";
  39. @week_foods = ();
  40. my $week = `date +%V`;
  41. chomp($week);
  42. # Loop weekdays
  43. for (my $weekday = 1; $weekday < 7; $weekday++) {
  44. # Get current unix timestamp and week day
  45. $s = strftime "%s", localtime;
  46. $v = strftime "%w", localtime;
  47. # Calculate current weekday
  48. $s = $s + ($weekday - $v) * 86400;
  49. $timestr = strftime "%Y/%m/%d", localtime($s);
  50. my $url = "http://www.sodexo.fi/ruokalistat/output/daily_json/$kitchen/$timestr/fi";
  51. my $temp_fname = "sodexo$count-$weekday.temp.js";
  52. if (!-f $temp_fname || !$use_old) {
  53. system("wget -q --timeout=10 -O $temp_fname.tmp \"$url\" && mv $temp_fname.tmp $temp_fname");
  54. }
  55. if (-f $temp_fname) {
  56. open(FILE, $temp_fname);
  57. my $json = do { local $/; <FILE> };
  58. close(FILE);
  59. # the file is encapsulated in ({"d": json}); so we have to double parse it
  60. my $data = from_json($json);
  61. my $courses = $data->{'courses'};
  62. # loop different meals
  63. foreach my $course_info (@$courses) {
  64. if ($course_info->{'category'} ne 'Aamupala') {
  65. $cur_food = $course_info->{'title_fi'};
  66. $cur_food .= " (" . $course_info->{'properties'} . ")" if $course_info->{'properties'};
  67. }
  68. sodexo_finish_food();
  69. }
  70. sodexo_finish_day();
  71. }
  72. }
  73. push @restaurants, [ $title, $open_hours, $week, [ @week_foods ], \@info ];
  74. $count++;
  75. }
  76. return @restaurants;
  77. }
  78. 1;