Sabermetrics/League/CPBL.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 212 CC 18 SCAR 31.6
CC
18
Cov
0%
extract_fielding
56.0
new
30.0
get_table_in_html
20.0
CRAP 23.6
1 package Baseball::Sabermetrics::League::CPBL;
2  
3 use LWP::UserAgent;
4 use HTML::TableExtract;
5 use Encode qw/ encode decode /;
6 use Data::Serializer;
7 use constant CACHEFILE => 'cachefile';
8  
9 use strict;
10  
11 sub new
12 {
13 my ($class, %config) = @_;
14 my $data = Data::Serializer->new();
15  
16 if (-f CACHEFILE && !$config{nocache}) {
17 my $mtime = (stat CACHEFILE)[9];
18 # just a heuristic for speeding up
19 if ($config{usecache} || time - $mtime < 6 * 3600) {
20 print STDERR "cache hit\n";
21 return $data->retrieve(CACHEFILE);
22 }
23 }
24  
25 my $league = {};
26 my $teams = $league->{teams} = {};
27  
28 $teams->{bulls} = { code => 'B02', name => 'bulls', company => "興農" };
29 $teams->{cobras} = { code => 'G01', name => 'cobras', company => "誠泰" };
30 $teams->{elephants} = { code => 'E01', name => 'elephants', company => "兄弟" };
31 $teams->{whales} = { code => 'W01', name => 'whales', company => "中信" };
32 $teams->{lions} = { code => 'L01', name => 'lions', company => "統一" };
33 $teams->{bears} = { code => 'A02', name => 'bears', company => "La New" };
34  
35 extract_score($league, "http://www.cpbl.com.tw/Score/FightScore.aspx");
36  
37 for my $team (values %$teams) {
38 print "Fetching records of team $team->{name}\n";
39  
40 my $code = $team->{code};
41 $team->{players} = {};
42  
43 extract_record(
44 $team,
45 "http://www.cpbl.com.tw/teams/Team_Pitcher.aspx?Tno=$code",
46 [qw/ name p_game gs _ _ win lose tie sv bs hld _ cg sho /]);
47 extract_record(
48 $team,
49 "http://www.cpbl.com.tw/teams/Team_Pitcher.aspx?Tno=$code&page=2",
50 [qw/ name ip p_pa np h_allowed hr_allowed sh_allowed sf_allowed p_bb p_ibb hb p_so wp bk ra er /]);
51  
52 extract_record(
53 $team,
54 "http://www.cpbl.com.tw/teams/Team_Hitter.aspx?Tno=$code",
55 [qw/ name game pa ab rbi r h 1b 2b 3b hr tb dp /]);
56 extract_record(
57 $team,
58 "http://www.cpbl.com.tw/teams/Team_Hitter.aspx?Tno=$code&page=2",
59 [qw/ name sh sf 4ball ibb bb so sb cs /]);
60  
61 # fgame stards for fielding games
62 extract_fielding($team, "http://www.cpbl.com.tw/teams/Team_defend.aspx?Tno=$code");
63  
64  
65 # fix something up
66 for my $p (values %{$team->{players}}) {
67 $p->{ip} = int($p->{ip}) + ($p->{ip} - int $p->{ip}) * 10 / 3
68 if exists $p->{ip};
69  
70 if (exists $p->{'4ball'}) {
71 #$p->{hbp} = $p->{bb} - $p->{ibb} - $p->{'4ball'};
72 # XXX I'm not sure whether ibb is counted in bb in cpbl.com.tw
73 $p->{hbp} = $p->{bb} - $p->{'4ball'};
74 delete $p->{'4ball'};
75 }
76 else {
77 # XXX I'm not sure whether ibb is counted in bb in cpbl.com.tw
78 #$p->{p_bb} = $p->{p_bb} + $p->{p_ibb};
79 }
80 }
81 }
82  
83 $data->store($league, CACHEFILE);
84  
85 $league;
86 }
87  
88 sub get_content
89 {
90 my $url = shift;
91 my $page = LWP::UserAgent->new();
92 my $ua = $page->get($url);
93 if (not $ua->is_success) {
94 die "failed to fetch url $url";
95 }
96 return $ua->content;
97 }
98  
99 sub get_table_in_html
100 {
101 my ($page, $attribs) = @_;
102  
103 my $t;
104 if (exists $attribs->{cpb2_choose_table}) {
105 $t = $attribs->{cpb2_choose_table};
106 delete $attribs->{cpb2_choose_table};
107 }
108 else {
109 $t = 0;
110 }
111  
112 my $te = HTML::TableExtract->new(attribs => $attribs);
113 $te->parse($page =~ /^http/ ? get_content($page) : $page);
114  
115 my @tables = $te->tables;
116 die "No table is found" unless @tables;
117 return $tables[$t];
118 }
119  
120 sub extract_table_record
121 {
122 my ($page, $attribs, $hash, $n_dummy, $keys) = @_;
123 my $table = get_table_in_html($page, $attribs);
124 my @rows = $table->rows;
125  
126 shift @rows for (1 .. $n_dummy);
127  
128 my $i = 0;
129 while ($keys->[$i] eq '_') {
130 ++$i;
131 }
132  
133 for my $p_row (@rows) {
134 my @col = @$p_row;
135 $col[$i] =~ s/\s//g unless $keys->[$i] eq 'company';
136 $hash->{$col[$i]} = {} unless exists $hash->{$col[$i]};
137 my $h = $hash->{$col[$i]};
138  
139 for ($i + 1 ..@$keys - 1) {
140 my $key = $keys->[$_];
141 if ($key eq '_') {
142 next;
143 }
144 $h->{$key} = $col[$_];
145 }
146 }
147 }
148  
149 sub extract_score
150 {
151 my ($league, $url) = @_;
152 my $hash = {};
153 extract_table_record($url, { class => 'Report_Table_score', cpb2_choose_table => 2 }, $hash, 2,
154 [qw/ company game score /]);
155 for my $key (keys %$hash) {
156 my $name = $key;
157 $name =~ s/\d\.//;
158 my $team = $hash->{$key};
159 my ($t) = grep { $_->{company} eq $name } values %{$league->{teams}};
160 $t->{game} = $team->{game};
161 ($t->{win}, $t->{tie}, $t->{lose}) = ($team->{score} =~ /(\d+)勝(\d+)和(\d+)敗/);
162 }
163 }
164  
165 sub extract_record
166 {
167 my ($team, $url, $cols) = @_;
168 extract_table_record($url, { class => 'Report_Table' }, $team->{players}, 2, $cols);
169 }
170  
171 sub fix_cpbls_bug
172 {
173 my ($team, $ids) = @_;
174 my $content = get_content("http://www.cpbl.com.tw/teams/Team_Hitter.aspx?Tno=$team->{code}");
175 my %hash = @$ids;
176 my %hash2 = $content =~ m!href="\.\./personal_Rec/pbat_personal\.aspx\?Pno=([^"]+)">([^<]+)<!gs;
177 my %hash3 = (%hash, %hash2);
178 @$ids = %hash3;
179 }
180  
181 sub extract_fielding
182 {
183 my ($team, $url) = @_;
184  
185 my $content = get_content($url);
186 my @ids = $content =~ m!href="\.\./personal_Rec/pbat_personal\.aspx\?Pno=([^"]+)">([^<]+)<!gs;
187 fix_cpbls_bug($team, \@ids);
188  
189 while (@ids) {
190 my $id = shift @ids;
191 my $name = shift @ids;
192 $name =~ s/\s//g;
193 my $p = $team->{players}->{$name};
194 my $year = 2006;
195 my $hash = {};
196 extract_table_record("http://www.cpbl.com.tw/personal_Rec/pbat_personal.aspx?Pno=$id", { class => 'Report_Table_pdf' }, $hash, 1,
197 [qw/ year _ fgame tc po a e f_dp tp pb c_cs c_sb _ /]);
198 my $h = $hash->{$year - 1989};
199 next unless $h;
200 while (my ($key, $value) = each %$h) {
201 $p->{$key} = $value;
202 }
203  
204 $hash = {};
205 extract_table_record("http://www.cpbl.com.tw/personal_Rec/pdf_detail.aspx?pbyear=$year&Pno=$id", { class => 'Report_Table_pdf' }, $hash, 1,
206 [qw/ _ pos fgame tc po a e f_dp tp pb c_cs c_sb _ /]);
207  
208 while (my ($key, $value) = each %$hash) {
209 my %posname = ( '投手' => 'p', '捕手' => 'c', '一壘' => 'b1', '二壘' => 'b2', '三壘' => 'b3', '游擊' => 'ss', '左外野' => 'lf', '中外野' => 'cf', '右外野' => 'rf' );
210 $p->{fielding}->{$posname{$key}} = $value;
211 }
212  
213 $content = get_content("http://www.cpbl.com.tw/personal_Rec/pbat_personal.aspx?Pno=$id");
214 $content =~ m!投 打:<span id="lbl_bpcustom">(.+)</span>!;
215 my $type = $1;
216 $p->{bio} = {};
217 if ($type =~ /開弓/) {
218 $p->{bio}->{bats} = "switch";
219 $p->{bio}->{throws} = $type =~ /左投/ ? 'left' : 'right';
220 }
221 elsif ($type =~ /(.+)打(.+)投/) {
222 $p->{bio}->{bats} = $1 eq "左" ? 'left' : 'right';
223 $p->{bio}->{throws} = $2 eq "左" ? 'left' : 'right';
224 }
225 else {
226 die "unrecognized format: $type";
227 }
228 }
229 }
230  
231 1;