use vars qw(@day_names); my @short_day_names = ( "ma", "ti", "ke", "to", "pe", "la", "su" ); #my $pky_url = "http://www.pky.fi/lounaslistat"; my $pky_url = "http://www.pikante.fi/index.php/Pikante/lounaaksi-t%C3%A4n%C3%A4%C3%A4n.html"; my @restaurant_info = ( [ "(TAYS) Finn-Medi", "$pky_url", "all", "middle" ], [ "(TAYS) Café Olive", "$pky_url", "all", "middle" ], [ "(TAYS) Ellipsi", "$pky_url", "all", "middle" ] ); my ($parse_func, $day_id, $week); my (@cur_day_foods, @week_foods); my ($show_next_week, $content_title); sub utf8_to_8859 { $_ = shift; s/ //g; s/é/é/g; s/ä/ä/g; s/ö/ö/g; s/Ä/Ä/g; s/Ö/Ö/g; return $_; } sub pky_finish_day { push @week_foods, [@cur_day_foods]; @cur_day_foods = (); $day_id = $day_id + 1; } sub pky_parse_more_food { my $token = shift; if ($token->[0] eq 'T') { my $text = utf8_to_8859($token->[1]); foreach my $text (split("/", $text)) { push @cur_day_foods, $text if ($text ne ""); } } elsif ($token->[0] eq 'E') { if ($token->[1] eq 'tr') { pky_finish_day(); $parse_func = \&pky_parse_day_td; } } } sub pky_parse_day_td { my $token = shift; if ($token->[0] eq 'E') { if ($token->[1] eq 'td') { $parse_func = \&pky_parse_more_food; } elsif ($token->[1] eq 'table') { $parse_func = \&pky_parse_to_week; } } elsif ($token->[0] eq 'T') { my $text = $token->[1]; my $i = 0; foreach my $day (@short_day_names) { if ($text =~ /$day$/i) { while ($day_id < $i) { push @week_foods, [ ]; $day_id++; } last; } $i++; } } } sub pky_parse_to_monday { my $token = shift; if ($token->[0] eq 'S' && $token->[1] eq 'tr') { $parse_func = \&pky_parse_day_td; } elsif ($token->[0] eq 'E' && $token->[1] eq 'table') { $parse_func = \&pky_parse_to_week; } } sub want_second_week { my $week = shift; return 1 if $day_id == 0; # week didn't start from beginning my @l = localtime; my $this_week = strftime("%V", @l); return $week == $this_week || ($l[6] == 6 && ($week%52)+1 == $this_week); } sub pky_parse_to_eof { } sub pky_parse_to_week { my $token = shift; if ($token->[0] eq 'T' && $token->[1] =~ /Viikko (\d+)/) { my $parsed_week = $1; # earlier version could have shown two tables for two weeks # sometimes. but the new version? dunno yet.. #if ($week == 0 || $show_next_week) { if ($week == 0) { $week = $parsed_week; $day_id = 0; @cur_day_foods = (); @week_foods = (); $parse_func = \&pky_parse_to_monday; } else { $parse_func = \&pky_parse_to_eof; } } } sub pky_parse_to_title { my $token = shift; if ($token->[0] eq 'T') { my $text = utf8_to_8859($token->[1]); if ($text =~ /$content_title.*lounasaika/) { $parse_func = \&pky_parse_to_week; } } } sub parse_pky { my ($fname, $info_ref) = @_; my $p = HTML::TokeParser->new($fname) or die("Can't open file $fname"); my $title = @{$info_ref}[0]; $week = 0; $content_title = $title; $content_title =~ s/^\(TAYS\) //; $parse_func = \&pky_parse_to_title; while (my $token = $p->get_token) { &$parse_func($token); } return [ $title, "", $week, [ @week_foods ], $info_ref ]; } sub get_pky_restaurants { my $use_old; ($use_old, $show_next_week) = @_; my $temp_fname = "pky.temp.html"; if (!-f $temp_fname || !$use_old) { system("wget -q --timeout=10 -O $temp_fname.tmp '$pky_url' && mv $temp_fname.tmp $temp_fname"); } my @restaurants = (); if (-f $temp_fname) { my $count = 0; foreach my $i (@restaurant_info) { my @info = @{$i}; push @restaurants, parse_pky($temp_fname, \@info); $count++; } } return @restaurants; } 1;