-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit-news.js
124 lines (95 loc) Β· 2.89 KB
/
reddit-news.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
const FREE_PAID_REGEX = /\[\s*(free|paid)\s*\]/i;
const url = new URL('https://www.reddit.com/r/jailbreak/search.json');
url.searchParams.append('q', 'subreddit:jailbreak (flair:Release OR flair:Update OR flair:Upcoming OR flair:News)');
url.searchParams.append('restrict_sr', 'on');
url.searchParams.append('t', 'month');
if (!existsSync('reddit-news')) {
mkdirSync('reddit-news');
}
await Promise.all([
generateNews('relevance'),
generateNews('new')
]);
async function generateNews(sortBy) {
const sortByCreated = sortBy !== 'new';
const posts = [];
url.searchParams.set('sort', sortBy);
const response = await fetch(url);
const json = await response.json();
const items = json.data?.children ?? [];
for (const { data } of items) {
validatePost(data);
const thumbnail = getThumbnail(data);
const title = cleanupTitle(data.title);
const tags = createTags(data.title, data.link_flair_css_class);
posts.push({
title,
url: `https://www.reddit.com${data.permalink}`,
thumbnail,
tags,
created: sortByCreated ? data.created_utc : undefined
});
}
if (sortByCreated) {
posts.sort((a, b) => b.created - a.created);
posts.forEach(post => delete post.created);
}
writeToFile(posts, sortBy);
}
function validatePost(post) {
if (!post.permalink || post.permalink.length === 0 || !(new URL(`https://www.reddit.com${post.permalink}`))) {
throw new Error('Post permalink missing, did they change the JSON?');
}
if (!post.link_flair_css_class || post.link_flair_css_class.length === 0) {
throw new Error('Post flair missing, did they change the JSON?');
}
if (!post.title || post.title.length === 0) {
throw new Error('Post title missing, did they change the JSON?');
}
}
function getThumbnail(post) {
let thumbnail;
if (post.thumbnail && post.thumbnail != 'nsfw') {
if (post.preview?.images[0]?.source) {
thumbnail = post.preview.images[0].source.url;
}
else if (post.media_metadata) {
const entries = Object.values(post.media_metadata);
for (const entry of entries) {
if (entry.e === 'Image' && entry.s?.u) {
thumbnail = entry.s.u;
}
}
} else {
try {
// check that it is a valid URL
new URL(post.thumbnail);
thumbnail = post.thumbnail;
} catch { }
}
}
return thumbnail ?? null;
}
function cleanupTitle(title) {
let cleaned = title.trim();
let i = 0;
while (cleaned.startsWith('[') && i < 5) {
cleaned = cleaned
.substring(cleaned.indexOf(']') + 1)
.trimStart();
i++;
}
return cleaned;
}
function createTags(title, postTag) {
const match = title.match(FREE_PAID_REGEX);
return match ? `${postTag},${match[1].toLowerCase()}` : postTag;
}
function writeToFile(posts, sort) {
const filename = `reddit-news/${sort}.json`;
writeFileSync(filename, JSON.stringify({
data: posts
}));
console.log(`Written news sorted by ${sort} to ${filename}`);
}