-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.js
84 lines (74 loc) · 2.59 KB
/
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
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
const https = require("https");
const { consumerKey, consumerSecret } = require("./secrets.json");
module.exports.getToken = function() {
return new Promise(function(resolve, reject) {
let creds = `${consumerKey}:${consumerSecret}`;
let encodedCreds = Buffer.from(creds).toString("base64");
const options = {
host: "api.twitter.com",
path: "/oauth2/token",
method: "POST",
headers: {
"Content-Type":
"application/x-www-form-urlencoded;charset=UTF-8",
Authorization: `Basic ${encodedCreds}`
}
};
const cb = function(response) {
if (response.statusCode != 200) {
console.log("Response status: ", response.statusCode);
reject(response.statusCode);
}
let body = "";
response.on("data", chunk => {
body += chunk;
});
response.on("end", () => {
let parsedBody = JSON.parse(body);
resolve(parsedBody.access_token);
});
};
const req = https.request(options, cb);
req.end("grant_type=client_credentials");
});
};
module.exports.getTweets = function(bearerToken, screenName) {
return new Promise((resolve, reject) => {
const options = {
host: "api.twitter.com",
path: `/1.1/statuses/user_timeline.json?screen_name=${screenName}&tweet_mode=extended`,
method: "GET",
headers: {
Authorization: "Bearer " + bearerToken
}
};
const cb = function(response) {
if (response.statusCode != 200) {
reject(response.statusCode);
}
let body = "";
response.on("data", chunk => {
body += chunk;
});
response.on("end", () => {
const parsedBody = JSON.parse(body);
console.log("Get tweets response body: ", parsedBody);
resolve(parsedBody);
});
};
const req = https.request(options, cb);
req.end();
});
};
module.exports.filterTweets = function(tweets) {
const filteredTweets = [];
for (let i = 0; i < tweets.length; i++) {
let obj = {};
if (tweets[i].entities.urls.length === 1) {
obj.text = tweets[i].full_text.replace(/(https?:\/\/[^\s]+)/g, "");
obj.href = tweets[i].entities.urls[0].url;
filteredTweets.push(obj);
}
}
return filteredTweets;
};