-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (71 loc) · 1.91 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
/**
* Created by Raymon on 4-12-2014.
*/
var express = require('express'),
app = express(),
Twit = require('twit'),
Firebase = require('firebase'),
TwUsername = new Firebase('https://nodejs-express-demo.firebaseio.com/twitter/username')
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
/**
* onComplete function to check error
* @param error
*/
var onComplete = function (error) {
if (error) {
console.log('Synchronization failed')
} else {
console.log('Synchronization succeeded')
}
}
/**
* Firebase api
*/
function saveObjTwitter(req, obj, promise) {
console.log('Data is saved to Firebase')
TwUsername.child(req).update(obj, promise)
}
/**
* Twitter API
*
*/
var T = new Twit({
consumer_key: 'CONSUMER_KEY',
consumer_secret: 'CONSUMER_SECRET',
access_token: 'ACCESS_TOKEN',
access_token_secret: 'ACCESS_TOKEN_SECRET'
})
/**
* Gets and save twitter info from Twitter API when a request is done to for example:
* localhost:3000/twitter/username
* It returns a json object
*/
app.get('/twitter/:name', function (req, res) {
T.get('search/tweets', { q: req.params.name, count: 1 }, function (err, data, response) {
var userProfile, twitterSavedObj
userProfile = data.statuses[0].user
twitterSavedObj = {
followers: userProfile.followers_count,
screen_name: userProfile.screen_name,
logo: userProfile.profile_image_url,
banner: userProfile.profile_banner_url,
profile_link_color: userProfile.profile_link_color,
profile_text_color: userProfile.profile_text_color
}
saveObjTwitter(req.params.name, twitterSavedObj, onComplete)
TwUsername.on(
'value',
function (snapshot) {
console.log('Got the data')
res.send(snapshot.val())
},
function (errorObject) {
console.log('The read failed: ' + errorObject.code)
}
)
})
})