-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrent.js
More file actions
executable file
·105 lines (89 loc) · 2.32 KB
/
current.js
File metadata and controls
executable file
·105 lines (89 loc) · 2.32 KB
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
#!/usr/bin/env node
var downcache = require("downcache"),
request = require("request"),
fs = require("fs"),
cheerio = require("cheerio");
var URL = "http://www.baseball-reference.com/teams/$team/$year-schedule-scores.shtml";
function getTeam(team, year, callback) {
request(URL.replace("$team", team).replace("$year", 2014), function(err, resp, body) {
var $ = cheerio.load(body);
callback(get_csv_output($, "team_schedule"));
});
}
function getTeams() {
var teams = [],
count = 30;
downcache("http://www.baseball-reference.com/teams/", function(err, resp, body) {
var $ = cheerio.load(body.replace(/ \. franchise_names/g, ".franchise_names"));
$("#active").find(".franchise_names a").each(function(i, v) {
var team = $(v).text(),
team_id = $(v).attr("href").split("/")[2];
teams.push({
ids: {
"baseball-reference": team_id
},
name: team,
twitter: "",
teamname: ""
});
});
teams = teams.sort(function(a, b) { return a.name > b.name ? 1 : -1; });
fs.writeFileSync("./json/names.json", JSON.stringify(teams, null, 2));
});
}
function getSeason() {
var teams = {},
count = 30;
require("./json/names_twitter.json").forEach(function(team) {
console.log(team);
getTeam(team.ids["baseball-reference"], 2014, function(data) {
teams[team.ids["baseball-reference"]] = {
info: team,
record: data
};
if (data.length == 0) {
console.error(team);
}
count -= 1;
console.log(team.name, data.slice(-1));
if (count == 0) {
fs.writeFileSync("./json/current.json", JSON.stringify(teams));
}
});
})
}
var removals = [
'Streak',
'Attendance',
'D/N',
'Time',
'Win',
'Loss',
'Save',
'Inn'
];
function get_csv_output($, tableid, do_drop_over_headers, blank_colspans) {
var tableref = $("#" + tableid),
headers = [],
data = [];
tableref.find("thead th").each(function(i, v) {
headers.push($(v).text());
});
tableref.find("tbody tr").each(function(i, v) {
var datum = {};
$(v).find("td").each(function(ii, vv) {
if (headers[ii].replace(/\s/g, "") !== "") {
datum[headers[ii]] = $(vv).text();
}
});
if (datum.RA) {
data.push(datum["W-L"].split("-").map(function(d) {
return parseInt(d, 10);
}));
}
});
return data;
}
//getTeam("PHI", 2014);
//getTeams();
getSeason();