forked from jcutrell/jquery.getTweet.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetTweet.jquery.js
More file actions
108 lines (103 loc) · 4.14 KB
/
getTweet.jquery.js
File metadata and controls
108 lines (103 loc) · 4.14 KB
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
(function($){
$.fn.getTweet = function(username, numberOfTweets, options){
$.fn.getTweet.defaults = {
"username" : "jcutrell",
"numberOfTweets" : "1"
}
if ($.isPlainObject(username) && username != null){ options = $.extend({}, username) }
else if ($.isPlainObject(numberOfTweets && numberOfTweets != null )){ options = $.extend({}, numberOfTweets); options.username = username;}
else { options = {}; options.username = username; options.numberOfTweets = numberOfTweets };
var opts = $.extend({}, $.fn.getTweet.defaults, options);
return this.each(function(){
var that = $(this);
var jsonurl = "http://api.twitter.com/1/statuses/user_timeline/"+ opts.username +".json?count=" + opts.numberOfTweets + "&callback=?";
$.getJSON(jsonurl, function(data){
var html = '';
if (opts.numberOfTweets > 1){
html = "<ul id='getTweet'>";
$.each(data, function(index, item){
var tweet = item.text.replace(/(https?:\/\/[^\s]+|www\.[^\s]+)/g, "<a href='$1'>$1</a>");
tweet = tweet.replace(/(?:^|\s)#([^\s]+)/g, "<a href='http://search.twitter.com/search?q=%23$1'> #$1</a> ");
tweet = tweet.replace(/@([^\s]+)/g, '<a href="http://twitter.com/$1">@$1</a>');
html += "<li class='tweet'>" + tweet + "</li>";
});
html += "</ul>";
} else if (opts.numberOfTweets = 1) {
tweettext = data[0].text;
var tweet = tweettext.replace(/(https?:\/\/[^\s]+|www\.[^\s]+)/g, "<a href='$1'>$1</a>");
var html = tweet.replace(/(?:^|\s)#([^\s]+)\s/g, "<a href='http://search.twitter.com/search?q=%23$1'> #$1</a> ");
html = tweet.replace(/@([^\s]+)/g, '<a href="http://twitter.com/$1">@$1</a>');
}
that.html(html);
});
});
}
//developer
$.getTweet = function(username, numberOfTweets, callback, options){
$.getTweet.defaults = {
"username" : "jcutrell",
"numberOfTweets" : "1",
"callback" : ''
}
$.getTweet.api = {}
var options = $.extend({}, $.getTweet.defaults);
if (!($.isFunction(username)) && $.isPlainObject(username) && username != null)
{ options = $.extend({}, username) }
else if($.isFunction(username))
{options.callback = username}
if($.isArray(username)){
options.numberOfTweets = username;}
if (!($.isFunction(numberOfTweets)) && $.isPlainObject(numberOfTweets) && numberOfTweets != null )
{ options = $.extend({}, numberOfTweets); options.username = username;}
else if($.isFunction(numberOfTweets))
{options.callback = numberOfTweets; options.username = username}
else if (!($.isFunction(callback)) && $.isPlainObject(callback) && callback != null )
{ options = $.extend({}, callback); options.numberOfTweets = numberOfTweets; options.username = username;}
else {
var options = {}
options.username = username;
options.numberOfTweets = numberOfTweets;
options.callback = callback;
options.totalTweets = numberOfTweets;
}
if($.isArray(numberOfTweets)){
options.totalTweets = Math.max(options.numberOfTweets[0], options.numberOfTweets[1]) + 1;
} else {
options.totalTweets = numberOfTweets;
}
var opts = $.extend({}, $.getTweet.defaults, options);
var jsonurl = "http://api.twitter.com/1/statuses/user_timeline/"+ opts.username +".json?count=" + opts.totalTweets + "&callback=?";
$.getJSON(jsonurl, function(data){
returnval = [];
$.getTweet.api = data;
if (opts.numberOfTweets > 1 && !($.isArray(opts.numberOfTweets))){
$.each(data, function(index, item){
returnval[index] = item.text;
});
} else if (opts.numberOfTweets == 1 && !($.isArray(opts.numberOfTweets))) {
returnval[0] = data[0].text;
} else {
var start = opts.numberOfTweets[0],
end = data.length-1;
if (start > end){
end = opts.numberOfTweets[0];
start = opts.numberOfTweets[1];
var reverse = true;
}
var i = start;
for (var i = start; i <= end; i++){
if(data[i]){
returnval[(i-start)] = data[i].text;
}
}
if (reverse){
returnval = returnval.reverse();
}
}
// if opts.callback has been defined, execute it;
if(opts.callback != '' && $.isFunction(opts.callback)){
opts.callback(returnval);
};
});
};
})(jQuery);