-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget-from-html.js
391 lines (302 loc) · 10.3 KB
/
get-from-html.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"use strict";
const types = require("./types");
const utils = require("./utils");
function getCssUrl(urlString) {
const pattern = /url\s*\(\s*(['"])?/;
const match = pattern.exec(urlString);
if (match === null) { return null; }
const quote = match[1] || "";
urlString = urlString.substr(match.index + match[0].length - quote.length);
const pattern2 = new RegExp(`(${quote})\\s*\\)\\s*$`);
const match2 = pattern2.exec(urlString);
if (match2 === null) { return null; }
const url = urlString.substr(0, match2.index + match2[1].length);
let url2 = url;
if (!quote) {
url2 = url.replace(/"/g, "\\\"");
url2 = `"${url2}"`;
} else if (quote === "'") {
url2 = url.substr(1, url.length - 2);
url2 = url2.replace(/\\'/g, "'");
url2 = `"${url2}"`;
}
try {
return JSON.parse(url2);
} catch (e) {
return url;
}
}
function getTimestamp(text) {
const match = /([0-9]+)-([0-9]+)-([0-9]+)\s+([0-9]+):([0-9]+)/.exec(text);
if (match === null) { return null; }
return Date.UTC(
parseInt(match[1], 10), // year
parseInt(match[2], 10) - 1, // month
parseInt(match[3], 10), // day
parseInt(match[4], 10), // hours
parseInt(match[5], 10), // minutes
0, // seconds
0); // milliseconds
}
function getTitle(html) {
const node = html.querySelector("#gn");
return (node !== null ? node.textContent.trim() : null);
}
function getTitleOriginal(html) {
const node = html.querySelector("#gj");
return (node !== null ? node.textContent.trim() : null);
}
function getMainThumbnailUrl(html) {
const node = html.querySelector("#gd1>div");
if (node === null) { return null; }
let url = getCssUrl(node.style.backgroundImage);
if (url !== null) { return url; }
const img = node.querySelector("img[src]");
return (img !== null ? img.getAttribute("src") : null);
}
function getCategory(html) {
const node = html.querySelector("#gdc>div[onclick]");
if (node === null) { return null; }
const pattern = /['"].*?\/\/.+?\/(.*?)(\?.*?)?(#.*?)?['"]/;
const match = pattern.exec(node.getAttribute("onclick") || "");
return (match !== null ? match[1] : null);
}
function getUploader(html) {
const node = html.querySelector("#gdn>a");
if (node === null) { return null; }
const pattern = /^.*?\/\/.+?\/(.*?)(\?.*?)?(#.*?)?$/;
const match = pattern.exec(node.getAttribute("href") || "");
return (match !== null ? (match[1].split("/")[1] || "") : null);
}
function getRatingCount(html) {
const node = html.querySelector("#rating_count");
if (node === null) { return null; }
const value = parseInt(node.textContent.trim(), 10);
return (Number.isNaN(value) ? null : value);
}
function getRatingAverage(html) {
const node = html.querySelector("#rating_label");
if (node === null) { return null; }
const pattern = /average:\s*([0-9\.]+)/i;
const match = pattern.exec(node.textContent);
if (match === null) { return null; }
const value = parseFloat(match[1]);
return (Number.isNaN(value) ? null : value);
}
function getFavoriteCount(html) {
const node = html.querySelector("#favcount");
if (node === null) { return null; }
const pattern = /\s*([0-9]+|once)/i;
const match = pattern.exec(node.textContent);
if (match === null) { return null; }
const match1 = match[1];
return (match1.toLowerCase() === "once" ? 1 : parseInt(match1, 10));
}
function getFavoriteCategory(html) {
const node = html.querySelector("#fav>div.i");
if (node === null) { return null; }
const title = node.getAttribute("title") || "";
const pattern = /background-position\s*:\s*\d+(?:px)?\s+(-?\d+)(?:px)/;
const match = pattern.exec(node.getAttribute("style") || "");
const index = (match !== null) ?
Math.floor((Math.abs(parseInt(match[1], 10)) - 2) / 19) :
-1;
return { index, title };
}
function getThumbnailSize(html) {
const nodes = html.querySelectorAll("#gdo4>.nosel");
if (nodes.length < 2) { return null; }
return (nodes[0].classList.contains("ths") ? "normal" : "large");
}
function getThumbnailRows(html) {
const nodes = html.querySelectorAll("#gdo2>.nosel");
if (nodes.length === 0) { return null; }
const pattern = /\s*([0-9]+)/;
for (const node of nodes) {
if (node.classList.contains("ths")) {
const match = pattern.exec(node.textContent);
if (match !== null) {
return parseInt(match[1], 10);
}
}
}
return null;
}
function getTags(html) {
const pattern = /(.+):/;
const groups = html.querySelectorAll("#taglist tr");
const tags = {};
for (const group of groups) {
const tds = group.querySelectorAll("td");
if (tds.length === 0) { continue; }
const match = pattern.exec(tds[0].textContent);
const namespace = (match !== null ? match[1].trim() : "");
let namespaceTags;
if (tags.hasOwnProperty(namespace)) {
namespaceTags = tags[namespace];
} else {
namespaceTags = [];
tags[namespace] = namespaceTags;
}
const tagDivs = tds[tds.length - 1].querySelectorAll("div");
for (const div of tagDivs) {
const link = div.querySelector("a");
if (link === null) { continue; }
const tag = link.textContent.trim();
namespaceTags.push(tag);
}
}
return tags;
}
function getDetailsNodes(html) {
return html.querySelectorAll("#gdd tr");
}
function getDateUploaded(detailsNodes) {
if (detailsNodes.length <= 0) { return null; }
const node = detailsNodes[0].querySelector(".gdt2");
return (node !== null ? getTimestamp(node.textContent) : null);
}
function getVisibleInfo(detailsNodes) {
let visible = true;
let visibleReason = null;
if (detailsNodes.length > 2) {
const node = detailsNodes[2].querySelector(".gdt2");
if (node !== null) {
const pattern = /no\s+\((.+?)\)/i;
const match = pattern.exec(node.textContent);
if (match !== null) {
visible = false;
visibleReason = match[1].trim();
}
}
}
return { visible, visibleReason };
}
function getLanguageInfo(detailsNodes) {
let language = null;
let translated = false;
if (detailsNodes.length > 3) {
const node = detailsNodes[3].querySelector(".gdt2");
if (node !== null) {
const textNode = node.firstChild;
if (textNode !== null && textNode.nodeType === Node.TEXT_NODE) {
language = textNode.nodeValue.trim();
}
const trNode = node.querySelector(".halp");
translated = (trNode !== null && trNode.textContent.trim().toLowerCase() === "tr");
}
}
return { language, translated };
}
function getApproximateTotalFileSize(detailsNodes) {
if (detailsNodes.length <= 4) { return null; }
const node = detailsNodes[4].querySelector(".gdt2");
if (node === null) { return null; }
const pattern = /([0-9\.]+)\s*(\w+)/i;
const match = pattern.exec(node.textContent);
return (match !== null ? utils.getBytesSizeFromLabel(match[1], match[2]) : null);
}
function getFileCount(detailsNodes) {
if (detailsNodes.length <= 5) { return null; }
const node = detailsNodes[5].querySelector(".gdt2");
if (node === null) { return null; }
const pattern = /([0-9,]+)\s*pages/i;
const match = pattern.exec(node.textContent);
return (match !== null ? parseInt(match[1].replace(/,/g, ""), 10) : null);
}
function getParent(detailsNodes) {
if (detailsNodes.length <= 1) { return null; }
const node = detailsNodes[1].querySelector(".gdt2>a");
if (node === null) { return null; }
const info = utils.getGalleryIdentifierAndPageFromUrl(node.getAttribute("href") || "");
return (info !== null ? info.identifier : null);
}
function getNewerVersions(html) {
const results = [];
const nodes = html.querySelectorAll("#gnd>a");
for (const node of nodes) {
const info = utils.getGalleryIdentifierAndPageFromUrl(node.getAttribute("href") || "");
if (info === null) { continue; }
const galleryInfo = {
identifier: info.identifier,
name: node.textContent.trim(),
dateUploaded: null
};
if (node.nextSibling !== null) {
galleryInfo.dateUploaded = getTimestamp(node.nextSibling.textContent);
}
results.push(galleryInfo);
}
return results;
}
function getTorrentCount(html) {
const nodes = html.querySelectorAll("#gd5 .g2>a");
const pattern = /\btorrent\s+download\s*\(\s*(\d+)\s*\)/i;
for (const node of nodes) {
const match = pattern.exec(node.textContent);
if (match !== null) {
return parseInt(match[1], 10);
}
}
return null;
}
function getArchiverKey(html) {
const nodes = html.querySelectorAll("#gd5 .g2>a");
const pattern = /\barchive\s+download\b/i;
for (const node of nodes) {
const match = pattern.exec(node.textContent);
if (match !== null) {
const pattern2 = /&or=([^'"]*)['"]/;
const match2 = pattern2.exec(node.getAttribute("onclick") || "");
return (match2 !== null ? match2[1] : null);
}
}
return null;
}
function populateGalleryInfoFromHtml(info, html) {
// General
info.title = getTitle(html);
info.titleOriginal = getTitleOriginal(html);
info.mainThumbnailUrl = getMainThumbnailUrl(html);
info.category = getCategory(html);
info.uploader = getUploader(html);
info.ratingCount = getRatingCount(html);
info.ratingAverage = getRatingAverage(html);
info.favoriteCount = getFavoriteCount(html);
info.favoriteCategory = getFavoriteCategory(html);
info.thumbnailSize = getThumbnailSize(html);
info.thumbnailRows = getThumbnailRows(html);
info.newerVersions = getNewerVersions(html);
info.torrentCount = getTorrentCount(html);
info.archiverKey = getArchiverKey(html);
// Details
const detailsNodes = getDetailsNodes(html);
info.dateUploaded = getDateUploaded(detailsNodes);
info.parent = getParent(detailsNodes);
const visibleInfo = getVisibleInfo(detailsNodes);
info.visible = visibleInfo.visible;
info.visibleReason = visibleInfo.visibleReason;
const languageInfo = getLanguageInfo(detailsNodes);
info.language = languageInfo.language;
info.translated = languageInfo.translated;
info.approximateTotalFileSize = getApproximateTotalFileSize(detailsNodes);
info.fileCount = getFileCount(detailsNodes);
// Tags
info.tags = getTags(html);
info.tagsHaveNamespace = true;
}
function getFromHtml(html, url) {
const link = html.querySelector(".ptt td.ptds>a[href],.ptt td.ptdd>a[href]");
if (link === null) { return null; }
const idPage = utils.getGalleryIdentifierAndPageFromUrl(link.getAttribute("href") || "");
if (idPage === null) { return null; }
const info = new types.GalleryInfo();
info.identifier = idPage.identifier;
info.currentPage = idPage.page;
info.source = "html";
populateGalleryInfoFromHtml(info, html);
info.sourceSite = utils.getSourceSiteFromUrl(url);
info.dateGenerated = Date.now();
return info;
}
module.exports = getFromHtml;