-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
99 lines (83 loc) · 3.23 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const Xray = require('x-ray');
class Probables {
static get(dateString, callback) {
let dateComponents = dateString.split('-');
if (dateComponents.length !== 3) {
dateComponents = dateString.split('/');
if (dateComponents.length !== 3) {
throw `Invalid date format. Use 'YYYY-MM-DD'`;
}
}
const year = dateComponents[0];
const month = dateComponents[1];
const day = dateComponents[2];
const url = `https://www.mlb.com/probable-pitchers/${year}-${month}-${day}`;
const scope = 'body';
const selector = {
pitchers: ['div.probable-pitchers__pitcher-name a@href'],
names: ['div.probable-pitchers__pitcher-name a'],
throws: ['div.probable-pitchers__pitcher-details span.probable-pitchers__pitcher-pitch-hand'],
teams: ['div.probable-pitchers__team-names span.probable-pitchers__team-name--away, div.probable-pitchers__team-names span.probable-pitchers__team-name--home'],
games: ['div.probable-pitchers__matchup@data-gamePk'],
startTimes: ['div.probable-pitchers__game-details div.probable-pitchers__game-date-time time@dateitme'],
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 = Probables.convertResult(result);
return callback(null, matchups);
});
}
static trimWhiteSpace(arr) {
return arr.map((elem) => {
return elem.replace(/\s/g, '');
});
}
static convertResult(result) {
const matchups = [];
const pitcherIDInURL = /.*(\d{6}).*/;
const pitcherIDs = result.pitchers.map((pitcher) => {
const matches = pitcherIDInURL.exec(pitcher);
if (matches.length !== 2) {
throw `Unexpected URL format while trying to extract pitcher ID: ${pitcher}`;
}
return matches[1];
});
const teams = Probables.trimWhiteSpace(result.teams);
const names = result.names;
const throws = Probables.trimWhiteSpace(result.throws);
for (let i = 0; i < pitcherIDs.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: pitcherIDs[i],
name: names[i],
throws: throws[i]
},
home: {
id: pitcherIDs[j],
name: names[j],
throws: throws[j]
}
}
};
matchups.push(matchup);
}
return matchups;
}
}
module.exports = Probables;