| 1 | package WWW::YahooJapan::Baseball::Parser; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Web::Scraper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | sub parse_games_page { | ||||||||||||||||||||||||||||
| 8 | 0 | ▶ | my $date = shift; | ||||||||||||||||||||||||||
|
Subroutine
parse_games_page: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 9 | 0 | my $league = shift; | |||||||||||||||||||||||||||
| 10 | 0 | my %params = @_; | |||||||||||||||||||||||||||
| 11 | my $day_scraper = scraper { | ||||||||||||||||||||||||||||
| 12 | 0 | ▶ | process_first '//*[@id="gm_sch"]/div[contains(@class, "' . $league . '")]', 'league_name' => 'TEXT'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 13 | 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 | }; | |||||||||||||||||||||||||||
| 15 | 0 | ▶ | my $res = $day_scraper->scrape(defined $params{html} ? ($params{html}, $params{uri}) : $params{uri}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 0 | my %league_names = ( | |||||||||||||||||||||||||||
| 17 | NpbPl => 'パ・リーグ', | ||||||||||||||||||||||||||||
| 18 | NpbCl => 'セ・リーグ', | ||||||||||||||||||||||||||||
| 19 | NpbIl => '交流戦' | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | if ($res->{league_name} eq $league_names{$league}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | return @{$res->{uris}}; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | else { | ||||||||||||||||||||||||||||
| 25 | 0 | return (); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub parse_game_player_row { | ||||||||||||||||||||||||||||
| 30 | 31 | ▶ | my $cells = shift; | ||||||||||||||||||||||||||
|
Subroutine
parse_game_player_row: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 31 | my $stats = {}; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 31 | 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 | my @positions = (); | |||||||||||||||||||||||||||
| 48 | 31 | my $pos_rep = shift @$cells; | |||||||||||||||||||||||||||
| 49 | 31 | ▶ | if ($pos_rep =~ /^\((.*)\)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 20 | my $i = 0; | |||||||||||||||||||||||||||
| 51 | 20 | for my $p (split //, $1) { | |||||||||||||||||||||||||||
| 52 | push(@positions, { | ||||||||||||||||||||||||||||
| 53 | 72 | ▶ | position => $position_table{$p}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | is_starting => $i++ > 0 ? 0 : 1 | ||||||||||||||||||||||||||||
| 55 | }); | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||
| 59 | 11 | for my $p (split //, $pos_rep) { | |||||||||||||||||||||||||||
| 60 | push(@positions, { | ||||||||||||||||||||||||||||
| 61 | 33 | position => $position_table{$p}, | |||||||||||||||||||||||||||
| 62 | is_starting => 0 | ||||||||||||||||||||||||||||
| 63 | }); | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 31 | $stats->{positions} = \@positions; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 31 | my $player_name = shift @$cells; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 31 | my @indexes = qw(avg ab r h rbi k bbhbp shsf sb e hr); | |||||||||||||||||||||||||||
| 71 | 31 | for my $i (@indexes) { | |||||||||||||||||||||||||||
| 72 | 341 | $stats->{$i} = shift @$cells; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 31 | my $bi = 1; | |||||||||||||||||||||||||||
| 75 | 31 | $stats->{innings} = {}; | |||||||||||||||||||||||||||
| 76 | 31 | for my $bat (@$cells) { | |||||||||||||||||||||||||||
| 77 | 279 | ▶ | $stats->{innings}->{$bi++} = $bat ne '' ? [$bat] : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 31 | return $player_name, $stats; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub parse_game_stats_page { | ||||||||||||||||||||||||||||
| 83 | 1 | ▶ | my %params = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_game_stats_page: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | my $stats_scraper = scraper { | ||||||||||||||||||||||||||||
| 85 | process '//*[@id="st_batth" or @id="st_battv"]//tr', 'lines[]' => scraper { | ||||||||||||||||||||||||||||
| 86 | 35 | ▶ | process '//td', 'cells[]' => 'TEXT'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 35 | process_first '//a[contains(@href, "/npb/player")]', 'player_uri' => '@href'; | |||||||||||||||||||||||||||
| 88 | 1 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | 1 | }; | |||||||||||||||||||||||||||
| 90 | 1 | ▶ | my $res = $stats_scraper->scrape(defined $params{html} ? ($params{html}, $params{uri}) : $params{uri}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | my @players = (); | |||||||||||||||||||||||||||
| 92 | 1 | for my $line (@{$res->{lines}}) { | |||||||||||||||||||||||||||
| 93 | 35 | my $cells = $line->{cells}; | |||||||||||||||||||||||||||
| 94 | 35 | ▶ | unless ($cells and $line->{player_uri}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cells and $line->{"player_uri"}
MC/DC100%2/2
$cells and $line->{"player_uri"}
$cells
$line->{"player_uri"}
Truth table100%3/3
A and B
A= $cellsB= $line->{"player_uri"}
| |||||||||||||||||||||||||||||
| 95 | 4 | next; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 31 | my ($player_name, $player_stats) = WWW::YahooJapan::Baseball::Parser::parse_game_player_row($cells); | |||||||||||||||||||||||||||
| 98 | $player_stats->{player} = { | ||||||||||||||||||||||||||||
| 99 | name => $player_name, | ||||||||||||||||||||||||||||
| 100 | uri => $line->{player_uri}, | ||||||||||||||||||||||||||||
| 101 | $line->{player_uri}->query_form | ||||||||||||||||||||||||||||
| 102 | 31 | }; | |||||||||||||||||||||||||||
| 103 | 31 | push(@players, $player_stats); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 1 | @players; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | 1; | |||||||||||||||||||||||||||