-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (50 loc) · 1.98 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
const cheerio = require('cheerio');
const { Elysia } = require('elysia');
const fetch = require('node-fetch');
const server = new Elysia();
const port = 8655; // Change if needed
async function getLastUpdate(username) {
const url = `https://mydramalist.com/profile/${username}`;
console.log(`Scraping URL: ${url}`);
try {
const response = await fetch(url);
const html = await response.text();
const $ = cheerio.load(html);
const list = [];
const selector = '#content > div > div.container-fluid.profile-container > div > div.col-lg-8.col-md-8 > div.row.stats-section > div:nth-child(2) > div > ul > li';
$(selector).first().each(function () {
const activityHtml = $(this).find('.activity').html();
const episodeInfo = activityHtml.split('<div').shift().replace('<strong>', '').replace('</strong>', '');
const lastUpdateTime = activityHtml.split('">').pop().split('</').shift();
const dramaUrl = "https://mydramalist.com" + $(this).find('a').attr('href');
const title = $(this).find('a').attr('title');
const poster = $(this).find('img').attr('src');
list.push({
title,
episode: episodeInfo.replace('Currently watching', 'Episode:'),
lastUpdateTime,
poster,
dramaUrl
});
});
return list;
} catch (error) {
console.log(error);
return [];
}
}
server.get('/data', async (request) => {
const username = request.query.username;
if (!username) {
return { error: 'Username query parameter is required (/data?username=Cyadine)' };
}
try {
const data = await getLastUpdate(username);
console.log("✔ Scraped " + Date());
return data;
} catch (error) {
return { error: 'Internal Server Error' };
}
});
server.listen(port);
console.log(`Server running at https://127.0.0.1:${port}/`);