-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
198 lines (188 loc) · 6.06 KB
/
utils.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
import dotenv from 'dotenv'
import fetch from 'node-fetch'
import { load } from 'cheerio'
const urlPattern = /^https:\/\/letterboxd\.com\/([a-zA-Z0-9_-]+)\/film\/([a-zA-Z0-9_-]+)\/?(\d*)\/?$/
dotenv.config()
function handleError(msg, st){
return {
message: msg,
response: {status: st}
}
}
function matchURL(data) {
return new Promise((resolve, reject) => {
const url = data.url
const match = url.match(urlPattern);
if(match){
resolve(data)
}
else{
reject(handleError("Bad URL! Couln't match the url in script tag", 400))
}
})
}
function getOpenCloseBraces(scriptTagText){
var openBrace = -1
var closeBrace = -1
for(var i=0; i<scriptTagText.length; i++){
if(scriptTagText[i] == '{'){
openBrace = i
break
}
}
for(var i=scriptTagText.length-1; i>=0; i--){
if(scriptTagText[i] == '}'){
closeBrace = i
break
}
}
return new Promise( (resolve, reject) => {
if(openBrace != -1 && closeBrace != -1)
resolve({scriptTagText, openBrace, closeBrace})
else
reject(handleError("Bad URL! Couldn't JSON in script tag", 400))
})
}
// scrape page and check the script tag
function getScrapedData(response){
return new Promise((resolve, reject) => {
const status = response.status
response.text()
.then(response => {
const $ = load(response)
const scriptTagText = $('script[type="application/ld+json"]').text()
const film_year_scrape = $('.film-title-wrapper > small').text()
if(scriptTagText){
resolve({scriptTagText, film_year_scrape})
}
else{
if(status === 200)
reject(handleError("Bad URL! Couldn't find the script tag", 400))
else
reject(handleError("File not found", 404))
}
})
.catch((error) => {
reject(handleError(error.message || "Couldn't convert resource to html", 520))
})
})
}
function getJson(scriptTagText, openBrace, closeBrace){
return new Promise((resolve, reject) => {
const data = JSON.parse(scriptTagText.slice(openBrace, closeBrace+1))
if(data){
// data.film_year = $('.film-title-wrapper > small').text()
resolve(data)
}
else{
reject(handleError("Bad URL! Couldn't make JSON", 400))
}
})
}
function addImages(response){
const API_KEY = process.env.TMDB_API_KEY
return new Promise((resolve, reject) => {
getWebsite(response.filmURL, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
})
.then((res) => res.text())
.then((res) => {
const $ = load(res)
const retBody = $('body')
const content_type = retBody.attr('data-tmdb-type')
const content_id = retBody.attr('data-tmdb-id')
return new Promise((iresolve, ireject) => {
if(content_type && content_id){
iresolve({content_id, content_type})
}
else{
var lbx_image = $('#backdrop').attr('data-backdrop2x')
if(!lbx_image)
lbx_image = $('#backdrop').attr('data-backdrop')
const images = []
if(lbx_image)
images.push(lbx_image)
response.images = images
resolve(response)
// ireject({
// message: "Couldn't find content id and/or type for TMDB",
// response: {status: 400},
// })
}
})
})
.then(({content_id, content_type}) => {
return getWebsite(`https://api.themoviedb.org/3/${content_type}/${content_id}/images?api_key=${API_KEY}`,
{
method: 'GET',
headers: {
'Accept': 'application/json'
}
})
})
.then((res) => res.json())
.then((res) => {
const IMAGES_BASE_URL = 'https://image.tmdb.org/t/p/original'
const images = []
if(res.backdrops.length > 0){
res.backdrops.forEach((item) => {
images.push(IMAGES_BASE_URL+item.file_path)
})
}
else if(res.posters.length > 0){
res.posters.forEach((item) => {
images.push(IMAGES_BASE_URL+item.file_path)
})
}
response.images = images
resolve(response)
})
.catch((error) => {
reject({
error: true,
message: error.message || "Error occured",
status: error.response ? error.response.status || 404 : 404
})
})
})
}
function getWebsite(req_url, obj){
if(obj)
return fetch(req_url, obj)
return fetch(req_url)
}
function isImage(res){
return new Promise((resolve, reject) => {
if(res.headers.get('content-type') === 'image/jpeg'){
resolve(res)
}
else if(res.headers.get('content-type').startsWith('image/')){
reject({
error: true,
message: "Not a jpeg image",
status: 415,
url: res.url
})
}
else if(res.status === 200){
reject({
error: true,
message: "Not an image",
status: 415,
url: res.url
})
}
else{
reject({
error: true,
message: "File not found",
status: 404,
url: res.url
})
}
})
}
export { matchURL, getOpenCloseBraces, getScrapedData, getJson, addImages, getWebsite, isImage }