File Coverage

blib/lib/WWW/Hanako.pm
Criterion Covered Total %
statement 69 77 89.6
branch 4 8 50.0
condition 3 7 42.8
subroutine 12 12 100.0
pod 3 3 100.0
total 91 107 85.0


line stmt bran cond sub pod time code
1             package WWW::Hanako;
2              
3 1     1   52744 use 5.010000;
  1         4  
  1         43  
4 1     1   6 use strict;
  1         3  
  1         39  
5 1     1   5 use warnings;
  1         2  
  1         40  
6              
7 1     1   6 use Carp;
  1         1  
  1         293  
8 1     1   6541 use LWP::UserAgent;
  1         131774  
  1         32  
9 1     1   2137 use WWW::Mechanize;
  1         290091  
  1         41  
10 1     1   3427 use Web::Scraper;
  1         80241  
  1         7  
11 1     1   4153 use Data::Dumper;
  1         7258  
  1         1043  
12              
13             our @ISA = qw();
14              
15             our $VERSION = '0.05';
16              
17             my $HANAKO_BASE_URI = 'http://kafun.taiki.go.jp/';
18              
19             my %WIND_DIRECTION = (
20             "\x{6b20}\x{6e2c}" => -1,# missing observation
21             "\x{9759}\x{7a4f}" => 0, # light wind
22             "\x{5317}\x{5317}\x{6771}" => 1, # north northeast
23             "\x{5317}\x{6771}" => 2, # northeast
24             "\x{6771}\x{5317}\x{6771}" => 3, # east northeast
25             "\x{6771}" => 4, # east
26             "\x{6771}\x{5357}\x{6771}" => 5, # east southeast
27             "\x{5357}\x{6771}" => 6, # southeast
28             "\x{5357}\x{5357}\x{6771}" => 7, # south southeast
29             "\x{5357}" => 8, # south
30             "\x{5357}\x{5357}\x{897f}" => 9, # south southwest
31             "\x{5357}\x{897f}" => 10,# southwest
32             "\x{897f}\x{5357}\x{897f}" => 11,# west southwest
33             "\x{897f}" => 12,# west
34             "\x{897f}\x{5317}\x{897f}" => 13,# west northwest
35             "\x{5317}\x{897f}" => 14,# northwest
36             "\x{5317}\x{5317}\x{897f}" => 15,# north northwest
37             "\x{5317}" => 16,# north
38             );
39              
40             # Preloaded methods go here.
41              
42             sub new
43             {
44 1     1 1 11 my $proto = shift;
45 1   33     8 my $class = ref($proto) || $proto;
46 1   50     23 my $self = {
      50        
47             area => $_{area} || 0,
48             mst => $_{mst} || 0,
49             mech => WWW::Mechanize->new(agent=>"Net-Hanako/$VERSION"),
50             debug => 0,
51             };
52 1         59944 %$self = (%$self, @_);
53 1         6 bless($self, $class);
54 1         5 return $self;
55             }
56              
57             sub today
58             {
59 1     1 1 3 my $self = shift;
60 1         2 my @ret;
61 1         9 my $path = sprintf("Hyou0.aspx?MstCode=%d&AreaCode=%02d",
62             $self->{'mst'}, $self->{'area'});
63              
64 1         13 my $uri = $HANAKO_BASE_URI . $path;
65 1         3 my $uri2 = $HANAKO_BASE_URI . "Hyou2.aspx";
66              
67 1 50       6 if($self->{debug}){
68 0         0 print "uri: $uri\n";
69 0         0 print "uri2: $uri2\n";
70             }
71              
72             # set cookie
73 1         6 $self->{mech}->get($uri);
74             # get cookie
75 1         661490 $self->{mech}->get($uri2);
76              
77 1 50       2870154 if(!$self->{mech}->success()){
78 0         0 carp("error");
79 0         0 return;
80             }
81              
82 1 50       26 if($self->{mech}->status() != 200){
83 0         0 carp("response code: " . $self->{mech}->status());
84 0         0 return;
85             }
86              
87 1         14 my $content = $self->{mech}->content();
88              
89             my $scraper = scraper {
90 1     1   228912 process '//table[@class="bun"]/tr[2]/td/font', 'head[]' => 'TEXT';
91 1         114508 process '//table[@class="bun"]/tr/td[1]/font', 'hour[]' => 'TEXT';
92 1         101319 process '//table[@class="bun"]/tr/td[2]/font', 'pollen[]' => 'TEXT';
93 1         95089 process '//table[@class="bun"]/tr/td[3]/font', 'wd[]' => 'TEXT';
94 1         95376 process '//table[@class="bun"]/tr/td[4]/font', 'ws[]' => 'TEXT';
95 1         125504 process '//table[@class="bun"]/tr/td[5]/font', 'temp[]' => 'TEXT';
96 1         104354 process '//table[@class="bun"]/tr/td[6]/font', 'prec[]' => 'TEXT';
97 1         108931 process '//table[@class="bun"]/tr/td[7]/font', 'prec_bool[]' => 'TEXT';
98 1         47 };
99 1         25 my $res = $scraper->scrape($content);
100 1 50       112325 if(!$res->{"hour"}){
101 0         0 carp("scrape error");
102 0         0 return;
103             }
104              
105 1         4 my $num = @{$res->{"hour"}};
  1         3  
106 1         2 for my $i (0 .. @{$res->{"hour"}} - 3){
  1         6  
107 24         44 my $hour = $res->{'hour'}->[$i+2];
108 24         147 $hour =~ s/^(\d*)\x{6642}/$1/;
109 24         56 my $pollen = $res->{'pollen'}->[$i+2];
110 24         51 my $wd = $WIND_DIRECTION{$res->{'wd'}->[$i+2]};
111 24         43 my $ws = $res->{'ws'}->[$i+2];
112 24         41 my $temp = $res->{'temp'}->[$i];
113 24         100 my $prec = $res->{'prec'}->[$i];
114              
115 24         136 push(@ret, {hour => $hour,
116             pollen => $pollen,
117             wd => $wd,
118             ws => $ws,
119             temp => $temp,
120             prec => $prec});
121             }
122 1         48 return @ret;
123             }
124              
125             sub now
126             {
127 1     1 1 655 my $self = shift;
128 1         5 my @today = $self->today();
129 1         4 my $ret = pop(@today);
130 1         23 return $ret;
131             }
132              
133             1;
134             __END__