-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
170 lines (136 loc) · 4.2 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
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
const Twit = require('twit');
const moment = require('moment');
const {
createProgressBar,
createQueue,
} = require('api-toolkit');
const getMentions = require('./helpers/getMentions');
const getMentionedBy = require('./helpers/getMentionedBy');
const getFollowing = require('./helpers/getFollowing');
const getFollowers = require('./helpers/getFollowers');
const { getRateLimits, setRateLimitOnQueue } = require('./utils/rateLimits');
const paginate = require('./utils/paginate');
const convertUserIdsToHandles = require('./helpers/convertUserIdsToHandles');
// Allows us to check 180/ 15 minutes. Let's check at half that rate just in cases
const RATE_LIMIT_AUTO_FETCH_INTERVAL = 15 * 60 * 1000 / 180 * 2;
const ON_RATE_LIMIT_TIMEOUT = RATE_LIMIT_AUTO_FETCH_INTERVAL;
// how many pending requests can we have per endpoint
const MAX_CONCURRENT_FETCH = 2;
module.exports = class TwitterToolkit {
constructor(opts) {
// because constructor can't be async
this.init(opts);
}
async init({
autoFetchRateLimits = true,
showProgressBar = true,
auth: {
consumerKey,
consumerSecret,
accessToken,
accessTokenSecret,
},
}) {
let readyResolver;
// listen to this for ready
this.ready = new Promise((res) => {
readyResolver = res;
});
this.queues = {};
// holds rate limits
this.rateLimits = {};
this.client = new Twit({
consumer_key: consumerKey,
consumer_secret: consumerSecret,
access_token: accessToken,
access_token_secret: accessTokenSecret,
});
this.paginate = paginate.bind(null, this);
this.getRateLimits = getRateLimits.bind(null, this);
this.setRateLimitOnQueue = setRateLimitOnQueue.bind(null, this);
this.initHelpers();
if (autoFetchRateLimits) {
this.initRateLimitsAutoFetch();
}
if (showProgressBar) {
this.progressBar = createProgressBar({
queues: this.queues,
});
}
this.rateLimits = await this.getRateLimits();
readyResolver();
}
createQueue(url) {
this.queues[url] = createQueue({
maxConcurrent: MAX_CONCURRENT_FETCH,
retry: true,
maxRetries: 3,
}).on('all', () => {
this.progressBar.update();
});
const reset = this.rateLimits[url];
if (reset) {
this.setRateLimitOnQueue(url, reset);
}
}
get(url, params, opts) {
return this.request('get', url, params, opts);
}
post(url, params, opts) {
return this.request('post', url, params, opts);
}
delete(url, params, opts) {
return this.request('delete', url, params, opts);
}
update(url, params, opts) {
return this.request('update', url, params, opts);
}
async request(method, url, params, { noWaitForReady, retry } = {}) {
// ex: that first rate limits request
if (!noWaitForReady) {
// make sure we are ready
await this.ready;
}
if (!this.queues[url]) {
this.createQueue(url);
}
return this.queues[url].add(async () => {
try {
return (await this.client[method](url, params)).data;
} catch (e) {
// not found
if (e.code === 34) return [];
// rate limited
if (e.code === 88) {
// write this better
// for now it just waits 20 seconds so that the rate limit
// auto request can find something
this.setRateLimitOnQueue(url, moment().add(ON_RATE_LIMIT_TIMEOUT, 'milliseconds').unix());
return;
}
throw e;
}
}, {
name: `${method.toUpperCase()} ${(url + (params ? ` ${JSON.stringify(params)}` : '')).substring(0, 100)}... }`,
retry,
});
}
initRateLimitsAutoFetch() {
this.rateFetchTimeeout = setInterval(async () => {
this.rateLimits = await this.getRateLimits();
}, RATE_LIMIT_AUTO_FETCH_INTERVAL);
}
initHelpers() {
this.helpers = {
getMentions: getMentions.bind(null, this),
getMentionedBy: getMentionedBy.bind(null, this),
getFollowing: getFollowing.bind(null, this),
getFollowers: getFollowers.bind(null, this),
convertUserIdsToHandles: convertUserIdsToHandles.bind(null, this),
};
}
close() {
clearInterval(this.rateFetchTimeeout);
this.progressBar.removeBar();
}
};