-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (86 loc) · 2.85 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
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
import PushMeSDK, { Consts } from "@pushme-tgxn/pushmesdk";
import { CronJob } from "cron";
import axios from "axios";
import moment from "moment";
import { JobDefs } from "./config.js";
// get top post from subreddits
const getTopPostsSub = async (subreddit) => {
try {
const response = await axios.get(
`https://old.reddit.com/r/${subreddit}/top/.json?sort=top&t=day`
);
return response.data.data.children;
} catch (error) {
console.error(error);
}
};
const getTopPostsMulti = async (user, multiName) => {
try {
const requestUrl = `https://reddit.com/user/${user}/m/${multiName}/top/.json?sort=top&t=day`;
console.log(requestUrl);
const response = await axios.get(requestUrl);
return response.data.data.children;
} catch (error) {
console.error(error);
}
};
// send push notifications
async function sendLinkPush(client, secret, pushRequest) {
console.log("Sending push notification", pushRequest);
try {
const { pushIdent } = await client.push.pushToTopic(secret, pushRequest);
console.log(`Created Push Ident: ${pushIdent}`);
} catch (error) {
console.error(error.toString());
}
}
async function runJob(jobDef) {
let topPosts = null;
if (jobDef.type === "sub") {
topPosts = await getTopPostsSub(jobDef.sub);
} else if (jobDef.type === "multi") {
topPosts = await getTopPostsMulti(jobDef.user, jobDef.multi);
}
let topPost = null;
for (const post of topPosts) {
const testPostData = post.data;
// console.log(testPostData);
// tests for valid post (image, video, etc)
// return the first one
topPost = testPostData;
break;
}
const backendUrl = jobDef.backendUrl || Consts.BACKEND_URL;
console.log(`Using backend URL: ${backendUrl}`);
const pushmeClient = new PushMeSDK({
backendUrl,
timeout: 10000, // extend this for slow networks/etc
});
// which link to send (comments vs. direct link)
const linkUrl = `https://old.reddit.com/${topPost.subreddit_name_prefixed}/comments/${topPost.id}/`;
console.log(linkUrl);
const dateString = moment().format("MMM Do");
const pushRequest = {
categoryId: Consts.PushCategory.BUTTON_OPEN_LINK,
title: `What's the story for ${dateString}?`,
body: `Top post is from ${topPost.subreddit_name_prefixed} with ${topPost.ups} upvotes.`,
data: {
linkUrl,
// openInstanced: true,
// sendReceipt: true,
},
};
// send to all tokens
for (const token of jobDef.sendtokens) {
await sendLinkPush(pushmeClient, token, pushRequest);
}
}
async function run() {
for (const jobDef of JobDefs) {
const cronSchedule = jobDef.schedule || "*/1 * * * *";
const timeZone = jobDef.timezone || "Australia/Perth";
console.log(`Scheduling cron job for ${cronSchedule} in ${timeZone}...`);
new CronJob(cronSchedule, () => runJob(jobDef), null, true, timeZone);
}
}
run();