-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (45 loc) · 1.97 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
require('dotenv').config()
const http = require('http');
const url = require('url');
const express = require("express");
const { TwitterApi } = require('twitter-api-v2');
const PORT = process.env.PORT || 3001;
const app = express();
const config = {
headers: {
'Authorization': `Bearer ${process.env.TWITTER_BEARER_TOKEN}`,
}
}
//app.use(express.static(path.join(__dirname, 'build')));
const appOnlyClient = new TwitterApi(process.env.TWITTER_BEARER_TOKEN);
app.get("/api", async (req, res) => {
res.json({ message: "Hello from Express"});
});
app.get("/api/twitterCounts", async (req, res) => {
try {
res.setHeader('Access-Control-Allow-Credentials', true)
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT')
res.setHeader(
'Access-Control-Allow-Headers',
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
)
const setDate = req.query.date + "T12:00:00Z"
const twitterQuery =
'((' + req.query.name + ') (bullpen OR warming OR "getting loose" OR stretching OR "started throwing" OR "done for the day" OR pitches OR "pitch count") OR '+
'(' + req.query.team + ') (bullpen OR warming OR "getting loose" OR stretching OR "started throwing" OR "done for the day" OR pitches OR "pitch count")) ' +
'is:verified'
const response = await appOnlyClient.v2.tweetCountRecent(twitterQuery, {start_time: setDate})
const averageTweets = response.meta.total_tweet_count/response.data.length
const recentHourTweets = response.data[response.data.length-1]
res.json({
message: "Twitter request sent",
data: {data: response.data, recentHourTweets: recentHourTweets.tweet_count, averageTweets: averageTweets }});
} catch (e) {
console.log(e)
res.json({ message: "Hello from Express"});
}
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});