-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.js
71 lines (63 loc) · 1.77 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
require("dotenv").config();
const Twitter = require("twitter");
const Octokit = require("@octokit/rest");
const wrapAnsi = require("wrap-ansi");
const { formatDistanceStrict } = require("date-fns");
const {
GIST_ID: gistId,
TWITTER_USER: twitterHandle,
TWITTER_CONSUMER_KEY: consumerKey,
TWITTER_CONSUMER_SECRET: consumerSecret,
TWITTER_ACCESS_TOKEN_KEY: accessTokenKey,
TWITTER_ACCESS_TOKEN_SECRET: accessTokenSecret,
GH_TOKEN: githubToken
} = process.env;
const twitter = new Twitter({
consumer_key: consumerKey,
consumer_secret: consumerSecret,
access_token_key: accessTokenKey,
access_token_secret: accessTokenSecret
});
const octokit = new Octokit({
auth: `token ${githubToken}`
});
async function main() {
const timeline = await twitter.get("statuses/user_timeline", {
screen_name: twitterHandle,
count: 1,
trim_user: 1,
exclude_replies: true
});
const tweet = timeline[0];
await updateGist(tweet);
}
async function updateGist(tweet) {
let gist;
try {
gist = await octokit.gists.get({ gist_id: gistId });
} catch (error) {
console.error(`Unable to get gist\n${error}`);
}
// Get original filename to update that same file
const filename = Object.keys(gist.data.files)[0];
const parsedDate = new Date(tweet.created_at);
const timeAgo = formatDistanceStrict(parsedDate, new Date());
try {
await octokit.gists.update({
gist_id: gistId,
files: {
[filename]: {
filename: `@${twitterHandle} - ${timeAgo} ago | ❤ ${
tweet.favorite_count
} | 🔁 ${tweet.retweet_count}`,
content: wrapAnsi(tweet.text, 62, { hard: true })
}
}
});
} catch (error) {
console.error(`Unable to update gist\n${error}`);
}
}
(async () => {
await main();
})();