File Coverage

blib/lib/WWW/YahooJapan/Baseball/Parser.pm
Criterion Covered Total %
statement 40 48 83.3
branch 9 12 75.0
condition 2 3 66.6
subroutine 4 6 66.6
pod 0 3 0.0
total 55 72 76.3


line stmt bran cond sub pod time code
1             package WWW::YahooJapan::Baseball::Parser;
2              
3 2     2   43279 use Web::Scraper;
  2         170946  
  2         16  
4              
5             sub parse_games_page {
6 0     0 0 0 my $date = shift;
7 0         0 my $league = shift;
8 0         0 my %params = @_;
9             my $day_scraper = scraper {
10 0     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';
11 0         0 };
12 0 0       0 my $res = $day_scraper->scrape(defined $params{html} ? ($params{html}, $params{uri}) : $params{uri});
13 0         0 @{$res->{uris}};
  0         0  
14             }
15              
16             sub parse_game_player_row {
17 31     31 0 42 my $cells = shift;
18 31         38 my $stats = {};
19              
20 31         230 my %position_table = (
21             '投' => 'p',
22             '捕' => 'c',
23             '一' => '1b',
24             '二' => '2b',
25             '三' => '3b',
26             '遊' => 'ss',
27             '左' => 'lf',
28             '中' => 'cf',
29             '右' => 'rf',
30             '指' => 'dh',
31             '打' => 'ph',
32             '走' => 'pr'
33             );
34 31         42 my @positions = ();
35 31         53 my $pos_rep = shift @$cells;
36 31 100       119 if ($pos_rep =~ /^\((.*)\)$/) {
37 20         23 my $i = 0;
38 20         63 for my $p (split //, $1) {
39             push(@positions, {
40 72 100       265 position => $position_table{$p},
41             is_starting => $i++ > 0 ? 0 : 1
42             });
43             }
44             }
45             else {
46 11         28 for my $p (split //, $pos_rep) {
47             push(@positions, {
48 33         90 position => $position_table{$p},
49             is_starting => 0
50             });
51             }
52             }
53 31         75 $stats->{positions} = \@positions;
54              
55 31         47 my $player_name = shift @$cells;
56              
57 31         88 my @indexes = qw(avg ab r h rbi k bbhbp shsf sb e hr);
58 31         44 for my $i (@indexes) {
59 341         623 $stats->{$i} = shift @$cells;
60             }
61 31         38 my $bi = 1;
62 31         53 $stats->{innings} = {};
63 31         47 for my $bat (@$cells) {
64 279 100       802 $stats->{innings}->{$bi++} = $bat ne '' ? [$bat] : [];
65             }
66 31         160 return $player_name, $stats;
67             }
68              
69             sub parse_game_stats_page {
70 1     1 0 5725 my %params = @_;
71             my $stats_scraper = scraper {
72             process '//*[@id="st_batth" or @id="st_battv"]//tr', 'lines[]' => scraper {
73 35         1131445 process '//td', 'cells[]' => 'TEXT';
74 35         164643 process_first '//a[contains(@href, "/npb/player")]', 'player_uri' => '@href';
75 1     1   603578 };
76 1         33 };
77 1 50       59 my $res = $stats_scraper->scrape(defined $params{html} ? ($params{html}, $params{uri}) : $params{uri});
78 1         21172 my @players = ();
79 1         3 for my $line (@{$res->{lines}}) {
  1         4  
80 35         63 my $cells = $line->{cells};
81 35 100 66     290 unless ($cells and $line->{player_uri}) {
82 4         7 next;
83             }
84 31         282 my ($player_name, $player_stats) = WWW::YahooJapan::Baseball::Parser::parse_game_player_row($cells);
85             $player_stats->{player} = {
86             name => $player_name,
87             uri => $line->{player_uri},
88             $line->{player_uri}->query_form
89 31         110 };
90 31         1067 push(@players, $player_stats);
91             }
92 1         54 @players;
93             }
94              
95             1;