-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
79 lines (71 loc) · 2.23 KB
/
main.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
import Snoowrap from "snoowrap";
import { downloadSavedPosts } from "./download.js";
import fs from "fs/promises";
const config = JSON.parse(await fs.readFile("config.json", "utf-8"));
const r = await new Snoowrap(config.bot);
const parsePost = (post) => {
try {
let res = {
subreddit: post.subreddit.display_name,
title:
post.title +
(Number(post.created_utc+"000") > 1671372484781 ? "_" + post.created_utc : ""),
author: post.author.name,
link: post.permalink,
time: Date.now(),
downloads: [],
};
if (post?.crosspost_parent_list?.[0]) {
post = post.crosspost_parent_list[0];
}
if (post?.media?.reddit_video?.fallback_url) {
res.downloads = [post.secure_media.reddit_video.fallback_url];
} else if (post.preview && post.preview.reddit_video_preview)
res.downloads = [post.preview.reddit_video_preview.fallback_url];
else if (post.gallery_data)
for (let i of post.gallery_data.items)
res.downloads.push(
post.media_metadata[i.media_id].s.u
? post.media_metadata[i.media_id].s.u
: post.media_metadata[i.media_id].s.gif
);
else if (post.domain == "redgifs.com")
res.downloads = [
post.media.oembed.thumbnail_url
.replace("jpg", "mp4")
.replace("png", "mp4"),
];
else if (post.url) res.downloads = [post.url];
else {
res.downloads = "https://notFound.notFound/notFound.null";
}
return res;
} catch (e) {
console.log(post);
//throw new Error(e);
return null;
}
};
const generateParsedSaves = async () => {
console.log("[", new Date().toISOString(), "]", "Fetching posts!");
let mode = config.mode ? config.mode : "saved";
let posts;
let limit = config.limit ? { limit: config.limit } : { limit: Infinity };
if (mode == "saved") {
posts = await r.getMe().getSavedContent(limit);
} else if (mode == "subreddit") {
posts = await r.getSubreddit(config.subreddit).getNew(limit);
}
let svd = [];
for (let i of posts) {
let p = parsePost(i);
if(p){
svd.push(p);
}
}
return svd;
};
while (true) {
await downloadSavedPosts(await generateParsedSaves());
await new Promise((r) => setTimeout(r, config.interval));
}