forked from mayscopeland/priceguide_php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inseason.php
352 lines (295 loc) · 10.2 KB
/
inseason.php
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
// This is a script that builds CSV files for current season-to-date batting and pitching stats.
// It is meant to run daily (i.e. via cron) during the regular season.
//
// To get current season stats, this script scrapes Yahoo's baseball stats pages using cURL. It
// then converts Yahoo's player IDs (from Stats, Inc.) to the player IDs used by the Price Guide,
// (MLBAM IDs). It saves the batter and pitcher CSVs, overwriting the existing files (typically
// the previous day's year-to-date stats).
//
// During the course of the year, rookies will invariably show up with IDs from Stats Inc. that
// I don't have mapped to an MLBAM ID. This requires periodically checking the output of this
// script to see which players need their player IDs added to IdConversion.csv.
$year = 2013;
$TR_MATCH = "|<tr height=\"20\" class=\"ysprow.*?>(.*?)</tr>|";
$TD_MATCH = "|<td.*?>(.*?)</td>|";
$A_MATCH = "<a href=\"\/mlb\/players\/(.*?)\">";
$ch = curl_init("http://sports.yahoo.com/mlb/stats/byposition?pos=LF,CF,RF,1B,2B,SS,3B,C,DH&conference=MLB&year=season_" . $year . "&qualified=0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$html = curl_exec($ch);
//$html = file_get_contents("http://sports.yahoo.com/mlb/stats/byposition?pos=LF,CF,RF,1B,2B,SS,3B,C,DH&conference=MLB&year=season_" . $year . "&qualified=0");
$html = cleanupHtml($html);
$players = array();
preg_match_all($TR_MATCH, $html, $playerMatches);
for ($i = 0; $i < count($playerMatches[1]); $i++)
{
preg_match_all($TD_MATCH, $playerMatches[1][$i], $statMatches);
preg_match($A_MATCH, $statMatches[1][1], $idMatch);
$players[$i]["mlbamID"] = $idMatch[1];
$players[$i]["playerName"] = strip_tags($statMatches[1][1]);
$players[$i]["team"] = strip_tags($statMatches[1][2]);
$players[$i]["league"] = getLeague($players[$i]["team"]);
$players[$i]["G"] = intval(str_replace(" ", "", $statMatches[1][3]));
$players[$i]["AB"] = intval(str_replace(" ", "", $statMatches[1][5]));
$players[$i]["R"] = intval(str_replace(" ", "", $statMatches[1][7]));
$players[$i]["H"] = intval(str_replace(" ", "", $statMatches[1][9]));
$players[$i]["2B"] = intval(str_replace(" ", "", $statMatches[1][11]));
$players[$i]["3B"] = intval(str_replace(" ", "", $statMatches[1][13]));
$players[$i]["HR"] = intval(str_replace(" ", "", $statMatches[1][15]));
$players[$i]["RBI"] = intval(str_replace(" ", "", $statMatches[1][17]));
$players[$i]["BB"] = intval(str_replace(" ", "", $statMatches[1][19]));
$players[$i]["SO"] = intval(str_replace(" ", "", $statMatches[1][21]));
$players[$i]["SB"] = intval(str_replace(" ", "", $statMatches[1][23]));
$players[$i]["CS"] = intval(str_replace(" ", "", $statMatches[1][25]));
}
loadPlayerIds($players);
loadGamesByPosition($players, "Hitters");
loadDefaultPositions($players);
outputPlayers($players, "stats/" . $year . "Batting.csv");
$ch = curl_init("http://sports.yahoo.com/mlb/stats/byposition?pos=SP,RP&conference=MLB&year=season_" . $year . "&qualified=0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$html = curl_exec($ch);
//$html = file_get_contents("http://sports.yahoo.com/mlb/stats/byposition?pos=SP,RP&conference=MLB&year=season_" . $year . "&qualified=0");
$html = cleanupHtml($html);
$players = array();
preg_match_all($TR_MATCH, $html, $playerMatches);
for ($i = 0; $i < count($playerMatches[1]); $i++)
{
preg_match_all($TD_MATCH, $playerMatches[1][$i], $statMatches);
preg_match($A_MATCH, $statMatches[1][1], $idMatch);
$players[$i]["mlbamID"] = $idMatch[1];
$players[$i]["playerName"] = strip_tags($statMatches[1][1]);
$players[$i]["team"] = strip_tags($statMatches[1][2]);
$players[$i]["league"] = getLeague($players[$i]["team"]);
$players[$i]["G"] = intval(str_replace(" ", "", $statMatches[1][3]));
$players[$i]["GS"] = intval(str_replace(" ", "", $statMatches[1][5]));
$players[$i]["W"] = intval(str_replace(" ", "", $statMatches[1][7]));
$players[$i]["L"] = intval(str_replace(" ", "", $statMatches[1][9]));
$players[$i]["S"] = intval(str_replace(" ", "", $statMatches[1][11]));
$players[$i]["BS"] = intval(str_replace(" ", "", $statMatches[1][13]));
$players[$i]["HLD"] = intval(str_replace(" ", "", $statMatches[1][15]));
$players[$i]["CG"] = intval(str_replace(" ", "", $statMatches[1][17]));
$players[$i]["SHO"] = intval(str_replace(" ", "", $statMatches[1][19]));
$ip = str_replace(" ", "", $statMatches[1][21]);
$ip = str_replace(".1", ".3", $ip);
$ip = str_replace(".2", ".7", $ip);
$players[$i]["IP"] = number_format($ip, 1, ".", "");
$players[$i]["H"] = intval(str_replace(" ", "", $statMatches[1][23]));
$players[$i]["R"] = intval(str_replace(" ", "", $statMatches[1][25]));
$players[$i]["ER"] = intval(str_replace(" ", "", $statMatches[1][27]));
$players[$i]["HR"] = intval(str_replace(" ", "", $statMatches[1][29]));
$players[$i]["BB"] = intval(str_replace(" ", "", $statMatches[1][31]));
$players[$i]["K"] = intval(str_replace(" ", "", $statMatches[1][33]));
}
loadPlayerIds($players);
loadGamesByPosition($players, "Pitchers");
loadDefaultPositions($players);
outputPlayers($players, "stats/" . $year . "Pitching.csv");
// Delete the cache
foreach (glob("cache/*.cache") as $file)
{
unlink($file);
}
function cleanupHtml($html)
{
// Strip line breaks and remove accents
$html = str_replace("\n", "", $html);
$html = str_replace("\r", "", $html);
$html = str_replace("á", "a", $html);
$html = str_replace("é", "e", $html);
$html = str_replace("í", "i", $html);
$html = str_replace("ó", "o", $html);
$html = str_replace("ú", "u", $html);
$html = str_replace("ñ", "n", $html);
$html = str_replace("Á", "A", $html);
$html = str_replace("É", "E", $html);
/*
$html = str_replace("�", "a", $html);
$html = str_replace("�", "e", $html);
$html = str_replace("�", "i", $html);
$html = str_replace("�", "o", $html);
$html = str_replace("�", "u", $html);
$html = str_replace("�", "n", $html);
$html = str_replace("�", "A", $html);
*/
return $html;
}
function outputPlayers($players, $fileName)
{
$outstream = fopen($fileName, 'w');
if (!$players[0]["defaultPos"])
{
$players[0]["defaultPos"] = "";
}
foreach ($players[0] as $col => $value)
{
fwrite($outstream, $col . ",");
}
fwrite($outstream, "\n");
foreach ($players as $player)
{
foreach ($player as $col)
{
fwrite($outstream, $col . ",");
}
fwrite($outstream, "\n");
}
fclose($outstream);
}
function getLeague($team)
{
global $year;
if ($team == "HOU")
{
if ($year < 2013)
{
return "NL";
}
else
{
return "AL";
}
}
switch ($team)
{
case "ARI":
case "ATL":
case "CHC":
case "CIN":
case "COL":
case "FLA":
case "LAD":
case "MIL":
case "MIA":
case "NYM":
case "PHI":
case "PIT":
case "SD":
case "SF":
case "STL":
case "WAS":
return "NL";
break;
case "BAL":
case "BOS":
case "CLE":
case "CWS":
case "DET":
case "KC":
case "LAA":
case "MIN":
case "NYY":
case "OAK":
case "SEA":
case "TB":
case "TEX":
case "TOR":
return "AL";
break;
default:
return "";
}
}
function loadPlayerIds(&$players)
{
$handle = fopen("stats/IdConversion.csv", "r");
if ($handle)
{
while (!feof($handle))
{
$teamLine = fgetcsv($handle);
foreach ($players as &$player)
{
if (($teamLine[3]) && ($teamLine[3] == $player["mlbamID"]))
{
$player["mlbamID"] = $teamLine[0];
break;
}
}
}
fclose($handle);
}
}
function loadGamesByPosition(&$players, $playerType)
{
global $year;
$handle = fopen("stats/GamesByPos" . ($year - 1) . ".csv", "r");
if ($handle)
{
if (!feof($handle))
{
$columns = fgetcsv($handle);
}
foreach ($players as &$player)
{
if ($playerType == "Hitters")
{
$player["G_C"] = 0;
$player["G_1B"] = 0;
$player["G_2B"] = 0;
$player["G_3B"] = 0;
$player["G_SS"] = 0;
$player["G_LF"] = 0;
$player["G_CF"] = 0;
$player["G_RF"] = 0;
$player["G_OF"] = 0;
}
else
{
$player["G_SP"] = 0;
$player["G_RP"] = 0;
}
}
while (!feof($handle))
{
$playerGames = fgetcsv($handle);
foreach ($players as &$player)
{
if (($playerGames) && ($playerGames[0] == $player["mlbamID"]))
{
if ($playerType == "Hitters")
{
$player["G_C"] = $playerGames[6];
$player["G_1B"] = $playerGames[7];
$player["G_2B"] = $playerGames[8];
$player["G_3B"] = $playerGames[9];
$player["G_SS"] = $playerGames[10];
$player["G_LF"] = $playerGames[11];
$player["G_CF"] = $playerGames[12];
$player["G_RF"] = $playerGames[13];
$player["G_OF"] = $playerGames[14];
}
else
{
$player["G_SP"] = $playerGames[5];
$player["G_RP"] = $playerGames[4] - $playerGames[5];
}
}
}
}
fclose($handle);
}
}
function loadDefaultPositions(&$players)
{
global $year;
$handle = fopen("stats/Teams" . $year . ".csv", "r");
if ($handle)
{
while (!feof($handle))
{
$teamLine = fgetcsv($handle);
foreach ($players as &$player)
{
if (($teamLine[1]) && ($teamLine[1] == $player["mlbamID"]))
{
$player["defaultPos"] = $teamLine[4];
}
}
}
fclose($handle);
}
}
?>