-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (110 loc) · 4.44 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require('dotenv').config({path:__dirname+'/.env'})
var searchTerm = process.argv[2] || "vim";
var async = require('async');
var util = require('util');
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
const serviceAccount = require(process.env.pwdToServiceFile);
const { TwitterApi } = require('twitter-api-v2');
var chatEmoji = '💬';
var linkEmoji = '🔗';
var shortUrl = require('node-url-shortener');
var client = new TwitterApi({
appKey: process.env[searchTerm + "TwitterAPIKey"],
appSecret: process.env[searchTerm + "TwitterAPIKeySecret"],
accessToken: process.env[searchTerm + "TwitterAccessToken"],
accessSecret: process.env[searchTerm + "TwitterAccessSecret"]
});
var date = new Date();
var prettyDate = date.toLocaleDateString('en-US', {
year: "numeric",
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric'
});
console.log("========================\n\nStarting HN Bot Process for", searchTerm, prettyDate, "\n");
initializeApp({
credential: cert(serviceAccount)
});
const db = getFirestore();
var queue = async.queue(function(storyID, callback) {
var outgoingTweet = "";
(async () => {
try {
storyID = storyID.toString();
const response = await fetch('https://hacker-news.firebaseio.com/v0/item/' + storyID + '.json');
const story = await response.json();
var re = new RegExp("\\b" + searchTerm + "\\b", "gi");
if (story.title.match(re)) {
console.log("Found story on ", searchTerm, story.title);
console.log("Shortening HN Discussion Link", story.id);
} else {
return callback();
}
var storyDoc = await db.collection(searchTerm).doc(storyID).get();
if (storyDoc.exists) {
console.log("Story Has Already Been Tweeted");
return callback();
}
console.log("Shortening HN Link");
var shortHNLink = await shortenURL('https://news.ycombinator.com/item?id=' + storyID);
shortHNLink = shortHNLink.shortened_url;
console.log("Successfully Shortened HN Link as", shortHNLink);
var shortStoryURL = ""
if (!story.url) {
console.log("ASK HN Detected");
outgoingTweet = story.title + "\n" + chatEmoji + ": " + shortHNLink + "\n#HackerNews #" + searchTerm.toUpperCase();
} else {
console.log("Shortening Story Link");
shortStoryURL = await shortenURL(story.url);
outgoingTweet = story.title + "\n" + chatEmoji + " " + shortHNLink + "\n" + linkEmoji + " " + shortStoryURL + "\n#HackerNews #" + searchTerm.toUpperCase();
console.log("Successfully Shortened HN Link as", shortStoryURL);
}
console.log("Tweeting:\n\n", outgoingTweet);
var tweetResponse = await client.v2.tweet(outgoingTweet, {});
console.log("Successfully Tweeted", tweetResponse.data.id);
console.log("Setting Record to firestore");
await db.collection(searchTerm).doc(storyID).set({
dateTweeted:new Date(),
tweetID:tweetResponse.data.id,
urlLink:shortStoryURL,
shortHNLink:shortHNLink,
tweetMessage:outgoingTweet
});
console.log("Successfully Saved Record To DB, Ending Process");
callback();
} catch(err) {
console.log("ERROR in Queue", err);
callback();
}
})();
}, 1);
queue.drain(function () {
console.log("HN Bot for", searchTerm, "Process Complete");
process.exit(0);
});
(async () => {
try {
const response = await fetch("https://hacker-news.firebaseio.com/v0/topstories.json");
const topStories = await response.json();
topStories.forEach(storyID => {
queue.push(storyID);
})
} catch (err) {
console.log("ERROR", err);
process.exit(1);
}
})();
async function shortenURL(urlToShorten) {
return new Promise(function (resolve, reject) {
shortUrl.short(urlToShorten, function (err, url) {
// Shorten Story Link
if (err) {
reject(err);
} else {
resolve(url);
}
})
})
}