-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.simple_lifestream.js
147 lines (121 loc) · 3.57 KB
/
jquery.simple_lifestream.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
/**
* jQuery simple_lifestream plugin.
* Author: Marcus Nitzschke
*
* The plugin aggregates different feeds and sort all entries
* by date.
*
* Contribution:
* The favicon extraction is inspired by
* jQuery UrlFavicon Plugin - http://urlfavicon.danswackyworld.com - Daniel Yates
*/
(function($) {
$.fn.lifestream = function(options, callback)
{
// Defining settings and default values
var settings = jQuery.extend(
{
// array of the feed urls
feeds : [],
// number of shown items
count : 5,
// optionally hide the favicons
favicons : true,
// Google api key
api_key : null
},options
);
// Array of all feed data
var feeds = [];
// getting targeted DOM element
var output = $(this);
// initial cleaning of the target
output.empty();
// load google feed api
var key = settings.api_key ? "?key="+settings.api_key : "";
$.getScript('https://www.google.com/jsapi' + key, function(){
google.load("feeds", "1", { callback: function(){
$.each(settings.feeds, function(index, url){
// get feed items
var feed = new google.feeds.Feed(url);
feed.setNumEntries(settings.count);
feed.includeHistoricalEntries();
feed.load(function(result) {
if (!result.error) {
// push feed to the global array
feeds.push(result);
if (index+1 === settings.feeds.length) {
// transform the native feed objects into more useful entry objects
var entries = [];
$.each(feeds, function(index, item){
var feed_index = index;
$.each(item.feed.entries, function(index, entry){
// retrieve the favicon of the feed
var favicon = null;
if (settings.favicons){
var http = '(https*:\\/\\/)';
var fqdn = '((?:[a-z][a-z\\.\\d\\-]+)\\.(?:[a-z][a-z\\-]+))(?![\\w\\.])';
var url = new RegExp(http + fqdn, ['i']).exec(entry.link);
if(url != null) {
favicon = url[0] + '/favicon.ico';
}
else{
// Default icon
// TODO
}
}
entries.push({
"feed" : item.feed.title,
"favicon" : favicon,
"author" : entry.author,
"title" : entry.title,
"content" : entry.content,
"date" : entry.publishedDate,
"link" : entry.link
});
if ((index+1 === item.feed.entries.length) && (feed_index+1 === feeds.length)){
// sort all feeds by date
entries.sort(_entrySort);
// display the results
$(output).append("<ul id='entries'>");
// check wether there are less items than requested to be printed
var length = Math.min(settings.count, entries.length);
for ( i = 0; i < length; i++){
// set favicon output
favicon = entries[i].favicon ? "<img width='16' height='16' src='"+entries[i].favicon+"'/> " : "";
$("#entries")
.append(""
+ "<li>"
+ favicon
+ "<span class='title'>"
+ "<a href='"+entries[i].link+"' title='by "+entries[i].author+" on "+entries[i].date+"'>"
+ entries[i].title
+ "</a>"
+ "</span><br />"
+ "<span class='content'>"+entries[i].content+"</span>"
+ "</li>"
);
// finally call the optional callback function
if ( (i+1 === length) && ($.isFunction(callback)) ){
callback();
};
};
};
});
});
};
};
});
});
}});
});
/**
* Sort entry objects by descending date
*/
function _entrySort(a, b){
a = new Date(a.date);
b = new Date(b.date);
return b - a;
}
};
})(jQuery);