PNA.fi koodi

sodexo.pl 2.4KB

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