forked from raulrene/twitter-contest-js-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-functions.js
112 lines (94 loc) · 3.06 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
var request = require('request-promise');
var oauth = require("./config");
var allItems = [];
//Callback functions
var callbacks = {
default: function (response) {
var result = JSON.parse(response);
// Case when the callback is used after a search
if (result && result.statuses) {
result.statuses.forEach(function (item) {
allItems.push(item.id);
});
console.log("So far we have a total of:", allItems.length);
// If we have the next_results, search again for the rest (sort of a pagination)
if (result.search_metadata.next_results) {
API.searchByStringParam(result.search_metadata.next_results, callbacks.default);
}
}
},
processBlockedList: function (response) {
var result = JSON.parse(response),
blockedList = [];
result.users.forEach(function (user) {
blockedList.push(user.id);
});
console.log("Your list of blocked users:", blockedList);
return blockedList;
}
};
var API = {
search: function (options) {
params =
"?q=" + encodeURIComponent(options.text),
"&count=" + options.count ? options.count : 100,
"&result_type=" + options.result_type ? options.result_type : 'popular',
"&since_id=" + options.since_id ? options.since_id : 0;
if (options.max_id) {
params += "&max_id=" + options.max_id;
}
API.searchByStringParam(params, options.callback ? options.callback : callbacks.default, options.error_callback);
},
searchByStringParam: function (stringParams, callback, errorHandler) {
request.get({url: 'https://api.twitter.com/1.1/search/tweets.json' + stringParams, oauth: oauth})
.then(callback)
.catch(function (err) {
if (errorHandler) {
errorHandler(err);
} else {
console.error("An error occurred:", err.message);
}
});
},
retweet: function (tweetId) {
request.post({url: 'https://api.twitter.com/1.1/statuses/retweet/' + tweetId + '.json', oauth: oauth})
.then(callbacks.default)
.catch(function(err) {
console.error(err.message);
});
},
favorite: function (tweetId) {
request.post({url: 'https://api.twitter.com/1.1/favorites/create.json?id=' + tweetId, oauth: oauth})
.then(callbacks.default)
.catch(function(err) {
console.error(err.message);
});
},
follow: function (userId) {
request.post({url: 'https://api.twitter.com/1.1/friendships/create.json?user_id=' + userId, oauth: oauth})
.then(callbacks.default)
.catch(function(err) {
console.error(err.message);
});
},
followByUsername: function (userName) {
request.post({url: 'https://api.twitter.com/1.1/friendships/create.json?screen_name=' + userName, oauth: oauth})
.then(callbacks.default)
.catch(function(err) {
console.error(err.message);
});
},
getBlockedUsers: function (callback) {
request.get({url: 'https://api.twitter.com/1.1/blocks/list.json', oauth: oauth})
.then(function (response) {
var blockedList = callbacks.processBlockedList(response);
if (callback) {
callback(blockedList);
}
})
.catch(function(err) {
console.error("Error retrieving blocked users:", err.message);
});
}
};
module.exports = API;