-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvshows.puppeteer.js
373 lines (298 loc) · 9.83 KB
/
tvshows.puppeteer.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const puppeteer = require("puppeteer");
const _ = require("lodash");
const fs = require("fs");
const ArgumentParser = require("argparse").ArgumentParser;
const parser = require("episode-parser");
// create a custom timestamp format for log statements
const SimpleNodeLogger = require('simple-node-logger');
const log = SimpleNodeLogger.createSimpleLogger({
timestampFormat: 'YYYY-MM-DD HH:mm:ss.SSS'
});
log.setLevel("info");
let _eztvURL = "https://eztvx.to/";
class Scraper {
constructor(numPages) {
this._numPages = numPages;
}
async execute() {
let unknownShows = [],
showsCache = {},
showsByName = {};
log.info(`Extracting ${this._numPages} pages of TV shows from EZTV`);
await this.initBrowser();
log.info("Extracting titles from EZTV");
let shows = await this.eztv();
log.debug("loading shows from cache");
try {
showsCache = JSON.parse(fs.readFileSync("shows.json"));
} catch (e) {
log.warn("Cache not found: " + e);
}
for (let show of shows) {
let showCache = showsCache[_.snakeCase(show.initialName)];
if (showCache) {
_.extend(show, showCache);
}
}
log.info("Finding imdb links for " + shows.length + " shows");
for (let show of shows) {
if (show.url) {
log.debug("(Cached) IMDB link for '" + show.initialName + "' is " + show.url);
} else {
try {
await this.imdbLink(show);
} catch (e) {
log.warn(this.page.content());
}
}
}
log.debug("filtering shows with no imdb link");
unknownShows = shows.filter(function (show) {
return !show.url;
});
shows = shows.filter(function (show) {
return show.url;
});
log.info(shows.length + " shows remaining after filtering for imdb link");
log.debug("finding imdb info for remaining " + shows.length + " shows");
for (let show of shows) {
if (show.rating) {
log.debug("(Cached) IMDB info for '" + show.initialName);
} else {
await this.imdb(show);
}
}
log.debug("filtering shows with no imdb info");
unknownShows.push.apply(unknownShows, shows.filter(function (show) {
return !show.description;
}));
shows = shows.filter(function (show) {
return show.description;
});
log.info(shows.length + " shows remaining after filtering for description");
// Should already be de-duped when scraping EZTV, but some slip the net.
shows = _.uniqBy(shows, 'url');
log.info(shows.length + " shows remaining after de-duplication");
if (shows.length === 0) {
log.info("Zero titles left after filtering");
return;
}
log.debug("writing show info to disk cache");
for (let show of shows) {
showsByName[_.snakeCase(show.initialName)] = _.pick(show, [
"season", "episode", "url", "year", "genre", "rating", "duration",
"description", "name"]);
}
_.assign(showsCache, showsByName);
try {
fs.writeFileSync("shows.json", JSON.stringify(showsCache));
} catch (e) {
log.warn("Failed to write cache: " + e);
}
log.info("Constructing html for " + shows.length + " shows");
let html = "<thead><tr><th>Title</th><th>Rating</th><th>Genre</th><th>Duration</th></tr>" +
"</thead><tbody>\n";
shows.forEach((show, idx) => {
html += this.showHTML(show, idx);
});
html += "</tbody>";
log.debug("Reading ejs source");
const tmplStr = fs.readFileSync("body.ejs");
log.debug("Creating template from ejs");
const tmpl = _.template(tmplStr);
log.debug("Generating html");
html = tmpl({
page: "tvshows", icon: "blackboard", headerTitle: "TV Shows",
headerSubtitle: "overview of popular shows airing in the last couple of days",
tableContent: html, listSrcURL: "https://eztv.ag", listSrcName: "EZTV",
lastUpdatedFile: "tvupdated.json"
});
log.debug("Writing html");
fs.writeFileSync("tvshows.html", html);
log.warn("Unknown shows:\n" + _.uniq(unknownShows.map(function (show) {
return " " + show.initialName;
})).sort().join("\n"));
}
/**
* Fire up the Chrome headless browser and open a blank page.
*/
async initBrowser() {
// Only one request at once...
if (this.browser)
throw new Error("Attempting to open more than one browser session");
try {
// Open browser.
this.browser = await puppeteer.launch({
// headless: false,
args: [
'--no-sandbox',
'--disable-setuid-sandbox'
]
});
// Open new tab in browser.
this.page = await this.browser.newPage();
await this.page.setUserAgent(
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)' +
' Chrome/78.0.3904.108 Safari/537.36');
await this.page.setViewport({width: 1280, height: 720});
// Log site console logs.
this.page.on('console', (message) => {
const type = message.type().substr(0, 3).toUpperCase();
log.debug(`---- Page: ${type} ${message.text()} ${message.location().url || ""}`);
});
} catch (e) {
await this.logError(e);
}
}
async eztv() {
let url = _eztvURL + "home",
titles = [],
pageTitles;
log.info("Extracting titles");
for (let pageNum = 0; pageNum <= this._numPages; pageNum++) {
log.debug("opening eztv page: " + url);
await this.page.goto(url, {waitUntil: "domcontentloaded"});
log.debug("extracting titles");
pageTitles = await this.page.evaluate(function () {
return $("a.epinfo").map(function () {
return $(this).text().trim();
}).get();
});
pageTitles = pageTitles.map(function (title) {
return parser(title);
}).filter(function (title) {
return title && title.show;
});
[].push.apply(titles, pageTitles);
url = _eztvURL + "page_" + pageNum;
}
titles = _.uniqBy(titles, 'show');
titles = titles.map(function ({show, season, episode}) {
return {initialName: show, name: show, season, episode};
});
return titles;
}
async imdbLink(title) {
log.info("Search IMDB link for " + title.name);
await this.page.goto(
"http://www.imdb.com/find?s=tt&ttype=tv&q=" + encodeURIComponent(title.name)
);
log.debug("Waiting for selector on " + "http://www.imdb.com/find?s=tt&ttype=tv&q=" +
encodeURIComponent(title.name));
await this.page.waitForSelector(".ipc-metadata-list");
title.url = await this.page.evaluate(() => {
return document.querySelector("a.ipc-metadata-list-summary-item__t").href;
});
if (!title.url) {
log.warn("IMDB link not found for '" + title.name + "'");
} else {
title.url = title.url.split("?")[0];
log.info("IMDB link for '" + title.name + "' is " + title.url);
}
}
async imdb(show) {
let imdbInfo;
log.info("Opening IMDB info for '" + show.name + "' from " + show.url);
await this.page.goto(show.url);
try {
await this.page.waitForSelector(
"[data-testid='hero__pageTitle'], div.title_wrapper > h1");
log.info("Parsing IMDB info for '" + show.name + "' from " + this.page.url());
imdbInfo = await this.page.evaluate(function () {
function $$(selector) {
return Array.from(document.querySelectorAll(selector));
}
function $q(selector) {
return document.querySelector(selector);
}
function $children(selector) {
try {
return Array.from(document.querySelector(selector).children);
} catch (e) {
console.error("Failed to query children of " + selector);
return [];
}
}
function $texts(els) {
return els.map((el) => el.textContent.trim()).join(", ");
}
return {
duration: $texts($$(
'li[data-testid="title-techspec_runtime"] > div'
)),
rating: $children(
"[data-testid='hero-rating-bar__aggregate-rating__score']"
).map((el) => parseFloat(el.textContent.trim(), 10))[0],
name: $q("[data-testid='hero__pageTitle']").textContent,
year: $texts($$(
"[data-testid='hero__pageTitle'] + ul > li:nth-child(2) > a")),
description: $q("[data-testid='plot-xl']").textContent.trim(),
genre: $texts($$("[data-testid='genres'] span"))
};
});
} catch (e) {
log.error(`Failed to parse IMDB page for "${show.initialName}": ${show.url} ... `, e);
return;
}
log.debug(_.truncate(JSON.stringify(imdbInfo), {length: 1000}));
["name", "description"].forEach((attr) => {
if (imdbInfo[attr])
imdbInfo[attr] = _.escape(imdbInfo[attr]);
});
_.each(imdbInfo, (value, key) => {
if (!value) {
log.warn(key + " has no value: '" + value + "'");
}
});
_.extend(show, imdbInfo);
}
showHTML(show, seq) {
try {
return "<tbody data-seq='" + seq + "' data-imdb='" + show.url + "' data-rating='" + (show.rating || 0) +
"' data-episode='S" + _.padStart(show.season, 2, "0") +
"E" + _.padStart(show.episode, 2, "0") + "'><tr><th>" +
show.name + " (" + show.year + ")" + "</th><th>" +
(show.rating && show.rating.toFixed(1) || "???") + "</th><th>" + show.genre +
"</th><th>" + show.duration + "</th>" +
"</tr><tr><td colspan=4>" + show.description + "<br/>" +
"<a href='" + show.url + "'>" + show.url + "</a></td></tr></tbody>\n";
} catch (e) {
log.error("Failed to generate HTML for show:\n" + JSON.stringify(show));
throw e;
}
}
/**
* Log the error.
*
* @param {Error} e exception to log
*/
async logError(e) {
log.error("Unexpected exception", e);
}
}
if (require.main === module) {
const parser = new ArgumentParser({
version: '1.0.0',
addHelp: true,
description: "Scrape TV shows from EZTV, furnish with" +
" info from IMDB, then write html of results"
});
parser.addArgument("--pages", {
help: "Max pages of EZTV scrape",
type: "int",
defaultValue: 15
});
const [args, remainder] = parser.parseKnownArgs(process.argv);
// log.debug(JSON.stringify(process.argv));
// log.debug(JSON.stringify(args));
const scraper = new Scraper(args.pages);
scraper.execute().then(() => {
log.info("Completed successfully");
// Allow time for logger to flush, then exit.
process.nextTick(() => process.exit());
}, (err) => {
log.error("Unhandled exception", err);
// Allow time for logger to flush, then exit with error code.
process.nextTick(() => process.exit(1));
});
}