-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
36 lines (34 loc) · 1.48 KB
/
main.ts
File metadata and controls
36 lines (34 loc) · 1.48 KB
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
import { REDDIT_RSS, CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET } from "./global_constants";
import Parse from "rss-parser";
import { TwitterApi } from 'twitter-api-v2';
import { checkIfRedditPostMatchesToday } from "./checkIfRedditPostMatchesToday";
import { formatDataForTweet} from "./formatDataForTweet";
import { logWhenTweetIsSuccessful } from "./logWhenTweetIsSuccessful";
import { logNoChanges} from "./logNoChanges";
import { logAndThrowError } from "./logAndThrowError";
const parser: any = new Parse();
const client = new TwitterApi({
appKey: CONSUMER_KEY,
appSecret: CONSUMER_SECRET,
accessToken: ACCESS_TOKEN,
accessSecret: ACCESS_TOKEN_SECRET,
});
(async () => {
try {
const resp: any = await parser.parseURL(REDDIT_RSS);
const urlId = resp.items[0].link.slice(resp.items[0].link.length-8, resp.items[0].link.length-1);
const postId: string = resp.items[0].id.slice(resp.items[0].id.length-7);
const postIsValid: boolean = !(urlId === postId); // post is valid if Ids don't match, this excludes comments from being tweeted and only includes actual result posts
if (resp &&
resp.items &&
checkIfRedditPostMatchesToday(new Date(resp.items[0].pubDate)) &&
postIsValid) {
await client.v1.tweet(formatDataForTweet(resp));
logWhenTweetIsSuccessful();
} else {
logNoChanges();
}
} catch (error) {
logAndThrowError(error);
}
})()