PNA.fi koodi

sodexo.pl 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. print $url . " -> " . $temp_fname . "\n";
  54. system("wget -q --timeout=10 -O $temp_fname.tmp \"$url\" && mv $temp_fname.tmp $temp_fname");
  55. }
  56. if (-f $temp_fname) {
  57. open(FILE, $temp_fname);
  58. my $json = do { local $/; <FILE> };
  59. close(FILE);
  60. # the file is encapsulated in ({"d": json}); so we have to double parse it
  61. my $data = from_json($json);
  62. my $courses = $data->{'courses'};
  63. # loop different meals
  64. foreach my $course_info (@$courses) {
  65. if ($course_info->{'category'} ne 'Aamupala') {
  66. $cur_food = $course_info->{'title_fi'};
  67. $cur_food .= " (" . $course_info->{'properties'} . ")" if $course_info->{'properties'};
  68. }
  69. sodexo_finish_food();
  70. }
  71. sodexo_finish_day();
  72. }
  73. }
  74. push @restaurants, [ $title, $open_hours, $week, [ @week_foods ], \@info ];
  75. $count++;
  76. }
  77. return @restaurants;
  78. }
  79. 1;