Skip to content
This repository has been archived by the owner on May 2, 2021. It is now read-only.

Commit

Permalink
parsing all rss pages
Browse files Browse the repository at this point in the history
  • Loading branch information
danecando committed Aug 31, 2014
1 parent 055e45c commit b980942
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
75 changes: 48 additions & 27 deletions jquery.ghostrelated.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* @package jquery.ghostrelated
* @version 0.1.1
* @version 0.2.0
* @Copyright (C) 2014 Dane Grant (danecando@gmail.com)
* @License MIT
*/
Expand All @@ -20,44 +20,62 @@
this.element = element;
this.options = $.extend({}, defaults, options);

this.displayRelated();
this.parseRss();
};

RelatedPosts.prototype.displayRelated = function() {
RelatedPosts.prototype.displayRelated = function(posts) {

var self = this,
count = 0;

this._currentPostTags = this.getCurrentPostTags(this.options.tagsClass);

var that = this;
var related = this.matchByTag(this._currentPostTags, posts);

related.forEach(function(post) {
if (count < self.options.limit) {
$(self.element).append($('<li><a href="' + post.url + '">' + post.title + '</a></li>'));
}
count++;
});

if (count == 0) {
$(this.element).append($('<p>No related posts were found. ' +
'Check the <a href="/">index</a>.</p>'));
}

};

RelatedPosts.prototype.parseRss = function(pageNum, prevId, feeds) {

var page = pageNum || 1,
prevId = prevId || '',
feeds = feeds || [],
self = this;

$.ajax({
url: this.options.feed,
url: this.options.feed + '/' + page,
type: 'GET'
})
.done(function(data) {

// Success fetching feed, find related posts and output them
var posts = that.getPosts(data);
var related = that.matchByTag(that._currentPostTags, posts);

var count = 0;
related.forEach(function(post) {
if (count < that.options.limit) {
$(that.element).append($('<li><a href="' + post.url + '">' + post.title + '</a></li>'));
}
count++;
});
.done(function(data, textStatus, xhr) {

var curId = $(data).find('item > guid').text();

if (count == 0) {
$(that.element).append($('<p>No related posts were found. ' +
'Check the <a href="/">index</a>.</p>'));
if (curId != prevId) {
feeds.push(data);
self.parseRss(page+1, curId, feeds);
} else {
var posts = self.getPosts(feeds);
self.displayRelated(posts);
}

})
.fail(function(e) {
that.reportError(e);
self.reportError(e);
});

};


RelatedPosts.prototype.getCurrentPostTitle = function(titleClass) {

if (titleClass[0] != '.') {
Expand Down Expand Up @@ -93,14 +111,17 @@
};


RelatedPosts.prototype.getPosts = function(feed) {
RelatedPosts.prototype.getPosts = function(feeds) {

var posts = [];
var items = $(feed).find('item');
var posts = [], items = [];

feeds.forEach(function(feed) {
items = $.merge(items, $(feed).find('item'));
});

for (var i = 0; i < items.length; i++) {

var item = items.eq(i);
var item = $(items[i]);

if (item.find('title').text() !== this.getCurrentPostTitle(this.options.titleClass)) {

Expand Down
2 changes: 1 addition & 1 deletion jquery.ghostrelated.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b980942

Please sign in to comment.