-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (94 loc) · 2.99 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
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 8000
require('now-env')
const Twitter = require('twitter')
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
})
app.use(function(req, res, next) {
// Website you wish to allow to connect
const allowedOrigins = ['http://localhost:8082', 'https://robipop.io']
const origin = req.headers.origin
if (allowedOrigins.indexOf(origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', origin)
}
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET')
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type')
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true)
// Pass to next layer of middleware
next()
})
app.use(bodyParser.json())
app.get('/timeline', (req, res) => {
const params = { user_id: req.query.user_id, count: req.query.count }
client.get('statuses/user_timeline', params, function(
error,
tweets,
response
) {
if (!error) {
let formatedData = []
let monthNames = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
for (let i = 0; i < tweets.length; i++) {
let tweetFormated = {}
const {
text,
created_at,
user,
retweet_count,
favorite_count,
retweeted,
retweeted_status,
id_str
} = tweets[i]
const { name, screen_name, profile_image_url_https } = user
if (retweeted) {
tweetFormated.link = `https://twitter.com/${
retweeted_status.user.screen_name
}/status/${retweeted_status.id_str}`
} else {
tweetFormated.link = `https://twitter.com/${screen_name}/status/${id_str}`
}
const dateObj = new Date(created_at)
tweetFormated.description = text
tweetFormated.name = name
tweetFormated.author = screen_name
tweetFormated.imgURL = profile_image_url_https
tweetFormated.retweetCount = retweet_count
tweetFormated.favoriteCount = favorite_count
tweetFormated.date = `${dateObj.getHours() || '00'}:${
dateObj.getMinutes() < 10 ? '0' : ''
}${dateObj.getMinutes()} - ${
monthNames[dateObj.getMonth()]
} ${dateObj.getDay()}, ${dateObj.getFullYear()}`
formatedData.push(tweetFormated)
}
res.send({ tweets: formatedData })
}
})
})
app.listen(port, () => {
console.log('We are live on ' + port)
})