-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
163 lines (144 loc) · 4.06 KB
/
lib.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
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
const AA = Array.from;
class ScrapeError extends Error {
constructor(msg) {
super();
this.name = `ScrapeError: ${msg}`;
}
}
const select = (selector, name = selector) => {
const el = document.querySelector(selector);
if (el === null) {
throw new ScrapeError(`${name} element not found`);
}
return el;
}
const selectAll = (selector, name = selector) => {
const els = document.querySelectorAll(selector);
if (els.length === 0) {
throw new ScrapeError(`${name} elements not found`);
}
return Array.from(els);
}
const toSeconds = (q, m, s) => {
if (q > 4) {
return (4 * 12 * 60) + (q - 5) * (5 * 60) + (4 - m) * 60 + (60 - s);
} else {
return (q - 1) * (12 * 60) + (11 - m) * 60 + (60 - s);
}
}
const toPrettyTime = time => {
let quarter = 1;
while (quarter <= 4 && time > 12 * 60) {
time -= 12 * 60;
quarter++;
}
while (quarter > 4 && time > 5 * 60) {
time -= 5 * 60;
quarter++;
}
let overtime = quarter - 4;
overtime = overtime > 0 ? overtime : 0;
quarter -= overtime;
let remaining = (overtime > 0) ?
5 * 60 - time :
12 * 60 - time
let minute = ("0" + Math.floor(remaining / 60)).slice(-2);
let second = ("0" + remaining % 60).slice(-2);
return (overtime > 0) ?
`${overtime}OT ${minute}:${second}` :
`${quarter}Q ${minute}:${second}`
}
const asyncForEach = async (arr, cb) => {
for (let i = 0; i < arr.length; i++) {
try {
await cb(arr[i], i, arr);
} catch (e) {
console.error(e);
}
}
return true;
}
const logoToTeam = el => {
const match = el.children[0].src.match(/(\w{2,4})\.png/);
if (match === null) {
throw new ScrapeError('Logo did not contain team');
}
return match[1].toUpperCase();
}
const processTimeEl = el => {
const { innerText } = el;
const msMatch = innerText.match(/(\d\d?)\:(\d\d)/);
if (msMatch === null) {
const sdMatch = innerText.match(/(\d\d?)\.\d/);
if (sdMatch === null) {
throw new ScrapeError('Time element did not contain timestamp');
}
return [0, Number(sdMatch[1])]
}
return [msMatch[1], msMatch[2]].map(Number);
}
const processScoreEl = el => {
const { innerText } = el;
const match = innerText.match(/(\d+) - (\d+)/);
if (match === null) {
throw new ScrapeError('Score element did not contain score');
}
return {
away: Number(match[1]),
home: Number(match[2])
}
}
const processDetail = el => {
const { innerText } = el;
const scoreMatch = innerText.match(/([\w'\.\- ]+)makes(.+free)?(.+three)?/);
if (scoreMatch !== null) {
const { 1: player, 2: ft, 3: three } = scoreMatch;
const points = ft ? 1 : (three ? 3 : 2);
return {
type: "score",
player: player.trim(),
points
}
}
const switchMatch = innerText.match(/([\w'\.\- ]*)enters the game for ?([\w'\.\- ]*)/);
if (switchMatch !== null) {
const { 1: subIn, 2: subOut } = switchMatch;
return {
type: "switch",
subIn: subIn.trim() || null,
subOut: subOut.trim() || null
}
}
const missMatch = innerText.match(/([\w'\.\- ]+)misses/);
if (missMatch !== null) {
const { 1: player } = missMatch;
return {
type: "miss",
player: player.trim()
}
}
const rbMatch = innerText.match(/([\w'\.\- ]+)(offensive|defensive) rebound/);
if (rbMatch !== null) {
const { 1: player } = rbMatch;
return {
type: "rebound",
player: player.trim()
}
}
const toMatch = innerText.match(/([\w'\.\- ]+)turnover/);
if (toMatch !== null) {
const { 1: player } = toMatch;
return {
type: "turnover",
player: player.trim()
}
}
return null;
}
module.exports = {
select,
selectAll,
toSeconds,
toPrettyTime,
asyncForEach
}