forked from jaw187/mlbprobablepitchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (60 loc) · 1.87 KB
/
index.js
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
'use strict';
const Xray = require('x-ray');
const internals = {};
module.exports = internals.Probables = {};
internals.Probables.get = function (day, callback) {
const url = `http://mlb.mlb.com/news/probable_pitchers/index.jsp?c_id=mlb&date=${day}`;
const scope = 'div#mc';
const selector = {
pitchers: ['div.pitcher@pid'],
names: ['div.pitcher h5 a'],
throws: ['div.pitcher h5 span'],
teams: ['div.pitcher@tid'],
games: ['div.pitcher@gid'],
startTimes: ['div.pitcher@local_time'],
easternTimes: ['div.pitcher@eastern_time'],
timezones: ['div.pitcher@local_time_zone']
};
const x = Xray();
x(url, scope, selector)((err, result) => {
if (err) {
return callback(err);
}
const matchups = internals.convertResult(result);
return callback(null, matchups);
});
};
internals.convertResult = function (result) {
const matchups = [];
const pitchers = result.pitchers;
const teams = result.teams;
const names = result.names;
const throws = result.throws;
for (let i = 0; i < pitchers.length; i += 2) {
const j = i + 1;
const matchup = {
id: result.games[i],
startTime: result.startTimes[i],
easternTime: result.easternTimes[i],
timezone: result.timezones[i],
teams: {
away: teams[i],
home: teams[j]
},
pitchers: {
away: {
id: pitchers[i],
name: names[i],
throws: throws[i]
},
home: {
id: pitchers[j],
name: names[j],
throws: throws[j]
}
}
};
matchups.push(matchup);
}
return matchups;
};