-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery-twitter-plugin.js
377 lines (291 loc) · 9.93 KB
/
jquery-twitter-plugin.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* @author Stéphane Roucheray
* @extends jQuery
*/
(function($){
var base_url = "http://twitter.com/",
search_base_url = "http://search.twitter.com/",
urls = {
search : search_base_url + "search",
help_test : base_url + "help/test",
trends : search_base_url + "trends",
trends_current : search_base_url + "trends/current",
trends_daily : search_base_url + "trends/daily",
trends_weekly : search_base_url + "trends/weekly",
statuses_public_timeline : base_url + "statuses/public_timeline",
statuses_friends_timeline : base_url + "statuses/friends_timeline",
statuses_user_timeline : base_url + "statuses/user_timeline",
statuses_mentions : base_url + "statuses/mentions",
statuses_show : base_url + "statuses/show",
statuses_update : base_url + "statuses/update",
statuses_destroy : base_url + "statuses/destroy",
statuses_friends : base_url + "statuses/friends",
statuses_followers : base_url + "statuses/followers",
users_show : base_url + "users/show",
direct_messages : base_url + "direct_messages",
direct_messages_sent : base_url + "direct_messages/sent",
direct_messages_new : base_url + "direct_messages/new",
direct_messages : base_url + "direct_messages/destroy",
friendship_create : base_url + "friendships/create",
friendship_destroy : base_url + "friendships/destroy",
friendship_exists : base_url + "friendships/exists",
friends_ids : base_url + "friends/ids",
followers_ids : base_url + "followers/ids",
account_verify_credentials : base_url + "account/verify_credentials",
account_rate_limit_status : base_url + "account/rate_limit_status",
account_end_session : base_url + "account/end_session",
account_update_delivery_device : base_url + "account/update_delivery_device",
account_update_profile_colors : base_url + "account/update_profile_colors",
account_update_profile_image : base_url + "account/update_profile_image",
account_update_profile_background_image : base_url + "account/",
account_update_profile : base_url + "account/update_profile/update_profile_background_image",
favorites : base_url + "favorites",
favorites_create : base_url + "favorites/create/",
favorites_destroy : base_url + "favorites/destroy",
notifications_follow : base_url + "notifications/follow",
notifications_leave : base_url + "notifications/leave",
blocks_create : base_url + "blocks/create",
blocks_destroy : base_url + "blocks/destroy",
blocks_exists : base_url + "blocks/exists",
blocks_blocking : base_url + "blocks/blocking",
blocks_ids : base_url + "blocks/blocking/ids",
saved_searches : base_url + "saved_searches",
saved_searches_show : base_url + "saved_searches/show",
saved_searches_create : base_url + "saved_searches/create",
saved_searches_destroy : base_url + "saved_searches/destroy",
oauth_request_token : base_url + "oauth/request_token",
oauth_authorize : base_url + "oauth/authorize",
oauth_authenticate : base_url + "oauth/authenticate",
oauth_access_token : base_url + "oauth/access_token"
};
//Generic private methods to build requests
// parameters : $.ajax method specific parameters
// data : data encoded in the get url string
function request(parameters /* Object */, data /* Object */){
parameters = $.extend({
data:data
}, parameters);
parameters.url = parameters.url + ".json";
//Bug in jQuery with jsonp, if "jsonp" plugin is present then use it
if ($.jsonp && $.isFunction($.jsonp)) {
parameters.url += "?callback=?";
$.jsonp(parameters);
}else{
parameters.dataType = "jsonp";
$.ajax( parameters );
}
};
// Delete all parameters Object properties not specified in the authorized Array
function cleanupParam(parameters /* Object */, authorized /* Array */){
if (parameters) {
$.each(parameters, function(key, value){
if ($.inArray(key, authorized) == -1) {
delete parameters[key];
}
});
}
return parameters || {};
}
// Delete non wanted
function getAjaxParam(options, url, success){
var ajaxParams = [
"async",
"cache",
"complete",
"contentType",
// "data", // In our case, data is set outside this object
"dataFilter",
// "dataType", // Always jsonp
"error",
"global",
"ifModified",
// "jsonp", // callback name must not be renamed
"password",
"processData",
"scriptCharset",
// "success", // In our case, success is set outside this object
"timeout",
// "type", // Type, GET or POST, is determined by the calling method
// "url", // User not need to set the url
"username",
"xhr"],
output = {};
// Fix at http://code.google.com/p/jquery-twitter-api/issues/detail?id=1
/* if (ajaxParams) {
//Delete extra parameters
$.each(options, function(key, value){
if ($.inArray(key, ajaxParams) != -1) {
output[key] = value;
}
});
}*/
// new code
if (ajaxParams && options) {
//Delete extra parameters
$.each(options, function(key, value){
if ($.inArray(key, ajaxParams) != -1) {
output[key] = value;
}
});
}
// end fix
output.url = url;
output.success = success;
return output;
}
/*
* Generic twitter object method
*/
$.twitter = {
/* Help Methods */
test : function(callback, options){
var ajaxParam = getAjaxParam(options, urls.help_test, callback);
request(ajaxParam);
},
/* Search API Methods */
search : function(query, callback, options){
// From options, extract jQuery Ajax param
// And add the url and the callback method
var ajaxParam = getAjaxParam(options, urls.search, callback);
// From options cleanup non Twitter params
options = cleanupParam(
options,
["callback","lang","rpp","page","since_id","geocode","show_user"]
);
// Limit search to 140 characters
// And add the query to the options
options = $.extend(
{
q : query.substr(0, 140)
},options
);
// Make the request
request(ajaxParam, options);
},
/* Trends main method */
trends : function(callback, options){
var ajaxParam = getAjaxParam(options, urls.trends, callback);
request(ajaxParam);
},
statuses : {}, //No direct function
users : {}, //No direct function
direct_messages : function(){}, //TODO: implements
friendships : {}, //No direct function
friends : {}, //No direct function
followers : {}, //No direct function
account : {}, //No direct function
favorites : function(){}, //TODO: implements
notifications : {}, //No direct function
blocks : {},
saved_searches : function(){}, //TODO: implements
oauth : {} //No direct function
};
/* Simple searches */
$.twitter.search.user = function(username, callback, options){
$.twitter.search("from:"+username, callback, options);
};
$.twitter.search.repliesTo = function(username, callback, options){
$.twitter.search("to:"+username, callback, options);
};
$.twitter.search.mentioned = function(username, callback, options){
$.twitter.search("@"+username, callback, options);
};
$.twitter.search.hashtag = function(hashtag, callback, options){
$.twitter.search("#"+hashtag, callback, options);
};
// MVG my own
$.twitter.search.anytag = function(hashtag, callback, options){
$.twitter.search(hashtag, callback, options);
};
/* Specifics trends */
$.twitter.trends.current = function(callback, options){
// From options, extract jQuery Ajax param
// And add the url and the callback method
var ajaxParam = getAjaxParam(options, urls.trends_current, callback);
// From options cleanup non Twitter params
options = cleanupParam(
options,
["exclude"]
);
request(ajaxParam, options);
};
$.twitter.trends.daily = function(callback){//TODO: implements options
request(
{
url: urls.trends_daily,
success: callback
}
);
};
$.twitter.trends.weekly = function(callback){//TODO: implements options
request(
{
url: urls.trends_weekly,
success: callback
}
);
};
/* Timeline Methods */
$.twitter.statuses.publicTimeline = function(callback, options){
var ajaxParam = getAjaxParam(options, urls.statuses_public_timeline, callback);
request(ajaxParam);
};
/* Status Methods */
//Requires authentication if the author of the status is protected
$.twitter.statuses.show = function(userid, callback, options){
var ajaxParam = getAjaxParam(options, urls.statuses_show + "/" + userid, callback);
request(ajaxParam);
};
/* User Methods */
$.twitter.users.show = function(userid, callback, options){
//TODO : check userid is number until
if (userid){
options.user_id = userid;
}
var ajaxParam = getAjaxParam(options, urls.users_show + "/" + userid, callback);
options = cleanupParam(
options,
["id","user_id","screen_name"]
);
request(ajaxParam, options);
};
$.twitter.statuses.friends = function(userid, callback, options){
if (userid){
options.user_id = userid;
}
var ajaxParam = getAjaxParam(options, urls.statuses_friends + "/" + userid, callback);
options = cleanupParam(
options,
["id","user_id","screen_name", "page"]
);
request(ajaxParam, options);
};
/* Social Graph Methods */
$.twitter.friends.ids = function(userid, callback, options){
if (userid){
options.user_id = userid;
}
var ajaxParam = getAjaxParam(options, urls.friends_ids + "/" + userid, callback);
options = cleanupParam(
options,
["id","user_id","screen_name", "page"]
);
request(ajaxParam, options);
};
$.twitter.followers.ids = function(userid, callback, options){
if (userid){
options.user_id = userid;
}
var ajaxParam = getAjaxParam(options, urls.followers_ids + "/" + userid, callback);
options = cleanupParam(
options,
["id","user_id","screen_name", "page"]
);
request(ajaxParam, options);
};
//Only for IP rate limit status
$.twitter.account.rateLimitStatus = function(callback, options){
var ajaxParam = getAjaxParam(options, urls.account_rate_limit_status, callback);
request(ajaxParam);
};
})(jQuery);