-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (61 loc) · 1.71 KB
/
server.js
File metadata and controls
71 lines (61 loc) · 1.71 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
import express from 'express';
import morgan from 'morgan';
import cors from 'cors';
import errorHandler from 'middleware-http-errors';
import { TwitterApi } from 'twitter-api-v2';
import dotenv from 'dotenv';
dotenv.config();
const twitterClient = new TwitterApi({
appKey: process.env.TWITTER_CONSUMER_KEY,
appSecret: process.env.TWITTER_CONSUMER_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN,
accessSecret: process.env.TWITTER_ACCESS_SECRET
});
const readOnlyClient = twitterClient.readOnly;
const PORT = 1000;
// Fabrizo
const AUTHOR_ID = 330262748;
// Set up web app, use JSON
const app = express();
app.use(express.json());
// Use middleware that allows for access from other domains
app.use(cors());
// for logging errors
app.use(morgan('dev'));
// handles errors nicely
app.use(errorHandler());
app.get('/tweets', async (req, res) => {
let searchTerm = req.query.searchTerm;
const search = searchTerm.split(" ");
let tweetURLs = [];
const timeLine = await readOnlyClient.v2.userTimeline(AUTHOR_ID, { exclude: 'replies' });
await timeLine.fetchLast(500);
for (const tweet of timeLine.tweets) {
let hasTerms = true;
for (const word of search) {
if (!tweet.text.toLowerCase().includes(word.toLowerCase())) {
hasTerms = false;
break;
}
}
if (hasTerms) {
console.log(tweet.text);
tweetURLs.push(tweet.id);
}
}
res.json(tweetURLs);
});
// start server
const server = app.listen(
parseInt(PORT),
process.env.IP,
() => {
console.log(
`⚡️ Server listening on port ${PORT}`
);
}
);
// For coverage, handle Ctrl+C gracefully
process.on('SIGINT', () => {
server.close(() => console.log('Shutting down server gracefully.'));
});