-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvidScraper.js
93 lines (87 loc) · 3.09 KB
/
vidScraper.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
const fetch = (...args) =>
import('node-fetch').then(({default: fetch}) => fetch(...args));
async function getVids() {
const finalListings = await getScreeningData();
return finalListings
}
async function getScreeningData() {
// const place = somehow
const url = 'https://vidiotsfoundation.org/wp-json/nj/v1/showtime/listings';
try {
const res = await fetch(url);
const json = await res.json();
const showtimes = json.showtimes;
const listings = getByShowtimes(showtimes);
return listings
} catch (err) {
console.log(`${err} for ${url}`);
}
return []
}
async function getByShowtimes(showtimes,page) {
let cleanedData = [];
for (const show of showtimes) {
const url = `https://vidiotsfoundation.org/wp-json/nj/v1/show/${show.movie_id}`;
try {
const res = await fetch(url);
const json = await res.json();
const screening = json;
const listing = createListing(screening,show);
cleanedData.push(listing);
} catch (err) {
console.log(`${err} for ${url}`);
}
}
return cleanedData
}
function createListing(screening,show) {
const startDate = new Date(show.datetime.substring(0,4),show.datetime.substring(4,6)-1,show.datetime.substring(6,8),show.datetime.substring(8,10),show.datetime.substring(10,12));
try {
return {
type: 'Screening',
startDate,
QA: isQAndA(decodeHtmlCharCodes(screening.content.rendered)),
url: show.purchase_url,
name: decodeHtmlCharCodes(screening.title.rendered).replaceAll('&', '&'),
workingText: decodeHtmlCharCodes(screening.content.rendered),
workPresented: [{
name: decodeHtmlCharCodes(screening.title.rendered).replaceAll('&', '&'),
duration: screening._length,
director: getDirector(screening),
year: getYear(screening),
videoFormat: getFormat(screening),
}]
}
}
catch (err) {
console.log(`${err} for ${show}`);
return {}
}
}
function decodeHtmlCharCodes(str) {
return str.replace(/(&#(\d+);)/g, function(match, capture, charCode) {
return String.fromCharCode(charCode);
});
}
function getFormat(data) {
if(data?.format.length == 0) {
return '';
}
return data?.format && data.format.length === 1 ? data.format[0].name : (data?.format[0]?.name + ' ' + data?.format[1]?.name)
}
function getYear(data) {
if(data._release_date) {
return data._release_date.substring(0,4)
}
else {
return ''
}
}
function getDirector(data) {
let director = data?.director && data.director?.length === 1 ? data.director[0].name : (data?.director[0]?.name + ' & ' + data?.director[1]?.name);
if(director.includes('undefined')){
director = ' ';
}
return director
}
module.exports = getVids;