-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspider.pl
executable file
·182 lines (168 loc) · 6.41 KB
/
spider.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#! /usr/bin/perl
use LWP;
use Time::Local;
use XML::Simple;
$boxparser= new XML::Simple(ForceArray => 1, KeepRoot => 1, KeyAttr => 'boxscore');
my $browser = LWP::UserAgent->new;
$baseurl = "http://gd2.mlb.com/components/game/mlb";
$outputdir = "./games";
$scbaseurl = "http://statsapi.mlb.com/api/v1/game/";
sub extractDate($) {
# extracts and formats date from a time stamp
($t) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= localtime($t);
$mon += 1;
$year += 1900;
$mon = (length($mon) == 1) ? "0$mon" : $mon;
$mday = (length($mday) == 1) ? "0$mday" : $mday;
return ($mon, $mday, $year);
}
sub verifyDir($) {
# verifies that a directory exists,
# creates the directory if the directory doesn't
my ($d) = @_;
if (-e $d) {
die "$d not a directory\n" unless (-d $outputdir);
} else {
die "could not create $d: $!\n" unless (mkdir $d);
}
}
sub getWithRetry($) {
my ($arg) = @_;
my ($fileurl) = $arg;
# print "\t\t\tGET : $fileurl\n";
my ($response) = $browser->get($fileurl);
unless ($response->is_success) {
$fileurl = $arg;
print "RETRY\t\t\tGET : $fileurl\n";
$response = $browser->get($fileurl);
}
unless ($response->is_success) {
$fileurl = $arg;
print "RETRY\t\t\tGET : $fileurl\n";
$response = $browser->get($fileurl);
}
unless ($response->is_success) {
die "FAIL\t\t\tGET : $fileurl\n", $response->status_line, "\n"
}
return ($response);
}
# Evaluate stdin start date, if valid, use as start, otherwise end
$input = $ARGV[0];
$start = "";
if ($input =~ m/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]([2][0][01][1-9])$/){
$dd = $1 + 0;
$mm = $2 - 1;
$yyyy = $3 - 1900;
$start = timelocal(0,0,0,$dd,$mm,$yyyy);
}
else {
print "Invalid date\n" and die;
}
# get all important files from MLB.com, 4/2/07 through yesterday
# 0,0,0,day,month-1,1900+year
# $start = timelocal(0,0,0,3,3,116);
($mon, $mday, $year) = extractDate($start);
print "starting at $mon/$mday/$year\n";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
#$now = timelocal(0,0,0,$mday - 0,$mon,$year);
$now = timelocal(0,0,0,$mday,$mon,$year);
#$now = timelocal(0,0,0,8,5,115);
($mon, $mday, $year) = extractDate($now);
print "ending at $mon/$mday/$year\n";
verifyDir($outputdir);
for ($t = $start; $t < $now; $t += 60*60*24) {
($mon, $mday, $year) = extractDate($t);
print "processing $mon/$mday/$year\n";
verifyDir("$outputdir/year_$year");
verifyDir("$outputdir/year_$year/month_$mon");
verifyDir("$outputdir/year_$year/month_$mon/day_$mday");
$dayurl = "$baseurl/year_$year/month_$mon/day_$mday/";
print "\t$dayurl\n";
$response = getWithRetry($dayurl);
$html = $response->content;
my @games = ();
while($html =~ m/<a href=\"(gid_\w+\/)\"/g ) {
# while($html =~ m/<a href=\"(gid_\w+_(sur|per|pes|afe|afw)win_1\/)\"/g) {
# while($html =~ m/<a href=\"(gid_\w+_(kca|tex|sdn|sea)\/)\"/g ) {
push @games, $1;
}
foreach $game (@games) {
$gamedir = "$outputdir/year_$year/month_$mon/day_$mday/$game";
if (-e $gamedir) {
# already fetched info on this game
print "\t\tskipping game: $game\n";
} else {
print "\t\tfetching game: $game\n";
verifyDir($gamedir);
$gameurl = "$dayurl/$game";
$response = getWithRetry($gameurl);
$gamehtml = $response->content;
if($gamehtml =~ m/<a href=\"boxscore\.xml\"/ ) {
$boxurl = "$dayurl/$game/boxscore.xml";
$response = getWithRetry($boxurl);
$boxhtml = $response->content;
open BOX, ">$gamedir/boxscore.xml"
or die "could not open file $gamedir/boxscore.xml: $|\n";
print BOX $boxhtml;
close BOX;
$box = $boxparser->XMLin("$gamedir/boxscore.xml");
$game_pk = $box->{boxscore}->[0]->{game_pk};
$scurl = "$scbaseurl/$game_pk/feed/color";
$scresponse = getWithRetry($scurl);
$schtml = $scresponse->content;
open SC, ">$gamedir/color.json"
or die "could not open file $gamedir/color.json: $|\n";
print SC $schtml;
close SC;
} else {
print "warning: no xml box score for $game\n";
}
if($gamehtml =~ m/<a href=\"game\.xml\"/ ) {
$gameurl = "$dayurl/$game/game.xml";
$response = getWithRetry($gameurl);
$infohtml = $response->content;
open GAME, ">$gamedir/game.xml"
or die "could not open file $gamedir/game.xml: $|\n";
print GAME $infohtml;
close GAME;
} else {
print "warning: no xml game file for $game\n";
}
if($gamehtml =~ m/<a href=\"players\.xml\"/ ) {
$plyrurl = "$dayurl/$game/players.xml";
$response = getWithRetry($plyrurl);
$plyrhtml = $response->content;
open PLYRS, ">$gamedir/players.xml"
or die "could not open file $gamedir/players.xml: $|\n";
print PLYRS $plyrhtml;
close PLYRS;
} else {
print "warning: no player list for $game\n";
}
if($gamehtml =~ m/<a href=\"inning\/\"/ ) {
$inningdir = "$gamedir/inning";
verifyDir($inningdir);
$inningurl = "$dayurl/$game/inning/";
$response = getWithRetry($inningurl);
$inninghtml = $response->content;
my @files = ();
while($inninghtml =~ m/<a href=\"(inning_.*)\"/g ) {
push @files, $1;
}
foreach $file (@files) {
print "\t\t\tinning file: $file\n";
$fileurl = "$inningurl/$file";
$response = getWithRetry($fileurl);
$filehtml = $response->content;
open FILE, ">$inningdir/$file"
or die "could not open file $inningdir/$file: $|\n";
print FILE $filehtml;
close FILE;
}
}
sleep(1); # be at least somewhat polite; one game per second
}
}
}