PNA.fi koodi

amica.pl 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. use vars qw(@day_names);
  2. use JSON;
  3. my $erkkeri_title = "(TTY) Amica Erkkeri";
  4. my @restaurant_info = (
  5. [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "http://www.amica.fi/modules/json/json/Index?costNumber=0815" ]
  6. );
  7. sub utf8_to_8859 {
  8. $_ = shift;
  9. s/ä/ä/g;
  10. s/ö/ö/g;
  11. s/Ä/Ä/g;
  12. s/Ö/Ö/g;
  13. return $_;
  14. }
  15. sub parse_amica {
  16. my ($fname, $info_ref) = @_;
  17. open(FILE, $fname);
  18. my $json = do { local $/; <FILE> };
  19. close(FILE);
  20. my $title = @{$info_ref}[0];
  21. my $week = `date +%V`;
  22. my @cur_day_foods = ();
  23. my @week_foods = ();
  24. my $data = from_json($json);
  25. my $MenusForDays = $data->{'MenusForDays'};
  26. foreach my $MenuForDay (@$MenusForDays) {
  27. my $SetMenus = $MenuForDay->{'SetMenus'};
  28. foreach my $SetMenu (@$SetMenus) {
  29. my $Components = $SetMenu->{'Components'};
  30. foreach my $Component (@$Components) {
  31. $cur_food .= "\n" if $cur_food ne "";
  32. $cur_food .= $Component;
  33. }
  34. push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne "");
  35. $cur_food = "";
  36. }
  37. push @week_foods, [@cur_day_foods];
  38. @cur_day_foods = ();
  39. }
  40. return [ $title, "", $week, [ @week_foods ], $info_ref ];
  41. }
  42. sub get_amica_restaurant {
  43. my $use_old = shift;
  44. my $count = 0;
  45. my @restaurants = ();
  46. my $weekDay = `date +%w`;
  47. chomp($weekDay);
  48. my $first_day = $weekDay == 1
  49. ? `date --date="today" +%Y-%m-%d`
  50. : `date --date="last monday" +%Y-%m-%d`;
  51. chomp($first_day);
  52. my $last_day = $weekDay == 0
  53. ? `date --date="today" +%Y-%m-%d`
  54. : `date --date="next sunday" +%Y-%m-%d`;
  55. chomp($last_day);
  56. foreach my $i (@restaurant_info) {
  57. my @info = @{$i};
  58. my $temp_fname = "amica$count.temp.html";
  59. my $url = "${info[4]}&firstDay=$first_day&lastDay=$last_day&language=fi";
  60. if (!-f $temp_fname || !$use_old) {
  61. system("wget -q --timeout=10 -O $temp_fname.tmp '$url' && mv $temp_fname.tmp $temp_fname") if ($url ne "");
  62. }
  63. if (-f $temp_fname) {
  64. $info[4] = $url;
  65. push @restaurants, parse_amica($temp_fname, \@info);
  66. }
  67. $count++;
  68. }
  69. return @restaurants;
  70. }
  71. 1;