This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
forked from raulrene/twitter-contest-js-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi-functions.js
146 lines (125 loc) · 4.89 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
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:\n", 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, cb, errorHandler) {
request.post({url: 'https://api.twitter.com/1.1/statuses/retweet/' + tweetId + '.json', oauth: oauth})
.then(function() {
cb();
})
.catch(function(err) {
if (errorHandler)
errorHandler(err);
else
console.error("An error occurred:", 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);
});
},
blockUser: function(userId)
{
request.post({url: 'https://api.twitter.com/1.1/blocks/create.json?user_id=' + userId, 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);
});
},
getTweetsForUser: function (userId, count, callback) {
request.get({url: 'https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=' + userId + '&count=' + count, oauth: oauth})
.then(function (response) {
callback(JSON.parse(response));
})
.catch(function (err) {
console.error(err.message);
});
},
deleteTweet: function (tweetId) {
request.post({url: 'https://api.twitter.com/1.1/statuses/destroy/' + tweetId + ".json", oauth: oauth})
.then(function () {
console.log("Deleted tweet", tweetId);
})
.catch(function (err) {
console.error(err.message)
});
}
};
module.exports = API;