-
Notifications
You must be signed in to change notification settings - Fork 58
/
api-functions.js
193 lines (171 loc) · 6.25 KB
/
api-functions.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const request = require('request-promise');
const oauth = require('./config').auth;
const rootUrl = 'https://api.twitter.com/1.1';
let allItems = [];
/* API methods */
const API = {
/**
* Search for tweets
* @param options {Object} Options object containing:
* - text (Required) : String
* - count (optional) : Number
* - result_type (optional) : String
* - geocode (optional) : String (lat long radius_in_miles)
* - since_id (optional) : Number - start search from this ID
* - max_id (optional) : Number - end search on this ID
*/
search: (options) => {
return new Promise((resolve, reject) => {
const {text, count = 100, result_type = 'popular', since_id = 0, max_id, geocode} = options;
let params =
`?q=${encodeURIComponent(options.text)}` +
`&count=${count}` +
`&result_type=${result_type}` +
`&since_id=${since_id}`;
if (max_id) {
params += `&max_id=${max_id}`;
}
if (geocode) {
params += `&geocode=${encodeURIComponent(geocode)}`;
}
allItems = [];
API.searchByStringParam(params).then((items) => resolve(items)).catch((err) => reject(err));
});
},
/**
* Search w/ params
* @param stringParams {String} Params as string
*/
searchByStringParam: (stringParams) =>
new Promise((resolve, reject) => {
const searchCallback = (res) => {
const result = JSON.parse(res);
if (result && result.statuses) {
result.statuses.forEach(item => allItems.push(item));
console.log('[Search] So far we have', allItems.length, 'items');
// If we have the next_results, search again for the rest (sort of a pagination)
const nextRes = result.search_metadata.next_results;
if (nextRes) {
API.searchByStringParam(nextRes).then((items) => resolve(items));
} else {
resolve(allItems);
}
} else {
resolve(null);
}
};
request.get({url: `${rootUrl}/search/tweets.json${stringParams}`, oauth})
.then(res => searchCallback(res))
.catch(err => reject(err));
})
,
/**
* Retweet a tweet
* @param tweetId {String} identifier for the tweet
*/
retweet: (tweetId) =>
new Promise((resolve, reject) =>
request.post({url: `${rootUrl}/statuses/retweet/${tweetId}.json`, oauth})
.then((res) => resolve(res))
.catch((err) => reject(err))
)
,
/**
* Like (aka favorite) a tweet
* @param tweetId {String} identifier for the tweet
*/
like: (tweetId) =>
new Promise((resolve, reject) =>
request.post({url: `${rootUrl}/favorites/create.json?id=${tweetId}`, oauth})
.then((res) => resolve(res))
.catch((err) => reject(err))
)
,
/**
* Follow a user by username
* @param userId {String} identifier for the user
*/
follow: (userId) =>
new Promise((resolve, reject) =>
request.post({url: `${rootUrl}/friendships/create.json?user_id=${userId}`, oauth})
.then((res) => resolve(res))
.catch((err) => reject(err))
)
,
/**
* Follow a user by username
* @param userName {String} username identifier for the user
*/
followByUsername: (userName) =>
new Promise((resolve, reject) =>
request.post({url: `${rootUrl}/friendships/create.json?screen_name=${userName}`, oauth})
.then((res) => resolve(res))
.catch((err) => reject(err))
)
,
/**
* Block a user
* @param userId {String} ID of the user to block
*/
blockUser: (userId) =>
new Promise((resolve, reject) =>
request.post({url: `${rootUrl}/blocks/create.json?user_id=${userId}`, oauth})
.then((res) => resolve(res))
.catch((err) => reject(err))
)
,
/** Get list of blocked users for the current user */
getBlockedUsers: () =>
new Promise((resolve, reject) =>
request.get({url: `${rootUrl}/blocks/list.json`, oauth})
.then((res) => resolve(JSON.parse(res).users.map((user) => user.id)))
.catch((err) => reject(err))
)
,
/**
* Get a user's tweets
* @param userId {String} identifier for the user
* @param count {Number} max tweets to retrieve
*/
getTweetsForUser: (userId, count) =>
new Promise((resolve, reject) =>
request.get({url: `${rootUrl}/statuses/user_timeline.json?user_id=${userId}&count=${count}`, oauth})
.then((response) => resolve(response))
.catch((err) => reject(err))
)
,
/**
* Delete a tweet
* @param tweetId {String} identifier for the tweet
*/
deleteTweet: (tweetId) =>
new Promise((resolve, reject) =>
request.post({url: `${rootUrl}/statuses/destroy/${tweetId}.json`, oauth})
.then(() => {
console.log('Deleted tweet', tweetId);
resolve();
})
.catch((err) => reject(err))
)
,
/**
* Reply to a tweet
* (The Reply on Twitter is basically a Status Update containing @username, where username is author of the original tweet)
* @param tweet {Object} The full Tweet we want to reply to
*/
replyToTweet: (tweet) =>
new Promise((resolve, reject) => {
try {
const text = encodeURIComponent(`@${tweet.user.screen_name} `);
request.post({
url: `${rootUrl}/statuses/update.json?status=${text}&in_reply_to_status_id=${tweet.id}`,
oauth
})
.then(() => resolve())
.catch(err => reject(err))
} catch (err) {
reject(err);
}
})
};
module.exports = API;