PNA.fi koodi

sodexo.pl 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. use vars qw(@day_names);
  2. my @restaurant_info = (
  3. #[ "(TaY) Sodexo Linna", "http://www.sodexo.fi/fi-FI/linna/lounas/", "right" ],
  4. #[ "(TTY) Sodexo Erkkeri", "http://www.sodexo.fi/fi-FI/erkkeri/lounas/", "left" ]
  5. [ "(TaY) Sodexo Linna", "http://www.sodexo.fi/linna", "right" ],
  6. [ "(TTY) Sodexo Erkkeri", "http://www.sodexo.fi/erkkeri", "left" ]
  7. );
  8. my ($cur_text, $cur_title, $parse_func, $day_id, $week);
  9. my (@cur_day_foods, @week_foods);
  10. sub sodexo_finish_day {
  11. push @week_foods, [@cur_day_foods];
  12. @cur_day_foods = ();
  13. $day_id = $day_id + 1;
  14. }
  15. sub utf8_to_8859 {
  16. $_ = shift;
  17. s/ä/ä/g;
  18. s/ö/ö/g;
  19. s/Ä/Ä/g;
  20. s/Ö/Ö/g;
  21. return $_;
  22. }
  23. sub sodexo_parse_finish {
  24. }
  25. sub sodexo_parse_after_food {
  26. my $token = shift;
  27. if ($token->[0] eq 'E' && $token->[1] eq 'tbody') {
  28. sodexo_finish_day();
  29. $parse_func = \&sodexo_parse_finish;
  30. } elsif ($token->[0] eq 'S' && $token->[1] eq 'td') {
  31. $parse_func = \&sodexo_parse_to_food;
  32. } elsif ($token->[0] eq 'S' && $token->[1] eq 'th') {
  33. sodexo_finish_day();
  34. $parse_func = \&sodexo_parse_to_food;
  35. }
  36. }
  37. sub sodexo_finish_food {
  38. if ($cur_text ne "") {
  39. push @cur_day_foods, "$cur_title ($cur_text)";
  40. } else {
  41. push @cur_day_foods, "$cur_title";
  42. }
  43. $parse_func = \&sodexo_parse_after_food;
  44. }
  45. sub sodexo_parse_allergy {
  46. my $token = shift;
  47. if ($token->[0] eq 'T' && !($token->[1] =~ /\s*\/\s*/)) {
  48. my $text = $token->[1];
  49. $text =~ s/^\s+//;
  50. $text =~ s/\s+$//;
  51. $cur_text .= ", " if $cur_text ne "";
  52. $cur_text .= $text;
  53. } elsif ($token->[0] eq 'E' && $token->[1] eq 'td') {
  54. sodexo_finish_food();
  55. }
  56. }
  57. sub sodexo_parse_to_allergy {
  58. my $token = shift;
  59. if ($token->[0] eq 'S' && $token->[1] eq 'td') {
  60. my %attrs = %{$token->[2]};
  61. if ($attrs{'class'} eq 'food-properties') {
  62. $parse_func = \&sodexo_parse_allergy;
  63. }
  64. }
  65. }
  66. sub sodexo_parse_food {
  67. my $token = shift;
  68. if ($token->[0] eq 'T' && $token->[1] =~ /\S/) {
  69. $cur_title = utf8_to_8859($token->[1]);
  70. $parse_func = \&sodexo_parse_to_allergy;
  71. }
  72. }
  73. sub sodexo_parse_to_food {
  74. my $token = shift;
  75. if ($token->[0] eq 'S' && $token->[1] eq "td") {
  76. my %attrs = %{$token->[2]};
  77. if ($attrs{'class'} eq "food-desc") {
  78. $cur_title = '';
  79. $cur_text = '';
  80. $parse_func = \&sodexo_parse_food;
  81. }
  82. }
  83. }
  84. sub sodexo_parse_week {
  85. my $token = shift;
  86. if ($token->[0] eq 'T' && $token->[1] =~ /Viikko (\d+)/) {
  87. $week = $1;
  88. $parse_func = \&sodexo_parse_to_food;
  89. }
  90. }
  91. sub sodexo_parse_to_week {
  92. my $token = shift;
  93. if ($token->[0] eq 'S' && $token->[1] eq 'div') {
  94. my %attrs = %{$token->[2]};
  95. if ($attrs{'class'} eq "list-date") {
  96. $parse_func = \&sodexo_parse_week;
  97. }
  98. }
  99. }
  100. sub parse_sodexo {
  101. my ($fname, $info_ref) = @_;
  102. my $title = @{$info_ref}[0];
  103. my $url = @{$info_ref}[1];
  104. my $align = @{$info_ref}[2];
  105. my $p = HTML::TokeParser->new($fname) or die("Can't open file $fname");
  106. $week = "";
  107. $day_id = 0;
  108. @cur_day_foods = ();
  109. @week_foods = ();
  110. my $week_day = `date +%w`;
  111. for (my $i = 1; $i < $week_day; ++$i) {
  112. sodexo_finish_day();
  113. }
  114. $parse_func = \&sodexo_parse_to_week;
  115. while (my $token = $p->get_token) {
  116. &$parse_func($token);
  117. }
  118. return [ $title, "", $week, [ @week_foods ], [ $title, $url, "M", $align ] ];
  119. }
  120. sub get_sodexo_restaurants {
  121. my $use_old = shift;
  122. my $count = 0;
  123. my @restaurants = ();
  124. foreach my $i (@restaurant_info) {
  125. my @info = @{$i};
  126. my $temp_fname = "sodexo$count.temp.html";
  127. my $url = $info[1];
  128. if (!-f $temp_fname || !$use_old) {
  129. system("wget -q --timeout=10 -O $temp_fname.tmp '$url' && mv $temp_fname.tmp $temp_fname");
  130. return undef if (!-f $temp_fname);
  131. }
  132. push @restaurants, parse_sodexo($temp_fname, \@info);
  133. }
  134. return @restaurants;
  135. }
  136. 1;