-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (96 loc) · 3.19 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const express = require('express');
const app = express();
const cors = require('cors');
const axios = require('axios');
const convert = require('xml-js');
const puppeteer = require('puppeteer');
const path = require('path');
const rateLimit = require('express-rate-limit');
const port = 8081;
const limiter = rateLimit({
windowMs: 1000,
max: 1
});
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'temp')));
app.use(limiter);
const getPosterUrl = html => {
const pattern = /(?<=src=")(.*)(?=")/;
return html.match(pattern)[0];
};
const getRssFeed = async (username, count) => {
try {
const url = `https://letterboxd.com/${username}/rss/`;
const res = await axios.get(url, { validateStatus: () => true});
if (res.status === 404) {
return { error: "Username not found" };
} else {
const obj = convert.xml2js(res.data, { compact: true, spaces: 4 });
const items = obj.rss.channel.item;
const filmsOnly = items.filter(item => item['letterboxd:watchedDate']).slice(0, count);
const mappedFilms = filmsOnly.map(film => {
return {
title: film['letterboxd:filmTitle'],
watchedDate: film['letterboxd:watchedDate']._text,
poster: getPosterUrl(film.description._cdata)
}
});
return mappedFilms;
}
} catch (err) {
if (err) {
console.log(err);
return [];
}
}
return [];
}
app.get('/', async (req, res) => {
res.send("hello there");
});
app.get('/grid', async (req, res) => {
const data = await getRssFeed(req.query.username, req.query.count);
console.log(data);
if (data instanceof Array) {
res.json({
data: data,
success: true
});
} else {
res.json({
data: [],
success: false,
message: data.message || 'Something went wrong'
})
}
});
app.post('/image', async (req, res) => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent('<html><p>hello</p>' + req.body.node + '</html>');
await page.waitForSelector('.grid');
const element = await page.$('.grid');
await page.evaluate(async () => {
const selectors = Array.from(document.querySelectorAll("img"));
await Promise.all(selectors.map(img => {
if (img.complete) return;
return new Promise((resolve, reject) => {
img.addEventListener('load', resolve);
img.addEventListener('error', reject);
});
}));
});
const content = await page.content();
await element.screenshot({path: 'temp/grid.jpg'});
await browser.close();
const filePath = path.join(__dirname, 'temp', 'grid.jpg');
res.download(filePath);
} catch (e) {
console.log(e);
}
});
const server = app.listen(process.env.PORT || port, () => {
console.log(`server is listening on port ${process.env.PORT || port}!`);
});