PNA.fi koodi

sodexo.pl 2.4KB

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