-
Notifications
You must be signed in to change notification settings - Fork 52
/
twitterlib.js
673 lines (597 loc) · 23 KB
/
twitterlib.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// twitterlib.js (c) 2011 Remy Sharp
// @version 1.0.9 / Sun Feb 19 23:05:25 2012 +0000
// MIT license: http://rem.mit-license.org
(function (global) {
var twitterlib = {};
// for Node.js - a quasi document polyfill
if (typeof exports !== 'undefined' && typeof require === 'function') {
var urlparse = require('url').parse,
http = require('http');
this.document = {
location: {
protocol: 'http:'
},
head: {
appendChild: function (element) {
if (!element.src) return; // exit if we're not a script
// send request
var urldata = urlparse(element.src, true),
request = http.request(urldata),
json = '';
var callback = window[urldata.query.callback];
request.on('response', function (res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
json += chunk;
}).on('end', function() {
switch (res.statusCode) {
case 200:
json = json.replace(new RegExp('^' + urldata.query.callback + '\\('), '').replace(/\);*$/, '');
outstanding[urldata.query.callback] && callback(JSON.parse(json));
break;
case 304:
break;
case 401:
// console.error('not authed');
break;
}
});
}).end();
}
},
getElementById: function () {},
createElement: function (type) {
return {
type: type,
id: null,
src: ''
};
}
};
}
var guid = +new Date,
window = this,
document = window.document,
head = document.head || document.getElementsByTagName('head')[0],
last = {}, // memorisation object for the next method
outstanding = {}, // reference object to allow us to cancel JSONP calls (though nulling the return function)
ENTITIES = {
'"': '"',
'<': '<',
'>': '>'
},
protocol = document.location.protocol.substr(0, 4) === 'http' ? document.location.protocol : 'http:',
URLS = {
search: protocol + '//search.twitter.com/search.json?q=%search%&page=%page|1%&rpp=%limit|100%&since_id=%since|remove%&result_type=recent&include_entities=true', // TODO allow user to change result_type
timeline: protocol + '//api.twitter.com/1/statuses/user_timeline.json?screen_name=%user%&count=%limit|200%&page=%page|1%&since_id=%since|remove%include_rts=%rts|false%&include_entities=true',
list: protocol + '//api.twitter.com/1/%user%/lists/%list%/statuses.json?page=%page|1%&per_page=%limit|200%&since_id=%since|remove%&include_entities=true&include_rts=%rts|false%',
favs: protocol + '//api.twitter.com/1/favorites/%user%.json?include_entities=true&skip_status=true&page=%page|1%&since_id=%since|remove%',
retweets: protocol + '//api.twitter.com/1/statuses/retweeted_by_user.json?screen_name=%user%&include_entities=true&count=%limit|200%&since_id=%since|remove%&page=%page|1%'
},
urls = URLS, // allows for resetting debugging
undefined,
caching = false;
var ify = function() {
return {
entities: function (t) {
return t.replace(/(&[a-z0-9]+;)/g, function (m) {
return ENTITIES[m];
});
},
link: function(t) {
return t.replace(/[a-z]+:\/\/([a-z0-9-_]+\.[a-z0-9-_:~\+#%&\?\/.=]+[^:\.,\)\s*$])/ig, function(m, link) {
return '<a title="' + m + '" href="' + m + '">' + ((link.length > 36) ? link.substr(0, 35) + '…' : link) + '</a>';
});
},
at: function(t) {
return t.replace(/(^|[^\w]+)\@([a-zA-Z0-9_]{1,15}(\/[a-zA-Z0-9-_]+)*)/g, function(m, m1, m2) {
return m1 + '<a href="http://twitter.com/' + m2 + '">@' + m2 + '</a>';
});
},
hash: function(t) {
return t.replace(/(^|[^&\w'"]+)\#([a-zA-Z0-9_^"^<]+)/g, function(m, m1, m2) {
return m.substr(-1) === '"' || m.substr(-1) == '<' ? m : m1 + '<a href="http://search.twitter.com/search?q=%23' + m2 + '">#' + m2 + '</a>';
});
},
clean: function(tweet) {
return this.hash(this.at(this.link(tweet)));
}
};
}();
var expandLinks = function (tweet) {
if (tweet === undefined) return '';
var text = tweet.text,
i = 0;
if (tweet.entities) {
// replace urls with expanded urls and let the ify shorten the link
if (tweet.entities.urls && tweet.entities.urls.length) {
for (i = 0; i < tweet.entities.urls.length; i++) {
if (tweet.entities.urls[i].expanded_url) text = text.replace(tweet.entities.urls[i].url, tweet.entities.urls[i].expanded_url); // /g ?
}
}
// replace media with url to actual image (or thing?)
if (tweet.entities.media && tweet.entities.media.length) {
for (i = 0; i < tweet.entities.media.length; i++) {
if (tweet.entities.media[i].media_url || tweet.entities.media[i].expanded_url) text = text.replace(tweet.entities.media[i].url, tweet.entities.media[i].media_url ? tweet.entities.media[i].media_url : tweet.entities.media[i].expanded_url); // /g ?
}
}
}
return text;
};
var time = function () {
var monthDict = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return {
time: function (date) {
var hour = date.getHours(),
min = date.getMinutes() + "",
ampm = 'AM';
if (hour == 0) {
hour = 12;
} else if (hour == 12) {
ampm = 'PM';
} else if (hour > 12) {
hour -= 12;
ampm = 'PM';
}
if (min.length == 1) {
min = '0' + min;
}
return hour + ':' + min + ' ' + ampm;
},
date: function (date) {
var mon = monthDict[date.getMonth()],
day = date.getDate()+'',
dayi = ~~(day),
year = date.getFullYear(),
thisyear = (new Date()).getFullYear(),
th = 'th';
// anti-'th' - but don't do the 11th, 12th or 13th
if ((dayi % 10) == 1 && day.substr(0, 1) != '1') {
th = 'st';
} else if ((dayi % 10) == 2 && day.substr(0, 1) != '1') {
th = 'nd';
} else if ((dayi % 10) == 3 && day.substr(0, 1) != '1') {
th = 'rd';
}
if (day.substr(0, 1) == '0') {
day = day.substr(1);
}
return mon + ' ' + day + th + (thisyear != year ? ', ' + year : '');
},
shortdate: function (time_value) {
var values = time_value.split(" "),
parsed_date = Date.parse(values[1] + " " + values[2] + ", " + values[5] + " " + values[3]),
date = new Date(parsed_date),
mon = monthDict[date.getMonth()],
day = date.getDate()+'',
year = date.getFullYear(),
thisyear = (new Date()).getFullYear();
if (thisyear === year) {
return day + ' ' + mon;
} else {
return day + ' ' + mon + (year+'').substr(2, 2);
}
},
datetime: function (time_value) {
var values = time_value.split(" "),
date = new Date(Date.parse(values[1] + " " + values[2] + ", " + values[5] + " " + values[3]));
return this.time(date) + ' ' + this.date(date);
},
relative: function (time_value) {
var values = time_value.split(" "),
parsed_date = Date.parse(values[1] + " " + values[2] + ", " + values[5] + " " + values[3]),
date = new Date(parsed_date),
relative_to = (arguments.length > 1) ? arguments[1] : new Date(),
delta = ~~((relative_to.getTime() - parsed_date) / 1000),
r = '';
delta = delta + (relative_to.getTimezoneOffset() * 60);
if (delta <= 1) {
r = '1 second ago';
} else if (delta < 60) {
r = delta + ' seconds ago';
} else if (delta < 120) {
r = '1 minute ago';
} else if (delta < (45*60)) {
r = (~~(delta / 60)) + ' minutes ago';
} else if (delta < (2*90*60)) { // 2* because sometimes read 1 hours ago
r = '1 hour ago';
} else if (delta < (24*60*60)) {
r = (~~(delta / 3600)) + ' hours ago';
} else {
r = this.shortdate(time_value);
}
return r;
}
};
}();
var filter = (function () {
return {
match: function (tweet, search, includeHighlighted) {
var i = 0, s = '', text = tweet.text.toLowerCase(),
notonly = (!search['and'] || !search['and'].length) && (!search['or'] || !search['or'].length);
if (typeof search == "string") {
search = this.format(search);
}
// loop ignore first
if (search['not'] && search['not'].length) {
for (i = 0; i < search['not'].length; i++) {
if (text.indexOf(search['not'][i]) !== -1) {
return false;
}
}
if (notonly) {
return true;
}
} else if (({}).toString.call(search['not']) !== '[object Array]') {
if (search['not'].test(text)) {
return false;
}
if (notonly) {
return true;
}
}
if (search['and'] && search['and'].length) {
for (i = 0; i < search['and'].length; i++) {
s = search['and'][i];
if (s.substr(0, 3) === 'to:') {
if (!RegExp('^@' + s.substr(3)).test(text)) {
return false;
}
} else if (s.substr(0, 5) == 'from:') {
if (tweet.user.screen_name !== s.substr(5)) {
return false;
}
} else if (text.indexOf(s) === -1) {
return false;
}
}
} else if (typeof search['and'] == 'function') {
if (search['and'].test(text)) {
return true;
}
}
if (search['or'] && search['or'].length) {
for (i = 0; i < search['or'].length; i++) {
s = search['or'][i];
if (s.substr(0, 3) === 'to:') {
if (RegExp('^@' + s.substr(3)).test(text)) {
return true;
}
} else if (s.substr(0, 5) == 'from:') {
if (tweet.user.screen_name === s.substr(5)) {
return true;
}
} else if (text.indexOf(search['or'][i]) !== -1) {
return true;
}
}
} else if (typeof search['or'] == 'function') {
if (search['or'].test(text)) {
return true;
}
} else if (search['and'] && search['and'].length) {
return true;
}
return false;
},
format: function (search, caseSensitive) {
// search can match search.twitter.com format
var blocks = [], ors = [], ands = [], i = 0, negative = [], since = '', until = '';
search.replace(/(-?["'](.*?)["']|\S+)/g, function (m) { // removed \b for chinese character support
var neg = false;
if (m.substr(0, 1) == '-') {
neg = true;
}
m = m.replace(/["']+|["']+$/g, '');
if (neg) {
negative.push(m.substr(1).toLowerCase());
} else {
blocks.push(m);
}
});
for (i = 0; i < blocks.length; i++) {
if (blocks[i] == 'OR' && blocks[i+1]) {
ors.push(blocks[i-1].toLowerCase());
ors.push(blocks[i+1].toLowerCase());
i++;
ands.pop(); // remove the and test from the last loop
} else {
ands.push(blocks[i].toLowerCase());
}
}
return {
'or' : ors,
'and' : ands,
'not' : negative
};
},
// tweets typeof Array
matchTweets: function (tweets, search, includeHighlighted) {
var updated = [], tmp, i = 0;
if (typeof search == 'string') {
search = this.format(search);
}
for (i = 0; i < tweets.length; i++) {
if (this.match(tweets[i], search, includeHighlighted)) {
updated.push(tweets[i]);
}
}
return updated;
}
};
})();
// based on twitter.com list of tweets, most common format for tweets
function render(tweet) {
var html = '<li><div class="tweet">';
html += '<div class="vcard"><a href="http://twitter.com/' + tweet.user.screen_name + '" class="url"><img style="height: 48px; width: 48px;" alt="' + tweet.user.name + '" class="photo fn" height="48" src="' + tweet.user.profile_image_url + '" width="48" /></a></div>';
html += '<div class="hentry"><strong><a href="http://twitter.com/';
html += tweet.user.screen_name + '" ';
html += 'title="' + tweet.user.name + '">' + tweet.user.screen_name + '</a></strong> ';
html += '<span class="entry-content">';
html += twitterlib.ify.clean(tweet.text); //twitterlib.expandLinks(tweet));
html += '</span> <span class="meta entry-meta"><a href="http://twitter.com/' + tweet.user.screen_name;
html += '/status/' + tweet.id_str + '" class="entry-date" rel="bookmark"><span class="published" title="';
html += twitterlib.time.datetime(tweet.created_at) + '">' + twitterlib.time.relative(tweet.created_at) + '</span></a>';
if (tweet.source) html += ' <span>from ' + tweet.source + '</span>';
if (tweet.retweetedby) html += ' <span>retweeted by ' + tweet.retweetedby.screen_name + '</span>';
html += '</span></div></div></li>';
return html;
}
function clean(guid) {
var el = document.getElementById(twitterlib + guid);
if (el) {
head.removeChild(document.getElementById(twitterlib + guid));
}
delete outstanding[twitterlib + guid];
window[twitterlib + guid] = undefined;
try{ delete window[ twitterlib + guid ]; } catch(e){}
}
function load(url, options, callback) {
var script = document.createElement('script'), match = null;
if (options == undefined) options = {};
guid++;
outstanding['twitterlib' + guid] = true;
window['twitterlib' + guid] = (function (guid, options) { // args are now private and static
return function (tweets) {
// remove original script include
var i = 0, parts = [];
// let's expand the urls by default
i = tweets.length;
while (i--) {
tweets[i].originalText = tweets[i].text;
tweets[i].text = expandLinks(tweets[i]);
}
if (tweets.results) {
tweets = tweets.results;
i = tweets.length;
// fix the user prop to match "normal" API calls
while (i--) {
tweets[i].user = { id: tweets[i].from_user_id, screen_name: tweets[i].from_user, profile_image_url: tweets[i].profile_image_url };
tweets[i].source = twitterlib.ify.entities(tweets[i].source);
// fix created_at
parts = tweets[i].created_at.split(' ');
tweets[i].created_at = [parts[0],parts[2],parts[1],parts[4],parts[5], parts[3]].join(' ').replace(/,/, '');
}
} else if (tweets.length && tweets[0].sender) { // DM - we'll change it to look like a tweet
i = tweets.length;
while (i--) {
tweets[i].user = tweets[i].sender;
tweets[i].originalText = tweets[i].text;
tweets[i].text = '@' + tweets[i].recipient_screen_name + ' ' + tweets[i].text;
}
} else if (options.rts == true || options.rts == 't' || options.rts == 1) {
// scan for native retweets and swap them in as real tweets
i = tweets.length;
while (i--) {
if (tweets[i].retweeted_status) {
tweets[i].retweeted_status.retweetedby = tweets[i].user;
tweets[i] = tweets[i].retweeted_status;
}
}
}
options.originalTweets = tweets;
if (options.filter) {
tweets = filter.matchTweets(tweets, options.filter);
}
if (options.limit && options.limit < tweets.length) {
// chop
tweets = tweets.splice(0, options.limit);
}
if (caching && options.page > 1) {
try {
sessionStorage.setItem('twitterlib.page' + options.page + '.tweets', JSON.stringify(tweets));
sessionStorage.setItem('twitterlib.page' + options.page + '.originalTweets', JSON.stringify(options.originalTweets));
sessionStorage.setItem('twitterlib.page' + options.page, 'true');
} catch (e) {
// possible QUOTA EXCEEDED
}
}
options.cached = false;
callback.call(twitterlib, tweets, options);
// clean up
clean(guid);
};
})(guid, options);
match = url.match(/callback=(.*)/);
if (match != null && match.length > 1) {
window[match[1]] = window['twitterlib' + guid];
} else {
url += '&callback=' + 'twitterlib' + guid;
}
// all first requests go via live
if (!caching || options.page <= 1 || (caching && sessionStorage.getItem('twitterlib.page' + options.page) == null)) {
script.src = url;
script.id = 'twitterlib' + guid;
head.appendChild(script);
} else if (caching) {
clean(guid);
options.cached = true;
options.originalTweets = JSON.parse(sessionStorage.getItem('twitterlib.page' + options.page + '.originalTweets'));
// reset the tweets - so we can cache but refilter
var tweets = JSON.parse(sessionStorage.getItem('twitterlib.page' + options.page + '.tweets') || '[]');
if (options.filter) {
tweets = filter.matchTweets(tweets, options.filter);
}
if (options.limit && options.limit < tweets.length) {
// chop
tweets = tweets.splice(0, options.limit);
}
callback.call(twitterlib, tweets, options);
}
}
function getUrl(type, options) {
options = options || {};
return urls[type].replace(/(\b.*?)%(.*?)(\|.*?)?%/g, function (a, q, key, def) {
// remove empty values that shouldn't be sent
if (def && def.substr(1) == 'remove' && options[key] == undefined) {
return '';
}
var val = key == 'limit' ? options[key] + 10 : options[key];
return q + (options[key] === undefined && def !== undefined ? def.substr(1) : val);
}) + (!!this.accessToken ? '&access_token=' + this.accessToken : '');
}
function normaliseArgs(options, callback) {
if (typeof options == 'function') {
callback = options;
options = {};
}
if (options === undefined) options = {};
options.page = options.page || 1;
options.callback = callback;
if (options.limit === 0) {
delete options.limit;
}
return options;
}
// save the last type of request, with all their options - so we can hit twitterlib.next() and it'll automagically prepopulate the filters, etc.
function setLast(method, arg, options) {
last = {
method: method,
arg: arg,
options: options,
callback: options.callback,
page: options.page || 1
};
options.method = method;
if (caching) {
var last_request = JSON.parse(sessionStorage.getItem('twitterlib.last_request') || '{}');
if (last.method != last_request.method || last.arg != last_request.arg) {
clearCache();
sessionStorage.setItem('twitterlib.last_request', JSON.stringify(last));
}
}
}
function clearCache() {
var i = sessionStorage.length;
while (i--) {
if (sessionStorage.key(i).substr(0, 'twitterlib'.length) == 'twitterlib') {
sessionStorage.removeItem(sessionStorage.key(i));
}
}
}
// create a new method on twitterlib, that hits the given url,
// i.e. twitterlib.custom('dm', '/proxy.php?page=%page%&type=direct_messages');
// can now be called using twitterlib.dm('rem', function (t) { });
function custom(name, url, defaults) {
if (url && urls[name] == undefined) urls[name] = url;
if (this[name] == undefined) {
this[name] = function (term, options, callback) {
// handle "termless" custom methods
if (typeof term == 'function') {
callback = term;
term = '';
} else if (term.toString() == '[Object object]') {
callback = options;
options = term;
term = '';
}
options = normaliseArgs(options, callback);
setLast(name, term, options);
// slight hack to support my own shortcuts
options[name] = options.user = term;
options.search = encodeURIComponent(term);
if (options.callback) load(getUrl(name, options), options, options.callback);
return this;
};
}
// makes my code nicer to read when setting up twitterlib object
return this[name];
}
twitterlib = {
version: '1.0.9', //@version 1.0.9 / Sun Feb 19 23:05:25 2012 +0000
// search is an exception case
custom: custom,
getUrl: getUrl,
status: function (user, options, callback) { // alias function
options = normaliseArgs(options, callback);
options.limit = 1;
setLast('status', user, options); // setting after limit = 1 to keep this intact
return this.timeline(user, options, options.callback);
},
list: function (list, options, callback) {
var parts = list.split('/');
options = normaliseArgs(options, callback);
setLast('list', list, options);
options.user = parts[0];
options.list = parts[1];
if (options.callback) load(getUrl('list', options), options, options.callback);
return this;
},
next: function () {
if (last.method) {
last.page++;
last.options.page = last.page;
this[last.method](last.arg, last.options, last.callback);
} // else we won't do anything
return this;
},
refresh: function () { // same as hitting again
if (last.method) {
this[last.method](last.arg, last.options, last.callback);
} // else we won't do anything
return this;
},
// appending on pre-existing utilities
time: time,
ify: ify,
filter: filter,
expandLinks: expandLinks,
cancel: function () {
for (var k in outstanding) {
window[k] = (function () { return function (guid) { clean(guid); };})(k.replace('twitterlib', ''));
}
outstanding = {};
return this;
},
reset: function () {
urls = URLS;
last.method = '';
},
render: render,
debug: function (data) {
for (var url in data) {
urls[url] = data[url];
}
return this;
},
cache: function (enabled) {
caching = enabled == undefined ? true : enabled;
// cache is only supported if you have native json encoding
if (!window.JSON || !window.sessionStorage) {
caching = false;
}
},
setAccessToken: function (token) {
this.accessToken = token;
return this;
}
};
twitterlib.custom('search');
twitterlib.custom('timeline');
twitterlib.custom('favs');
twitterlib.custom('retweets');
if (typeof exports !== 'undefined') {
module.exports = twitterlib;
}
global.twitterlib = twitterlib;
})(this);