PNA.fi koodi

pky.pl 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. use vars qw(@day_names);
  2. my @short_day_names = ( "ma", "ti", "ke", "to", "pe", "la", "su" );
  3. #my $pky_url = "http://www.pky.fi/lounaslistat";
  4. my $pky_url = "http://www.pikante.fi/index.php/Pikante/lounaaksi-t%C3%A4n%C3%A4%C3%A4n.html";
  5. my @restaurant_info = (
  6. [ "(TAYS) Finn-Medi", "$pky_url", "all", "middle" ],
  7. [ "(TAYS) Café Olive", "$pky_url", "all", "middle" ],
  8. [ "(TAYS) Ellipsi", "$pky_url", "all", "middle" ]
  9. );
  10. my ($parse_func, $day_id, $week);
  11. my (@cur_day_foods, @week_foods);
  12. my ($show_next_week, $content_title);
  13. sub utf8_to_8859 {
  14. $_ = shift;
  15. s/ //g;
  16. s/é/é/g;
  17. s/ä/ä/g;
  18. s/ö/ö/g;
  19. s/Ä/Ä/g;
  20. s/Ö/Ö/g;
  21. return $_;
  22. }
  23. sub pky_finish_day {
  24. push @week_foods, [@cur_day_foods];
  25. @cur_day_foods = ();
  26. $day_id = $day_id + 1;
  27. }
  28. sub pky_parse_more_food {
  29. my $token = shift;
  30. if ($token->[0] eq 'T') {
  31. my $text = utf8_to_8859($token->[1]);
  32. foreach my $text (split("/", $text)) {
  33. push @cur_day_foods, $text if ($text ne "");
  34. }
  35. } elsif ($token->[0] eq 'E') {
  36. if ($token->[1] eq 'tr') {
  37. pky_finish_day();
  38. $parse_func = \&pky_parse_day_td;
  39. }
  40. }
  41. }
  42. sub pky_parse_day_td {
  43. my $token = shift;
  44. if ($token->[0] eq 'E') {
  45. if ($token->[1] eq 'td') {
  46. $parse_func = \&pky_parse_more_food;
  47. } elsif ($token->[1] eq 'table') {
  48. $parse_func = \&pky_parse_to_week;
  49. }
  50. } elsif ($token->[0] eq 'T') {
  51. my $text = $token->[1];
  52. my $i = 0;
  53. foreach my $day (@short_day_names) {
  54. if ($text =~ /$day$/i) {
  55. while ($day_id < $i) {
  56. push @week_foods, [ ];
  57. $day_id++;
  58. }
  59. last;
  60. }
  61. $i++;
  62. }
  63. }
  64. }
  65. sub pky_parse_to_monday {
  66. my $token = shift;
  67. if ($token->[0] eq 'S' && $token->[1] eq 'tr') {
  68. $parse_func = \&pky_parse_day_td;
  69. } elsif ($token->[0] eq 'E' && $token->[1] eq 'table') {
  70. $parse_func = \&pky_parse_to_week;
  71. }
  72. }
  73. sub want_second_week {
  74. my $week = shift;
  75. return 1 if $day_id == 0; # week didn't start from beginning
  76. my @l = localtime;
  77. my $this_week = strftime("%V", @l);
  78. return $week == $this_week || ($l[6] == 6 && ($week%52)+1 == $this_week);
  79. }
  80. sub pky_parse_to_eof {
  81. }
  82. sub pky_parse_to_week {
  83. my $token = shift;
  84. if ($token->[0] eq 'T' && $token->[1] =~ /Viikko (\d+)/) {
  85. my $parsed_week = $1;
  86. # earlier version could have shown two tables for two weeks
  87. # sometimes. but the new version? dunno yet..
  88. #if ($week == 0 || $show_next_week) {
  89. if ($week == 0) {
  90. $week = $parsed_week;
  91. $day_id = 0;
  92. @cur_day_foods = ();
  93. @week_foods = ();
  94. $parse_func = \&pky_parse_to_monday;
  95. } else {
  96. $parse_func = \&pky_parse_to_eof;
  97. }
  98. }
  99. }
  100. sub pky_parse_to_title {
  101. my $token = shift;
  102. if ($token->[0] eq 'T') {
  103. my $text = utf8_to_8859($token->[1]);
  104. if ($text =~ /$content_title.*lounasaika/) {
  105. $parse_func = \&pky_parse_to_week;
  106. }
  107. }
  108. }
  109. sub parse_pky {
  110. my ($fname, $info_ref) = @_;
  111. my $p = HTML::TokeParser->new($fname) or die("Can't open file $fname");
  112. my $title = @{$info_ref}[0];
  113. $week = 0;
  114. $content_title = $title;
  115. $content_title =~ s/^\(TAYS\) //;
  116. $parse_func = \&pky_parse_to_title;
  117. while (my $token = $p->get_token) {
  118. &$parse_func($token);
  119. }
  120. return [ $title, "", $week, [ @week_foods ], $info_ref ];
  121. }
  122. sub get_pky_restaurants {
  123. my $use_old;
  124. ($use_old, $show_next_week) = @_;
  125. my $temp_fname = "pky.temp.html";
  126. if (!-f $temp_fname || !$use_old) {
  127. system("wget -q --timeout=10 -O $temp_fname.tmp '$pky_url' && mv $temp_fname.tmp $temp_fname");
  128. }
  129. my @restaurants = ();
  130. if (-f $temp_fname) {
  131. my $count = 0;
  132. foreach my $i (@restaurant_info) {
  133. my @info = @{$i};
  134. push @restaurants, parse_pky($temp_fname, \@info);
  135. $count++;
  136. }
  137. }
  138. return @restaurants;
  139. }
  140. 1;