forked from meppadiyan/movieStats
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathletterboxd.ml.js
254 lines (226 loc) · 7.93 KB
/
letterboxd.ml.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
const cheerio = require("cheerio");
const fs = require("fs");
const { HttpsProxyAgent } = require("https-proxy-agent");
const { groupBy, orderBy, pick, startCase, unescape } = require("lodash");
const fetch = require("node-fetch");
const path = require("path");
const { isJSON } = require("validator");
const { proxy } = require("./config/env.js");
const { toEnIn } = require("./config/misc.js");
const { moment } = require("./config/moment.js");
const { client } = require("./config/snoowrap.js");
const config_path = path.resolve(__dirname, "./letterboxd.ml.json");
const configs = JSON.parse(fs.readFileSync(config_path, "utf8"));
async function fetchWrapper(url, retry = 0) {
try {
const r = await fetch(url, {
...(proxy ? { agent: new HttpsProxyAgent(proxy) } : {}),
headers: { "user-agent": "curl/1.0" },
});
if (![200].includes(r.status)) throw new Error("invalid status");
const text = await r.text();
return isJSON(text) ? JSON.parse(text) : text;
} catch (e) {
console.error(e);
if (retry < 3) {
await new Promise((r) => setTimeout(r, 60000 * retry));
return await fetchWrapper(url, retry + 1);
} else throw e;
}
}
(async () => {
// const reddit_post_id = "1hio2ju"; // 2024 year-in-review
const reddit_post_id = "1hl4vy1"; // top100
// // lang:ml
// for (let i = 0; i < 15; i++) {
// const url = `https://letterboxd.com/films/ajax/language/malayalam/by/release/page/${
// i + 1
// }`;
// console.log(url);
// const $1 = cheerio.load(
// await fetch(url, {
// ...(proxy ? { agent: new HttpsProxyAgent(proxy) } : {}),
// headers: { "user-agent": "curl/1.0" },
// }).then((r) => r.text())
// );
// // if (!$1("[data-film-slug]").length) break;
// $1("[data-film-slug]").each((_i, e) => {
// const ltrbxd_slug = $1(e).attr("data-film-slug");
// if (!configs.filter((i) => ltrbxd_slug === i.ltrbxd_slug).length) {
// console.log(i + 1, ltrbxd_slug);
// configs.push({
// enable: true,
// last_updated_at: moment().format("YYYY-MM-DDTHH:mmZ"),
// ltrbxd_slug,
// });
// }
// });
// fs.writeFileSync(config_path, JSON.stringify(configs, undefined, 2));
// }
// lang:ml metadata
for (const config of configs.filter((i) => i.enable && !i.name))
try {
const { name, originalName, releaseYear, runTime, slug } = await fetch(
`https://letterboxd.com/film/${config.ltrbxd_slug}/json`,
{
...(proxy ? { agent: new HttpsProxyAgent(proxy) } : {}),
headers: { "user-agent": "curl/1.0" },
}
).then((r) => r.json());
if (name) config.name = name;
if (originalName) config.originalName = originalName;
if (releaseYear) config.releaseYear = releaseYear;
if (runTime) config.runTime = runTime;
if (slug) config.ltrbxd_slug = slug;
console.log(config);
config.last_updated_at = moment().format("YYYY-MM-DDTHH:mmZ");
fs.writeFileSync(config_path, JSON.stringify(configs, undefined, 2));
} catch (e) {
console.error(e);
}
// dedup ltrbxd_slug
console.log(
JSON.stringify(
Object.entries(
groupBy(
configs.filter((i) => i.enable),
"ltrbxd_slug"
)
)
.filter(([k, v]) => 1 < v.length)
.map(([k, v]) => ({ [k]: v })),
undefined,
2
)
);
// letterboxd fetch
for (const config of orderBy(
configs.filter(
(i) => i.enable && 5 < moment().diff(i.last_updated_at, "day")
),
[(i) => i.last_updated_at || ""], // last updated first
["asc"]
)) {
console.log(config);
const $1 = cheerio.load(
await fetchWrapper(`https://letterboxd.com/film/${config.ltrbxd_slug}`)
);
const metadata = $1('script:contains("<![CDATA[")')
.text()
.match(/\/\*.*<\!\[CDATA\[.*\*\/([\s+\S+]*)\/\*.*\]\]>.*\*\//);
if (metadata && isJSON(metadata[1])) {
const { director = [], genre = [], image } = JSON.parse(metadata[1]);
// director
for (const { name, sameAs } of director) {
if (!config.director) config.director = {};
config.director[sameAs] = name;
}
if (genre.length) config.genre = genre; // genre
if (image) config.image = image; // image
config.last_updated_at = moment().format("YYYY-MM-DDTHH:mmZ");
fs.writeFileSync(config_path, JSON.stringify(configs, undefined, 2));
}
// releaseDate
const releaseDate = $1('.-bycountry > .listitem:contains("India")')
.find('.release-date-list:contains("Theatrical")')
.find(".date")
.text();
if (releaseDate && moment(releaseDate, ["DD MMM YYYY"]).isValid()) {
config.releaseDate = moment(releaseDate, ["DD MMM YYYY"]).format(
"YYYY-MM-DD"
);
config.last_updated_at = moment().format("YYYY-MM-DDTHH:mmZ");
fs.writeFileSync(config_path, JSON.stringify(configs, undefined, 2));
}
// ratings
const $10 = cheerio.load(
await fetchWrapper(
`https://letterboxd.com/csi/film/${config.ltrbxd_slug}/rating-histogram`
)
);
if (!$10(".ratings-histogram-chart").length) continue;
const average_selector = ".average-rating a";
if ($10(average_selector).length) {
const [average, total] = $10(average_selector)
.attr("title")
.match(/([0-9,.%]+)/g);
config.average = +average;
config.total = +total.replace(/[^0-9.]+/g, "");
}
const selectors = {
half: 'a[href*="/rated/%C2%BD/"]',
one: 'a[href*="/rated/1/"]',
one_half: 'a[href*="/rated/1%C2%BD/"]',
two: 'a[href*="/rated/2/"]',
two_half: 'a[href*="/rated/2%C2%BD/"]',
three: 'a[href*="/rated/3/"]',
three_half: 'a[href*="/rated/3%C2%BD/"]',
four: 'a[href*="/rated/4/"]',
four_half: 'a[href*="/rated/4%C2%BD/"]',
five: 'a[href*="/rated/5/"]',
};
for (const [k, v] of Object.entries(selectors))
if ($10(v).length) {
const [count] = $10(v)
.attr("title")
.match(/([0-9,.%]+)/g);
config[k] = +count.replace(/[^0-9.]+/g, "");
}
config.last_updated_at = moment().format("YYYY-MM-DDTHH:mmZ");
fs.writeFileSync(config_path, JSON.stringify(configs, undefined, 2));
}
// table generation
let rank = 1;
let text = `| Rank | Movie | Reviews | Average | Director | Genre | Released At | Last Updated At |\n| -: | :- | -: | -: | :- | :- | :- | :- |`;
for (const config of orderBy(
configs.filter(
(i) => i.enable && i.releaseDate // && i.releaseDate.startsWith("2024") // top100
),
[
(i) => i.average || 0,
(i) =>
(i.half || 0) +
(i.one || 0) +
(i.one_half || 0) +
(i.two || 0) +
(i.two_half || 0) +
(i.three || 0) +
(i.three_half || 0) +
(i.four || 0) +
(i.four_half || 0) +
(i.five || 0),
],
["desc", "desc"]
).slice(0, 100)) {
// top100
const total =
config.total ||
(config.half || 0) +
(config.one || 0) +
(config.one_half || 0) +
(config.two || 0) +
(config.two_half || 0) +
(config.three || 0) +
(config.three_half || 0) +
(config.four || 0) +
(config.four_half || 0) +
(config.five || 0);
text += `\n| ${rank++} | [${startCase(
config.name
)}](https://letterboxd.com/film/${config.ltrbxd_slug})${
config.originalName ? ` ~ ${config.originalName}` : ""
} | ${total ? toEnIn(total) : ""} | ${
config.average ? config.average : ""
} | ${Object.entries(config.director || {})
.map(([k, v]) => `[${v}](https://letterboxd.com${k})`)
.sort()
.join(" / ")} | ${(config.genre || []).sort().join(" / ")} | ${
config.releaseDate
} | ${config.last_updated_at.split("T")[0]} |`;
}
console.log(text);
// reddit
await new Promise((resolve, reject) =>
client.getSubmission(reddit_post_id).edit(text).then(resolve).catch(reject)
);
})();