-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
225 lines (221 loc) · 7.35 KB
/
app.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
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
var url = require('url');
var _ = require('underscore');
var express = require('express');
var cheerio = require('cheerio');
var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'));
var ESPN_PROTO = 'http';
var ESPN_HOST = 'games.espn.go.com';
var SEASON = new Date().getFullYear();
function getLeagueRecentActivity($){
return $('ul#lo-recent-activity-list li.lo-recent-activity-item')
.map(function(i,e){
var $e = $(e);
return {
date: $e.find('.recent-activity-date').text(),
time: $e.find('.recent-activity-time').text(),
type: $e.find('.recent-activity-image').attr('class')
.split(' ').reverse()[0],
// TODO: parse harder
desc: $e.find('.recent-activity-description').text()
};
});
}
var app = express()
.get('/leagues/:id', function(req, res){
var espnUrl = url.format({
protocol: ESPN_PROTO,
host: ESPN_HOST,
pathname: '/ffl/leagueoffice',
query: {
leagueId: req.param('id')
}
});
request.getAsync(espnUrl)
.spread(function(espnRes){
var $ = cheerio.load(espnRes.body);
var name = $(".league-team-names h1").text();
var teams = [];
var currentDivision;
$('.lo-sidebar-box').eq(-1).find('table tr').map(function(i,e){
var $e = $(e);
var newDivision = $e.find('td.division-name');
if (newDivision.length) {
currentDivision = newDivision.text();
return;
}
var teamLink = $e.find('td').eq(1).find('a');
if (!teamLink.length) return;
var teamName = teamLink.text();
var teamId = teamLink.attr('href').match(/teamId=(\d*)/)[1];
var team = {name: teamName, id: teamId};
var record = $e.find('td').eq(2).text().split('-');
team.wins = Number(record[0]);
team.losses = Number(record[1]);
team.division = currentDivision;
teams.push(team);
});
res.json({
id : req.param('id'),
name : name,
teams: teams,
recent_activity: getLeagueRecentActivity($),
manager_note: $('.lm-note-body').html()
});
});
})
.get('/leagues/:id/recent_activity', function(req, res){
var espnurl = url.format({
protocol: ESPN_PROTO,
host: ESPN_HOST,
pathname: '/ffl/leagueoffice',
query: {
leagueId: req.param('id'),
seasonId: SEASON
}
});
request.getAsync(espnurl)
.spread(function(espnres){
var $ = cheerio.load(espnres.body);
var actions = getLeagueRecentActivity($);
res.json(actions);
});
})
.get('/leagues/:id/scoreboard', function(req, res){
var espnurl = url.format({
protocol: ESPN_PROTO,
host: ESPN_HOST,
pathname: '/ffl/scoreboard',
query: {
leagueId: req.param('id'),
seasonId: SEASON,
matchupPeriodId: req.param('week')
}
});
request.getAsync(espnurl)
.spread(function(espnres){
var $ = cheerio.load(espnres.body);
var matchups = $('#scoreboardMatchups table.matchup').map(function(i,e){
var sides = $(e).find('tr').slice(0, 2).map(function(i,e){
var teamAnchor = $(e).find('a').eq(0);
var team = {
id: teamAnchor.attr('href').match('teamId=([0-9]+)')[1],
name: teamAnchor.text(),
record: $(e).find('.record').text(), // TODO: parse harder
};
return {
team: team,
score: Number($(e).find('td.score').text())
};
});
return sides;
});
res.json(matchups);
});
})
.get('/leagues/:league_id/players', function(req, res){
var SLOT_CATEGORY_ID = {
QB: 0,
RB: 2,
WR: 4,
TE: 6,
'D/ST': 16,
K: 17
};
var espnurl = url.format({
protocol: ESPN_PROTO,
host: ESPN_HOST,
// pathname: '/ffl/playertable/prebuilt/freeagency',
pathname: '/ffl/leaders',
query: {
leagueId: req.param('league_id'),
seasonId: req.param('season'),
avail: -1, // -1: all, 1: FA
slotCategoryId: SLOT_CATEGORY_ID[req.param('position')]
}
});
request.getAsync(espnurl)
.spread(function(espnres){
var $ = cheerio.load(espnres.body);
var lines = $('table.playerTableTable tr.pncPlayerRow').map(function(i,e){
var $e = $(e);
var playerAnchor = $e.find('td.playertablePlayerName a');
var teamAndPos = $e.find('td.playertablePlayerName').contents().eq(1).text().split(/\s+/);
return {
id: playerAnchor.attr('playerid'),
name: playerAnchor.text(),
team: teamAndPos[1],
pos: teamAndPos[2],
stats: {
points: parseInt($e.find('td.playertableStat.appliedPoints').eq(0).text())
}
};
});
res.send(lines);
});
})
.get('/leagues/:league_id/teams/:team_id/news', function(req, res){
var espnUrl = url.format({
protocol: ESPN_PROTO,
host: ESPN_HOST,
pathname: '/ffl/playertable/prebuilt/manageroster',
query: {
leagueId: req.param('league_id'),
teamId: req.param('team_id'),
seasonId: SEASON,
// no idea what the rest of this does
view: 'news',
context: 'clubhouse',
ajaxPath: 'playertable/prebuilt/manageroster',
managingIr: false,
droppingPlayers: false,
asLM: false,
scoringPeriodId: 1,
version: 'chrono',
r: 24668505 // random number for cache bust?
}
});
request.getAsync(espnUrl)
.spread(function(espnRes){
var $ = cheerio.load(espnRes.body);
var stories = $('#lm-newsfeed .feedentry').map(function(i,e){
// TODO: parse harder
var $e = $(e);
return {
title: $e.find('.feedtitle').text(),
date: $e.find('.feeddate').text(),
text: $e.find('.feedcontent .pni-contents').text()
};
})
res.json(stories);
});
})
.get('/leagues/:league_id/teams/:team_id/roster', function(req, res){
var espnurl = url.format({
protocol: ESPN_PROTO,
host: ESPN_HOST,
pathname: '/ffl/clubhouse',
query: {
leagueId: req.param('league_id'),
teamId: req.param('team_id'),
seasonId: SEASON
}
});
request.getAsync(espnurl)
.spread(function(espnres){
var $ = cheerio.load(espnres.body);
var rosterSpots = $('table.playerTableTable tr.pncPlayerRow').map(function(i,e){
var playerAnchor = $(e).find('td').eq(1).find('a').eq(0);
var player = !playerAnchor.length ? null : {
id: playerAnchor.attr('playerid'),
name: playerAnchor.text()
}
return {
pos: $(e).find('td').eq(0).text(),
player: player
};
});
res.json(rosterSpots);
})
});
module.exports = app;