-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterHelper.js
170 lines (158 loc) · 4.75 KB
/
twitterHelper.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class TwitterHelper {
constructor() {
this.config = {
version: '1.1', // version '1.1' is the default (change for v2)
extension: true, // true is the default (this must be set to false for v2 endpoints)
// for app authentication
bearer_token: process.env.Twitter_Bearer_Token,
// for user authentication: not implemented
// consumer_key: process.env.REACT_APP_Twitter_Consumer_Key,
// consumer_secret: process.env.REACT_APP_Twitter_Consumer_Secret,
// access_token_key: process.env.REACT_APP_Twitter_Access_Token_Key,
// access_token_secret: process.env.REACT_APP_Twitter_Access_Token_Secret,
}
}
async getClient() {
if (this.client) return this.client
const Twitter = require('twitter-lite')
const appClient = new Twitter(this.config)
const isApp = !!this.config.bearer_token
if (isApp) {
this.client = appClient
return this.client
}
// user authentication
throw new Error('Not implemented')
}
/**
* @name getFollower
* @summary get Twitter follower information
*
* @param {String} userHandle user handle
* @param {String} followerHandle follower handle
*
* @returns {Boolean}
*/
async getFollower(userHandle, followerHandle) {
await this.getClient()
const params = {
source_screen_name: userHandle,
target_screen_name: followerHandle,
}
try {
const { relationship = {} } = await this.client
.get('friendships/show', params)
return relationship.target || {}
} catch (err) {
throw new Error(!err.errors
? err
: this.joinTwitterErrors(err)
)
}
}
/**
* @name getAllFollowerIds
* @summary get Twitter list of user IDs (not Twitter handles) of followers
*
* @param {String} userHandle
*
* @returns {Boolean}
*/
async getAllFollowerIds(userHandle = '') {
await this.getClient()
const limit = 5000
const params = {
count: limit,
screen_name: userHandle,
stringify_ids: true,
}
const followers = []
let result = {}
try {
do {
result = await this.client
.get('followers/ids', params)
params.cursor = result.next_cursor
followers.push(...result.ids)
} while (result.ids.length === limit)
} catch (err) {
throw new Error(!err.errors
? err
: this.joinTwitterErrors(err)
)
}
return followers
}
async getFollowersList(userHanlde, count = 200, cursor = -1, skipStatus = true, include_user_entities = false) {
await this.getClient()
const params = {
count,
cursor,
include_user_entities,
screen_name: userHanlde,
skipStatus,
}
try {
const result = await this.client
.get('followers/list', params)
return result || {}
} catch (err) {
throw new Error(!err.errors
? err
: this.joinTwitterErrors(err)
)
}
}
/**
* @name getTweetById
* @summary retrieve a tweet with full text by tweet ID
*
* @param {String} id
*
* @returns {Object}
*/
async getTweetById(id) {
// authenticate
await this.getClient()
const params = { id, tweet_mode: 'extended' }
try {
const result = await this.client
.get('statuses/show', params)
return result
} catch (err) {
throw new Error(!err.errors
? err
: this.joinTwitterErrors(err)
)
}
}
/**
* @name getUser
* @summary get Twitter user data
*
* @param {String} handleOrId
*
* @returns {Object}
*/
async getUser(handleOrId) {
await this.getClient()
const params = { 'screen_name': handleOrId }
try {
const result = await this.client
.get('users/show', params)
return result
} catch (err) {
throw new Error(!err.errors
? err
: this.joinTwitterErrors(err)
)
}
}
joinTwitterErrors(err) {
return err.message || (err.errors || [])
.map(e => e.message)
.filter(Boolean)
.join(' ')
}
}
export default new TwitterHelper()