-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitterAPI.js
More file actions
81 lines (74 loc) · 2.34 KB
/
Copy pathtwitterAPI.js
File metadata and controls
81 lines (74 loc) · 2.34 KB
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
const axios = require('axios');
const { TWITTER_TOKEN } = require("./config");
/**
* Functionality for Getting my 100 Days of Code Tweets
*
* TODO: look into adding pagination so that I'm only fetching
* 20 or so tweets at a time. Right now tweet count defaults to 50.
*/
class TwitterAPI {
constructor(user = "1398406010") {
this.userId = user;
this.baseUrl = "https://api.twitter.com/2";
// Tags idea may be lame... the thought is to create a few categories,
// that way the blog menu could be used to get different groups of pre-set
// texts... I'll start it this way and see how it goes.
this.tags = {
hundredDays: [
"100DaysOfCode", "100daysofcode", "100Daysofcode", "100DaysofCode"
],
javascript: [
"JavaScript", "javascript", "Javascript"
]
};
}
/** Gets Tweets by User, excludes replies and retweets.
* Accept tweetcount, but defaults to 50 tweets.
* */
async getMyTweets(tweetCount = "50") {
let options = {
method: 'GET',
url: `${this.baseUrl}/users/${this.userId}/tweets`,
params: {
'exclude': 'retweets,replies',
'tweet.fields': 'entities',
'max_results': tweetCount
},
headers: {
'Authorization': "Bearer " + TWITTER_TOKEN,
}
};
try {
const response = await axios(options);
return response.data.data;
} catch (e) {
console.error("Error: TwitterAPI.getMyTweets");
}
}
/* I haven't found a way to filter this on the twitter server
using params in the get request, but that would be preferred.*/
async filterByHashtag(tag) {
if (!tag in this.tags) return "Hashtag not present.";
let tagList = new Set(this.tags[tag]);
let tweetList = await this.getMyTweets();
console.log("tweetList: ", tweetList);
const filteredTweets = [];
for (let tweet of tweetList) {
if ("entities" in tweet && "hashtags" in tweet.entities) {
for (let { tag } of tweet.entities.hashtags) {
if (tagList.has(tag)) {
filteredTweets.push(tweet);
break;
}
}
}
}
return filteredTweets;
}
}
// async function thing() {
// const myTwitter = new TwitterAPI();
// let hundredDaysTweets = await myTwitter.filterByHashtag("hundredDays");
// console.log(hundredDaysTweets);
// }
module.exports = TwitterAPI;