File Coverage

blib/lib/WWW/YahooJapan/Baseball/Parser.pm
Criterion Covered Total %
statement 46 58 79.3
branch 9 14 64.2
condition 2 3 66.6
subroutine 5 7 71.4
pod 0 3 0.0
total 62 85 72.9


line stmt bran cond sub pod time code
1             package WWW::YahooJapan::Baseball::Parser;
2              
3 2     2   35031 use utf8;
  2         11  
  2         13  
4              
5 2     2   1494 use Web::Scraper;
  2         190206  
  2         15  
6              
7             sub parse_games_page {
8 0     0 0 0 my $date = shift;
9 0         0 my $league = shift;
10 0         0 my %params = @_;
11             my $day_scraper = scraper {
12 0     0   0 process_first '//*[@id="gm_sch"]/div[contains(@class, "' . $league . '")]', 'league_name' => 'TEXT';
13 0         0 process '//*[@id="gm_sch"]/div[contains(@class, "' . $league . '")]/following-sibling::div[position() <= 2 and contains(@class, "NpbScoreBg")]//a[starts-with(@href, "/npb/game/' . $date . '") and not(contains(@href, "/top"))]', 'uris[]' => '@href';
14 0         0 };
15 0 0       0 my $res = $day_scraper->scrape(defined $params{html} ? ($params{html}, $params{uri}) : $params{uri});
16 0         0 my %league_names = (
17             NpbPl => 'パ・リーグ',
18             NpbCl => 'セ・リーグ',
19             NpbIl => '交流戦'
20             );
21 0 0       0 if ($res->{league_name} eq $league_names{$league}) {
22 0         0 return @{$res->{uris}};
  0         0  
23             }
24             else {
25 0         0 return ();
26             }
27             }
28              
29             sub parse_game_player_row {
30 31     31 0 46 my $cells = shift;
31 31         44 my $stats = {};
32              
33 31         211 my %position_table = (
34             '投' => 'p',
35             '捕' => 'c',
36             '一' => '1b',
37             '二' => '2b',
38             '三' => '3b',
39             '遊' => 'ss',
40             '左' => 'lf',
41             '中' => 'cf',
42             '右' => 'rf',
43             '指' => 'dh',
44             '打' => 'ph',
45             '走' => 'pr'
46             );
47 31         46 my @positions = ();
48 31         56 my $pos_rep = shift @$cells;
49 31 100       118 if ($pos_rep =~ /^\((.*)\)$/) {
50 20         29 my $i = 0;
51 20         69 for my $p (split //, $1) {
52             push(@positions, {
53 72 100       267 position => $position_table{$p},
54             is_starting => $i++ > 0 ? 0 : 1
55             });
56             }
57             }
58             else {
59 11         29 for my $p (split //, $pos_rep) {
60             push(@positions, {
61 33         114 position => $position_table{$p},
62             is_starting => 0
63             });
64             }
65             }
66 31         78 $stats->{positions} = \@positions;
67              
68 31         63 my $player_name = shift @$cells;
69              
70 31         52 $stats->{statuses} = {};
71 31         49 for my $status (qw(ba)) {
72 31         166 $stats->{statuses}->{$status} = shift @$cells;
73             }
74              
75 31         54 $stats->{results} = {};
76 31         48 for my $result (qw(ab r h rbi k bb_hbp sh_sf sb e hr)) {
77 310         738 $stats->{results}->{$result} = 0 + shift @$cells;
78             }
79              
80 31         43 my $abi = 1;
81 31         47 $stats->{at_bats_by_innings} = {};
82 31         58 for my $bat (@$cells) {
83 279 100       823 $stats->{at_bats_by_innings}->{$abi++} = $bat ne '' ? [$bat] : [];
84             }
85 31         149 return $player_name, $stats;
86             }
87              
88             sub parse_game_stats_page {
89 1     1 0 8111 my %params = @_;
90             my $stats_scraper = scraper {
91             process '//*[@id="st_batth" or @id="st_battv"]//tr', 'lines[]' => scraper {
92 35         1128087 process '//td', 'cells[]' => 'TEXT';
93 35         166377 process_first '//a[contains(@href, "/npb/player")]', 'player_uri' => '@href';
94 1     1   610390 };
95 1         57 };
96 1 50       51 my $res = $stats_scraper->scrape(defined $params{html} ? ($params{html}, $params{uri}) : $params{uri});
97 1         22049 my @players = ();
98 1         3 for my $line (@{$res->{lines}}) {
  1         4  
99 35         58 my $cells = $line->{cells};
100 35 100 66     245 unless ($cells and $line->{player_uri}) {
101 4         10 next;
102             }
103 31         236 my ($player_name, $player_stats) = WWW::YahooJapan::Baseball::Parser::parse_game_player_row($cells);
104             $player_stats->{player} = {
105             name => $player_name,
106             uri => $line->{player_uri},
107             $line->{player_uri}->query_form
108 31         107 };
109 31         1149 push(@players, $player_stats);
110             }
111 1         52 @players;
112             }
113              
114             1;