-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-twitter.js
55 lines (47 loc) · 1.6 KB
/
get-twitter.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
const axios = require('axios');
var fs = require('fs')
const USERNAMES = ['openclimatefix', 'jack_kelly']
function fetchFollowersForUsername(username, bearerToken) {
const FILENAME = `data/twitter-followers-${username}.json`
axios.get(`https://api.twitter.com/1.1/users/show.json?screen_name=${username}`, {
headers: {
Authorization: `Bearer ${bearerToken}`
}
})
.then(response => {
console.log(response.data.followers_count)
fs.readFile(FILENAME, function (err, data) {
if (err) throw err;
var output = JSON.parse(data)
output.push({
x: new Date(),
y: response.data.followers_count
})
fs.writeFile(FILENAME, JSON.stringify(output), function (err) {
if (err) throw err;
console.log(`Data was appended to file for user ${username}!`);
});
})
})
.catch(error => {
console.log(error);
});
}
axios.post('https://api.twitter.com/oauth2/token', null, {
auth: {
username: process.env.TWITTER_API_KEY,
password: process.env.TWITTER_API_SECRET
},
params: {
'grant_type': 'client_credentials',
}
})
.then(response => {
const access_token = response.data.access_token;
for (username of USERNAMES) {
fetchFollowersForUsername(username, access_token)
}
})
.catch(error => {
console.log(error);
});