-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathownership.js
160 lines (149 loc) · 5.14 KB
/
ownership.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
var Nightmare = require('nightmare');
require('nightmare-inline-download')(Nightmare);
var fs = require('fs');
var http = require('http');
var nightmare = nightmareFactory();
loginToDK('', '')
.then(getDailyFantasyLinks)
.catch(function (error) {
console.log(error);
process.exit();
});
function clearDKCookies() {
return nightmare
.goto('https://www.draftkings.com/account/sitelogin/false?returnurl=%2Flobby')
.cookies.clearAll();
}
function loginToDK(username, password) {
return nightmare
.goto('https://www.draftkings.com/account/sitelogin/false?returnurl=%2Flobby')
.wait()
.type('#Username.textbox', username)
.type('#Password.textbox', password)
.click('#loginButton')
.wait(function () {
return window.location.href
.indexOf('https://www.draftkings.com/lobby') > -1;
});
}
function getDailyFantasyLinks() {
nightmareFactory()
.goto('https://rotogrinders.com/threads/category/main')
.wait()
.evaluate(function () {
var topics = document.querySelectorAll('td.topic > a');
return Array.prototype.map.call(topics, function (link) {
return {
title: link.innerText,
href: link.href
};
});
})
.then(function (topics) {
var dailyFantasyLinks = topics.filter(function (topic) {
return topic.title.indexOf('Daily Fantasy Tournament Links') === 0;
});
var tasks = dailyFantasyLinks.map(function (link) {
return getDKLinks(link.href, link.title);
});
Promise.all(tasks).then(function (response) {
var links = [];
response.forEach(function (contestLinks) {
links = links.concat(contestLinks);
});
downloadFiles(links);
}).catch(function (error) {
console.log(error);
process.exit();
});
})
.catch(function (error) {
console.log(error);
process.exit();
});
}
function getDKLinks(mainPage, contestDate) {
return new Promise(function (resolve) {
nightmareFactory()
.goto(mainPage)
.wait()
.evaluate(function () {
var links = document.querySelectorAll('p > a');
return Array.prototype.filter.call(links, function (link) {
var href = link.getAttribute('href');
var isDraftKings = href.indexOf('http://partners.draftkings.com') === 0;
var isDoubleUp = link.innerText.indexOf('DOUBLE UP') > -1;
return isDraftKings && isDoubleUp;
}).map(function (dkDoubleUp) {
return {
href: dkDoubleUp.getAttribute('href'),
title: dkDoubleUp.innerText
};
});
})
.then(function (draftKingDoubleUpLinks) {
var tasks = draftKingDoubleUpLinks.map(function (link) {
return {
href: link.href,
title: link.title,
date: contestDate
};
});
resolve(tasks);
});
});
}
function getFileName(contestTitle, contestDate) {
var fileName = [contestTitle.trim(), parseContestDate(contestDate)]
.join('_').replace(/[^\w]/g, '');
return './downloads/' + fileName + '.zip';
}
function downloadFiles(links) {
if (!links.length) {
return;
}
var link = links.pop();
var fileName = getFileName(link.title, link.date);
fs.exists(fileName, function (exists) {
if (!exists) {
console.log(fileName + ' does not exist!');
downloadFile(link.href, fileName)
.then(downloadFiles.bind(void 0, links));
} else {
console.log(fileName + ' already exists!');
downloadFiles(links);
}
});
}
function downloadFile(contestLink, fileName) {
return new Promise(function (resolve) {
nightmare.goto(contestLink)
.wait('#export-lineups-csv')
.click('#export-lineups-csv')
.download(fileName)
.then(function () {
resolve();
console.log(contestLink + ':' + fileName + ' was saved');
})
.catch(resolve);
});
}
function parseContestDate(contestDate) {
return contestDate.replace(/Daily Fantasy Tournament Links/, '');
}
function nightmareFactory() {
return new Nightmare({
webPreferences: {
partition: 'nopersist'
}
}).useragent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36");
}
function sequentialPromiseFactory (promises) {
var sequence = Promise.resolve();
promises.forEach(function (promise) {
sequence = sequence.then(function () {
return promise();
});
});
return sequence;
}