PNA.fi koodi

sodexo.pl 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if ($token->[0] eq 'S' && $token->[1] eq "td") {
  86. my %attrs = %{$token->[2]};
  87. if ($attrs{'class'} eq "food-type") {
  88. $cur_title = '';
  89. $cur_text = '';
  90. $parse_func = \&sodexo_parse_type;
  91. }
  92. if ($attrs{'class'} eq "food-desc") {
  93. $cur_title = '';
  94. $cur_text = '';
  95. $parse_func = \&sodexo_parse_food;
  96. }
  97. }
  98. }
  99. sub sodexo_parse_week {
  100. my $token = shift;
  101. if ($token->[0] eq 'T' && $token->[1] =~ /Viikko (\d+)/) {
  102. $week = $1;
  103. $parse_func = \&sodexo_parse_to_food;
  104. }
  105. }
  106. sub sodexo_parse_to_week {
  107. my $token = shift;
  108. if ($token->[0] eq 'S' && $token->[1] eq 'div') {
  109. my %attrs = %{$token->[2]};
  110. if ($attrs{'class'} eq "list-date") {
  111. $parse_func = \&sodexo_parse_week;
  112. }
  113. }
  114. }
  115. sub parse_sodexo {
  116. my ($fname, $info_ref) = @_;
  117. my $title = @{$info_ref}[0];
  118. my $url = @{$info_ref}[1];
  119. my $align = @{$info_ref}[2];
  120. my $p = HTML::TokeParser->new($fname) or die("Can't open file $fname");
  121. $week = "";
  122. $day_id = 0;
  123. @cur_day_foods = ();
  124. @week_foods = ();
  125. my $week_day = `date +%w`;
  126. for (my $i = 1; $i < $week_day; ++$i) {
  127. sodexo_finish_day();
  128. }
  129. $parse_func = \&sodexo_parse_to_week;
  130. while (my $token = $p->get_token) {
  131. &$parse_func($token);
  132. }
  133. return [ $title, "", $week, [ @week_foods ], [ $title, $url, "M", $align ] ];
  134. }
  135. sub get_sodexo_restaurants {
  136. my $use_old = shift;
  137. my $count = 0;
  138. my @restaurants = ();
  139. foreach my $i (@restaurant_info) {
  140. my @info = @{$i};
  141. my $temp_fname = "sodexo$count.temp.html";
  142. my $url = $info[1];
  143. if (!-f $temp_fname || !$use_old) {
  144. system("wget -q --timeout=10 -O $temp_fname.tmp '$url' && mv $temp_fname.tmp $temp_fname");
  145. return undef if (!-f $temp_fname);
  146. }
  147. push @restaurants, parse_sodexo($temp_fname, \@info);
  148. ++$count;
  149. }
  150. return @restaurants;
  151. }
  152. 1;