-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
214 lines (190 loc) · 5.99 KB
/
server.js
File metadata and controls
214 lines (190 loc) · 5.99 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require("dotenv").config();
// Env Variable
const port = 3000;
// Libraries
const bodyParser = require("body-parser"); // Library for parsing data
const cors = require("cors"); // Library for handling access headers
const { Autohook } = require("twitter-autohook");
const morgan = require("morgan");
const axios = require("axios");
const fs = require("fs");
const mongoose = require("mongoose");
// Modules
const tweetHandler = require("./modules/tweet");
const citation = require("./modules/citation");
const tester = require("./modules/tester");
const IP = require("./modules/ip");
// Server
const express = require("express"); // Framework for Node
const app = express(); // Establishing Express App
morgan("tiny");
app.use(cors()); // Cors to Handle Url Authentication
app.use(bodyParser.json()); // Using Body Parser
// app.set("jwtTokenSecret", ""); // JWT Secret
const server = app.listen(port); // Set Port
mongoose.set("useNewUrlParser", true);
mongoose.set("useUnifiedTopology", true);
mongoose
.connect(`mongodb://${process.env.MONGO_URL}`)
.then((con) => {
console.log("db connected");
app.set("con", con);
})
.catch((err) => {
console.log(err);
throw err;
});
let webhookSubscribe;
// Twitter Api
let twitterWebhook = async function () {
try {
console.log("Starting Webhook ...");
let webhook = new Autohook({
token: process.env.ACCESS_TOKEN,
token_secret: process.env.ACCESS_TOKEN_SECRET,
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
env: process.env.TWITTER_WEBHOOK_ENV,
port: 1337,
});
// Removes existing webhooks
await webhook.removeWebhooks();
// Listens to incoming activity
webhook.on("event", (event) => handleNewWebHook(event));
// Starts a server and adds a new webhook
await webhook.start();
// Subscribes to a user's activity
webhookSubscribe = await webhook.subscribe({
oauth_token: process.env.ACCESS_TOKEN,
oauth_token_secret: process.env.ACCESS_TOKEN_SECRET,
});
let ip = await IP.checkGCP();
axios.post(process.env.DISCORD_SERVER_URL, {
content: `Webhook Running From ${ip}`,
username: "Who Said This Bot",
avatar_url:
"https://pbs.twimg.com/profile_images/1255489352714592256/kICVOCy-_400x400.png",
});
} catch (err) {
console.log("Error in starting server", err);
let ip = await IP.checkGCP();
axios.post(process.env.DISCORD_SERVER_URL, {
content: `<@285449811975733248> <@491917392646111243> <@179264835618471936> <@367812757485125642> <@311891570570035200> Webhook Could not Start because of ${err} From ${ip}`,
username: "Who Said This Bot",
avatar_url:
"https://pbs.twimg.com/profile_images/1255489352714592256/kICVOCy-_400x400.png",
});
setTimeout(() => {
fs.writeFileSync(
"./lastRestart.json",
JSON.stringify({ time: new Date() })
);
}, 60 * 1000); // Wait 1 Min
}
};
let main = async function () {
await twitterWebhook();
await tweetHandler.getMentionTimeline();
setTimeout(async () => {
await tweetHandler.getMentionTimeline();
}, 60 * 15 * 1000);
setTimeout(() => {
fs.writeFileSync(
"./lastRestart.json",
JSON.stringify({ time: new Date() })
);
}, 3600 * 1000);
};
main();
let handleNewWebHook = function (event) {
console.log("handleNewWebHook -> event", event);
// let fs = require("fs");
// let json = JSON.stringify(event, null, 2);
// fs.writeFile("passiveRetweet.json", json, function (err) {
// if (err) throw err;
// console.log("Saved!");
// });
if (event.tweet_create_events) {
let tweet = event.tweet_create_events[0];
tweetHandler.tweetClassify(tweet, false);
}
};
// Create IPFS instance
// Routing
// Get Requests
// Testing Routes
app.get("/dbCheckTweet", async function (req, res) {
let database = require("./modules/database");
let tweetId = req.query.tweetId;
let output = await database.checkTweetCache(tweetId);
res.status(200).json({
output: output,
});
});
app.get("/nlpOutput", async function (req, res) {
let tweetId = req.query.tweetId;
mongoose.connection.db.collection("nlpSchemas", async function (
err,
collection
) {
if (err) {
console.log(err);
res.send(err);
}
//console.log("schema accessed");
console.log(tweetId);
let response = await collection.findOne({ tweetId: tweetId });
//console.log(response);
if (!response) {
res
.status(200)
.json({ output: "Sorry not output found for this tweet id" });
} else {
fs.writeFileSync("./nlpOutput.txt", response.nlpOutput);
res.sendFile(require("path").join(__dirname, "./nlpOutput.txt"));
}
});
});
// app.options("/", function (req, res) {
// res.setHeader("Access-Control-Allow-Origin", "*");
// res.setHeader("Access-Control-Allow-Methods", "*");
// res.setHeader("Access-Control-Allow-Headers", "*");
// res.end();
// });
app.get("/", async function (req, res, next) {
//res.set("Access-Control-Allow-Origin", "*");
//res.set("Access-Control-Allow-Methods", "GET, POST");
try {
let lastRestart = JSON.parse(fs.readFileSync("./lastRestart.json"));
res.status(200).json({
webhookSubscribeStatus: webhookSubscribe,
serverStatus: "Server is On!",
lastRestart: lastRestart,
});
} catch (err) {
res.status(200).json({
webhookSubscribeStatus: webhookSubscribe,
serverStatus: "Server is On!",
err: err,
});
}
});
app.get("/addTweetToTest", async function (req, res) {
let tweetId = req.query.tweetId;
let idName = req.query.testName;
let type = req.query.type;
let response = await tester.createTest(tweetId, idName, type);
res.status(200).json({
response: response,
});
});
// Post Requests
// app.post("/newTweets", async function (req, res) {
// let body = req.body;
// let citation = await tweet.handleNewTweet(body);
// console.log(body);
// res.status(200).json({
// output: "Test",
// citation: citation,
// });
// });