Sabermetrics/League/CPBL.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 122 SCAR 47.0
CC
10
Cov
0%
extract_record
42.0
extract_defense
20.0
get_table_in_html
6.0
CRAP 110.0
1 package Baseball::Sabermetrics::League::CPBL;
2  
3 use WWW::Mechanize;
4 use HTML::TableExtract;
5 use Encode qw/ encode decode /;
6  
7 use strict;
8  
9 sub new
10 {
11 my $league = {};
12 my $teams = $league->{teams} = {};
13  
14 $teams->{bulls} = { code => 'B02', name => 'bulls', company => "興農" };
15 $teams->{cobras} = { code => 'G01', name => 'cobras', company => "誠泰" };
16 $teams->{elephants} = { code => 'E01', name => 'elephants', company => "兄弟" };
17 $teams->{whales} = { code => 'W01', name => 'whales', company => "中信" };
18 $teams->{lions} = { code => 'L01', name => 'lions', company => "統一" };
19 $teams->{bears} = { code => 'A02', name => 'bears', company => "La New" };
20  
21 extract_score($league, "http://www.cpbl.com.tw/Score/FightScore.aspx");
22  
23 for my $team (values %$teams) {
24 print "Fetching records of team $team->{name}\n";
25  
26 my $code = $team->{code};
27  
28 extract_record(
29 $team,
30 "http://www.cpbl.com.tw/teams/Team_Pitcher.aspx?Tno=$code",
31 [qw/ name game gs _ _ win lose tie sv bs hld _ cg sho /]);
32 extract_record(
33 $team,
34 "http://www.cpbl.com.tw/teams/Team_Pitcher.aspx?Tno=$code&page=2",
35 [qw/ name ip p_pa np h_allowed hr_allowed sh_allowed sf_allowed p_bb p_ibb hb p_so wp bk r_allowed er /]);
36  
37 extract_record(
38 $team,
39 "http://www.cpbl.com.tw/teams/Team_Hitter.aspx?Tno=$code",
40 [qw/ name game pa ab rbi r h 1b 2b 3b hr tb dp /]);
41 extract_record(
42 $team,
43 "http://www.cpbl.com.tw/teams/Team_Hitter.aspx?Tno=$code&page=2",
44 [qw/ name sh sf 4ball ibb bb so sb cs /]);
45  
46 # finn stards for fielding innings
47 extract_defense(
48 $team,
49 "http://www.cpbl.com.tw/teams/Team_defend.aspx?Tno=$code",
50 [qw/ name finn tc po a e f_dp ppo tp pb c_cs c_sb _ /]);
51 }
52  
53 $league;
54 }
55  
56 sub get_table_in_html
57 {
58 my ($url, $attribs) = @_;
59  
60 my $mech = WWW::Mechanize->new();
61 my $te = HTML::TableExtract->new(attribs => $attribs);
62 $te->parse($mech->get($url)->content);
63  
64 my @tables = $te->tables;
65 die "No table is found" unless @tables;
66 return shift @tables;
67 }
68  
69 sub extract_score
70 {
71 my ($league, $url) = @_;
72 my $table = get_table_in_html($url, { class => 'Report_Table_score' });
73 my (undef, undef, @teams) = $table->rows;
74 for my $team (@teams) {
75 my ($name, $game, $score) = @$team;
76 $name =~ s/\d\.//;
77 my ($t) = grep { $_->{company} eq $name } values %{$league->{teams}};
78 $t->{game} = $game;
79 ($t->{win}, $t->{lose}, $t->{tie}) = ($score =~ /(\d+)勝(\d+)和(\d+)敗/);
80 }
81 }
82  
83 sub extract_record
84 {
85 my ($team, $url, $cols) = @_;
86  
87 my $table = get_table_in_html($url, { class => 'Report_Table' });
88 die "Table not found" unless $table;
89  
90 my (undef, $header, @players) = $table->rows;
91
92 for my $p_row (@players) {
93 my @rows = @$p_row;
94 my $name = shift @rows;
95 $name =~ s/\s//g;
96  
97 $team->{players}->{$name} = {}
98 unless exists $team->{players}->{$name};
99  
100 my $p = $team->{players}->{$name};
101 for (1..@$cols - 1) {
102 my $t = $cols->[$_];
103 if ($t eq '_') {
104 shift @rows;
105 next;
106 }
107 $p->{$t} = shift @rows;
108 }
109  
110 $p->{ip} = int($p->{ip}) + ($p->{ip} - int $p->{ip}) * 10 / 3
111 if exists $p->{ip};
112  
113 if (exists $p->{'4ball'}) {
114 #$p->{hbp} = $p->{bb} - $p->{ibb} - $p->{'4ball'};
115 # XXX I'm not sure whether ibb is counted in bb in cpbl.com.tw
116 $p->{hbp} = $p->{bb} - $p->{'4ball'};
117 delete $p->{'4ball'};
118 }
119 else {
120 # XXX I'm not sure whether ibb is counted in bb in cpbl.com.tw
121 #$p->{p_bb} = $p->{p_bb} + $p->{p_ibb};
122 }
123 }
124 }
125  
126 sub extract_defense
127 {
128 my ($team, $url, $cols) = @_;
129  
130 my $table = get_table_in_html($url, { id => 'DefendDG' });
131 die "Table not found" unless $table;
132  
133 my ($header, @players) = $table->rows;
134
135 for my $p_row (@players) {
136 my @rows = @$p_row;
137 my $name = shift @rows;
138 $name =~ s/\s//g;
139  
140 $team->{players}->{$name} = {}
141 unless exists $team->{players}->{$name};
142  
143 my $p = $team->{players}->{$name};
144 $p->{name} = $name;
145 for (1..@$cols - 1) {
146 my $t = $cols->[$_];
147 if ($t eq '_') {
148 shift @rows;
149 next;
150 }
151 $p->{$t} = shift @rows;
152 }
153 }
154 }
155  
156 1;