-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (62 loc) · 3.14 KB
/
index.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
const axios = require('axios');
const cheerio = require('cheerio');
const methods = {};
methods.searchMovie = require('./structures/searchMovie');
methods.searchUser = require('./structures/searchUser');
methods.searchPodcast = require('./structures/searchPodcast');
methods.searchList = require('./structures/searchList');
methods.getMovie = require('./structures/getMovie');
methods.getProfile = require('./structures/getProfile');
const baseURL = 'https://letterboxd.com';
class Scraper {
static getHtml(URL) {
return new Promise(async (resolve, reject) => {
axios(URL).then(response => resolve(response.data)).catch((error) => {
reject(`Err ${error.response.status}`);
})
});
}
static async searchMovie(query) {
if (!query || typeof query !== 'string') throw new Error('You must include the "query" element of type "string".');
const html = await this.getHtml(`${baseURL}/search/films/${encodeURI(query.trim())}`);
const $ = await cheerio.load(html);
return methods.searchMovie($, baseURL);
}
static async searchUser(query) {
if (!query || typeof query !== 'string') throw new Error('You must include the "query" element of type "string".');
const html = await this.getHtml(`${baseURL}/search/members/${encodeURI(query.trim())}`);
const $ = await cheerio.load(html);
return methods.searchUser($, baseURL);
}
static async searchPodcast(query) {
if (!query || typeof query !== 'string') throw new Error('You must include the "query" element of type "string".');
const html = await this.getHtml(`${baseURL}/search/episodes/${encodeURI(query.trim())}`);
const $ = await cheerio.load(html);
return methods.searchPodcast($);
}
static async searchList(query) {
if (!query || typeof query !== 'string') throw new Error('You must include the "query" element of type "string".');
const html = await this.getHtml(`${baseURL}/search/lists/${encodeURI(query.trim())}`);
const $ = await cheerio.load(html);
return methods.searchList($, baseURL);
}
static async getMovie(query) {
if (!query || typeof query !== 'string') throw new Error('You must include the "query" element of type "string".');
let url;
const reg = new RegExp((/https?:\/\/letterboxd\.com\/film\/([a-z\d-]+)/g));
if (query.match(reg)) url = query;
else await this.searchMovie(query).then((items) => url = items[0]?.url).catch((error) => console.log(error));
if (!url) return Object();
const html = await this.getHtml(`${encodeURI(url.trim())}`);
const $ = await cheerio.load(html);
return methods.getMovie($);
}
static async getProfile(username) {
if (!username || typeof username !== 'string') throw new Error('You must include the "username" element of type "string".');
const user = username.trim();
const html = await this.getHtml(`${baseURL}/${encodeURI(user)}`);
const $ = await cheerio.load(html);
return methods.getProfile($, baseURL, user);
}
}
module.exports = Scraper;