PNA.fi koodi

amica.pl 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. print $cur_food . "\n";
  35. push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne "");
  36. $cur_food = "";
  37. }
  38. push @week_foods, [@cur_day_foods];
  39. @cur_day_foods = ();
  40. }
  41. return [ $title, "", $week, [ @week_foods ], $info_ref ];
  42. }
  43. sub get_amica_restaurant {
  44. my $use_old = shift;
  45. my $count = 0;
  46. my @restaurants = ();
  47. my $weekDay = `date +%w`;
  48. chomp($weekDay);
  49. my $first_day = $weekDay == 1
  50. ? `date --date="today" +%Y-%m-%d`
  51. : `date --date="last monday" +%Y-%m-%d`;
  52. chomp($first_day);
  53. my $last_day = $weekDay == 0
  54. ? `date --date="today" +%Y-%m-%d`
  55. : `date --date="next sunday" +%Y-%m-%d`;
  56. chomp($last_day);
  57. foreach my $i (@restaurant_info) {
  58. my @info = @{$i};
  59. my $temp_fname = "amica$count.temp.html";
  60. my $url = "${info[4]}&firstDay=$first_day&lastDay=$last_day&language=fi";
  61. if (!-f $temp_fname || !$use_old) {
  62. system("wget -q --timeout=10 -O $temp_fname.tmp '$url' && mv $temp_fname.tmp $temp_fname") if ($url ne "");
  63. }
  64. if (-f $temp_fname) {
  65. $info[4] = $url;
  66. push @restaurants, parse_amica($temp_fname, \@info);
  67. }
  68. $count++;
  69. }
  70. return @restaurants;
  71. }
  72. 1;