-
Notifications
You must be signed in to change notification settings - Fork 0
/
rentals.casper.js
212 lines (160 loc) · 5.3 KB
/
rentals.casper.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
/*global angular,CasperError,$*/
var step = 0,
Casper = require("casper"),
_ = require('lodash'),
fs = require("fs"),
options = {
verbose: true,
logLevel: "warning",
waitTimeout: 60000,
onError: function () {
this.echo("ERROR " + arguments);
this.capture("/tmp/" + (step++) + ".ERROR.png");
}
},
casper = Casper.create(options),
numWeeks = parseInt(casper.cli.args[0] || "8", 10),
minRating = parseFloat(casper.cli.args[1] || "6.0", 10),
noop = function () {},
titles;
casper.echo("Starting at " + new Date());
casper.start("about:blank");
function charts() {
var page = casper;//Casper.create(options);
page.thenOpen("http://www.officialcharts.com/charts/film-on-video-chart/");
page.then(function () {
titles = this.evaluate(function () {
return $("table.chart-positions tr").not("[class]").map(function () {
return {
weeks: parseInt($("td", this).last().prev().text().trim(), 10),
name: $("div.title", this).text().trim()
};
}).toArray();
});
titles = _(titles).filter(function (title) {
return title.weeks <= numWeeks;
}).sortBy('weeks').value();
});
}
function imdbLink (title) {
var page = casper;//Casper.create(options);
page.then(function () {
page.echo("Started searching IMDB link for '" + title.name + "'");
});
page.thenOpen(
"http://www.imdb.com/find?s=tt&ttype=ft&q=" + encodeURIComponent(title.name)
);
page.then(function () {
page.echo("Waiting for IMDB link for '" + title.name + "'");
});
page.waitForSelector("#main");
page.then(function () {
title.url = this.evaluate(function () {
if ($("a[name='tt']").closest(".findSection").find("td.result_text > a").length === 0) {
return null;
}
return $("a[name='tt']").closest(".findSection").find("td.result_text > a").first().prop("href");
});
if (!title.url) {
this.echo("IMDB link not found for '" + title.name + "'");
} else {
title.url = title.url.split("?")[0];
this.echo("IMDB link for '" + title.name + "' is " + title.url);
}
});
}
function imdb(title) {
var page = casper;//Casper.create(options);
page.then(function () {
page.echo("Opening IMDB info for '" + title.name + "' from " + title.url);
});
page.thenOpen(title.url);
page.waitForSelector("div.title_wrapper > h1");
page.then(function () {
var imdbInfo;
this.echo("Parsing IMDB info for '" + title.name + "' from " + this.getCurrentUrl());
imdbInfo = this.evaluate(function () {
function text(selector) {
return $(selector).contents().not($(selector).children()).text().trim();
}
function num(selector) {
return parseFloat(text(selector), 10);
}
return {
rating: num("div.ratingValue > strong > span"),
name: text("div.title_wrapper > h1"),
year: /[0-9]{4}/.exec(
$("#titleDetails > div > h4:contains('Release Date')").parent().text()
)[0],
description: $("#titleStoryLine > div > p > span").text().trim(),
genre: $("#titleStoryLine > div > h4:contains('Genres')").parent().find("a").map(
function () {
return $(this).text().trim();
}
).toArray().join(", ")
};
});
_.extend(title, imdbInfo);
});
}
casper.echo(
"Extracting films charted for max " + numWeeks + " week(s) with minimum rating " + minRating
);
charts();
casper.then(function () {
casper.echo("getting imdb links");
titles.forEach(imdbLink);
});
casper.then(function () {
casper.echo("filtering out titles with no imdb link");
titles = titles.filter(function (title) {
return !!title.url;
});
titles = _.uniqBy(titles, "url");
casper.echo("getting imdb info");
titles.forEach(imdb);
});
casper.then(function () {
var html;
this.echo("filtering out titles less than min rating");
if (titles.length === 0) {
this.die("Zero titles found");
return;
}
titles = titles.filter(function (title) {
return title.rating >= minRating;
});
if (titles.length === 0) {
this.die("Zero titles left after filtering by rating");
return;
}
this.echo("constructing html for remaining " + titles.length + " shows");
html = "<thead><tr>" +
"<th>Title</th><th>Rating</th><th>Weeks</th><th>Genre</th></tr></thead><tbody>";
titles.forEach(function (title, idx) {
html += "<tbody data-seq='" + idx + "' data-rating='" + title.rating + "' data-imdb='" +
title.url + "'><tr><th>" +
title.name + " (" + title.year + ")" + "</th><th>" +
(title.rating && title.rating.toFixed(1) || "???") +
"</th><th>" + title.weeks + "</th><th>" + title.genre + "</th></tr>" +
"<tr><td colspan=4>" + title.description + "<br/>" +
"<a href='" + title.url + "'>" + title.url + "</a></td></tr></tbody>\n";
});
html += "</tbody>";
this.echo("reading ejs source");
tmpl = fs.read("body.ejs");
this.echo("creating template from ejs");
tmpl = _.template(tmpl);
this.echo("generating html");
html = tmpl({
page: "movierentals", icon: "film", headerTitle: "Movie Rentals",
headerSubtitle: "overview of movie rentals rated greater than " + minRating.toFixed(1) +
" from the last " + numWeeks + " weeks",
tableContent: html, listSrcURL: "http://www.officialcharts.com",
listSrcName: "officialcharts.com",
lastUpdatedFile: "moviesupdated.json"
});
this.echo("writing html");
fs.write("movierentals.html", html, { mode: "w", charset: "utf-8" });
});
casper.run();