-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch-happenings.mjs
78 lines (65 loc) · 2.03 KB
/
fetch-happenings.mjs
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
import dotenv from 'dotenv';
dotenv.config();
import snoowrap from 'snoowrap';
import fs from 'fs';
import { parse } from 'marked';
import { Feed } from 'feed';
const { CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN } = process.env;
console.log(`What's happening in Singapore?!?`);
const r = new snoowrap({
userAgent: 'whathappen-sg/1.0',
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
refreshToken: REFRESH_TOKEN,
});
const results = await r.getSubreddit('singapore').search({
query: `what's happening flair:news`,
time: 'month',
sort: 'relevance',
});
const happeningResult = results.find((result) =>
result.title.toLowerCase().includes('happening'),
);
if (!happeningResult) {
throw new Error('No happenings found');
}
fs.writeFileSync('happenings.md', happeningResult.selftext);
console.log('happenings.md written');
const html = parse(happeningResult.selftext);
fs.writeFileSync('happenings.html', html);
console.log('happenings.html written');
const feed = new Feed({
title: 'What Happen SG',
description:
'Latest "What\'s Happening in MONTH YEAR" thread content from /r/singapore subreddit.',
id: 'https://cheeaun.github.io/whathappen-sg/',
link: 'https://cheeaun.github.io/whathappen-sg/',
language: 'en',
generator: 'cheeaun/whathappen-sg',
});
feed.addItem({
title: happeningResult.title,
id: happeningResult.url,
link: happeningResult.url,
content: html,
date: new Date(),
author: [
{
name: 'whathappen-sg',
},
],
});
fs.writeFileSync('feed.rss', feed.rss2());
fs.writeFileSync('feed.atom', feed.atom1());
fs.writeFileSync('feed.json', feed.json1());
console.log('feed.* files written');
const text = `## ${happeningResult.title} ([thread](${happeningResult.url}))
${happeningResult.selftext.replace(/\B# /g, '### ')}`;
const readmeContent = fs
.readFileSync('README.md', 'utf8')
.replace(
/<!-- START HAPPENING -->.*<!-- END HAPPENING -->/s,
`<!-- START HAPPENING -->\n${text}\n<!-- END HAPPENING -->`,
);
fs.writeFileSync('README.md', readmeContent);
console.log('README.md written');