PNA.fi koodi

sodexo.pl 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. sodexo_parse_to_food($token);
  33. } elsif ($token->[0] eq 'S' && $token->[1] eq 'th') {
  34. sodexo_finish_day();
  35. $parse_func = \&sodexo_parse_to_food;
  36. }
  37. }
  38. sub sodexo_finish_food {
  39. if ($cur_text ne "") {
  40. push @cur_day_foods, "$cur_title ($cur_text)";
  41. } else {
  42. push @cur_day_foods, "$cur_title";
  43. }
  44. $parse_func = \&sodexo_parse_after_food;
  45. }
  46. sub sodexo_parse_allergy {
  47. my $token = shift;
  48. if ($token->[0] eq 'T' && !($token->[1] =~ /\s*\/\s*/)) {
  49. my $text = $token->[1];
  50. $text =~ s/^\s+//;
  51. $text =~ s/\s+$//;
  52. $cur_text .= ", " if $cur_text ne "";
  53. $cur_text .= $text;
  54. } elsif ($token->[0] eq 'E' && $token->[1] eq 'td') {
  55. sodexo_finish_food();
  56. }
  57. }
  58. sub sodexo_parse_to_allergy {
  59. my $token = shift;
  60. if ($token->[0] eq 'S' && $token->[1] eq 'td') {
  61. my %attrs = %{$token->[2]};
  62. if ($attrs{'class'} eq 'food-properties') {
  63. $parse_func = \&sodexo_parse_allergy;
  64. }
  65. }
  66. }
  67. sub sodexo_parse_food {
  68. my $token = shift;
  69. if ($token->[0] eq 'T' && $token->[1] =~ /\S/) {
  70. $cur_title .= utf8_to_8859($token->[1]);
  71. $parse_func = \&sodexo_parse_to_allergy;
  72. }
  73. }
  74. sub sodexo_parse_type {
  75. my $token = shift;
  76. if ($token->[0] eq 'T' && $token->[1] =~ /\S/) {
  77. if ($token->[1] !~ /^ $/) {
  78. $cur_title = utf8_to_8859($token->[1]) . ": ";
  79. }
  80. $parse_func = \&sodexo_parse_food;
  81. }
  82. }
  83. sub sodexo_parse_to_food {
  84. my $token = shift;
  85. # Allow list to skip days, ie. Helatorstai
  86. if ($token->[0] eq 'T') {
  87. for (my $day = $day_id; $day <= 6; ++$day) {
  88. if ($token->[1] eq $day_names[$day]) {
  89. while ($day > $day_id) {
  90. sodexo_finish_day();
  91. $day = $day - 1;
  92. }
  93. break;
  94. }
  95. }
  96. }
  97. if ($token->[0] eq 'S' && $token->[1] eq "td") {
  98. my %attrs = %{$token->[2]};
  99. if ($attrs{'class'} eq "food-type") {
  100. $cur_title = '';
  101. $cur_text = '';
  102. $parse_func = \&sodexo_parse_type;
  103. }
  104. if ($attrs{'class'} eq "food-desc") {
  105. $cur_title = '';
  106. $cur_text = '';
  107. $parse_func = \&sodexo_parse_food;
  108. }
  109. }
  110. }
  111. sub sodexo_parse_week {
  112. my $token = shift;
  113. if ($token->[0] eq 'T' && $token->[1] =~ /Viikko (\d+)/) {
  114. $week = $1;
  115. $parse_func = \&sodexo_parse_to_food;
  116. }
  117. }
  118. sub sodexo_parse_to_week {
  119. my $token = shift;
  120. if ($token->[0] eq 'S' && $token->[1] eq 'div') {
  121. my %attrs = %{$token->[2]};
  122. if ($attrs{'class'} eq "list-date") {
  123. $parse_func = \&sodexo_parse_week;
  124. }
  125. }
  126. }
  127. sub parse_sodexo {
  128. my ($fname, $info_ref) = @_;
  129. my $title = @{$info_ref}[0];
  130. my $url = @{$info_ref}[1];
  131. my $align = @{$info_ref}[2];
  132. my $p = HTML::TokeParser->new($fname) or die("Can't open file $fname");
  133. $week = "";
  134. $day_id = 0;
  135. @cur_day_foods = ();
  136. @week_foods = ();
  137. my $week_day = `date +%w`;
  138. for (my $i = 1; $i < $week_day; ++$i) {
  139. sodexo_finish_day();
  140. }
  141. $parse_func = \&sodexo_parse_to_week;
  142. while (my $token = $p->get_token) {
  143. &$parse_func($token);
  144. }
  145. return [ $title, "", $week, [ @week_foods ], [ $title, $url, "M", $align ] ];
  146. }
  147. sub get_sodexo_restaurants {
  148. my $use_old = shift;
  149. my $count = 0;
  150. my @restaurants = ();
  151. foreach my $i (@restaurant_info) {
  152. my @info = @{$i};
  153. my $temp_fname = "sodexo$count.temp.html";
  154. my $url = $info[1];
  155. if (!-f $temp_fname || !$use_old) {
  156. system("wget -q --timeout=10 -O $temp_fname.tmp '$url' && mv $temp_fname.tmp $temp_fname");
  157. return undef if (!-f $temp_fname);
  158. }
  159. push @restaurants, parse_sodexo($temp_fname, \@info);
  160. ++$count;
  161. }
  162. return @restaurants;
  163. }
  164. 1;