-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
39 lines (31 loc) · 1.38 KB
/
fetch.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
const chan = require('4chanapi.js');
const axios = require('axios');
async function fetchMediaDetailsFromThreads(boards, keywords) {
let allMediaDetails = [];
for (const board of boards) {
try {
let threads = await chan.threadsWithTopics([board], keywords);
console.log(`Threads found on ${board}:`, threads);
for (let thread of threads) {
let threadData = await axios.get(thread.url);
let posts = threadData.data.posts;
let mediaLinks = await chan.threadMediaLinks(thread.url);
for (let mediaLink of mediaLinks) {
let thumbnailLink = mediaLink.replace(/(\.[\w\d]+)$/, 's.jpg');
let mediaId = mediaLink.match(/\/(\d+)(\.[\w\d]+)$/)[1];
let post = posts.find(p => p.tim === parseInt(mediaId));
let originalFilename = post ? post.filename + post.ext : "Unknown";
allMediaDetails.push({
mediaLink,
thumbnailLink,
originalFilename
});
}
}
} catch (error) {
console.error(`Error fetching data from ${board}:`, error);
}
}
return allMediaDetails;
}
module.exports = fetchMediaDetailsFromThreads;