From b98094243b29b7fcbfca277ab4aa7da915683f16 Mon Sep 17 00:00:00 2001 From: Dane Grant Date: Sun, 31 Aug 2014 19:57:21 -0400 Subject: [PATCH] parsing all rss pages --- jquery.ghostrelated.js | 75 ++++++++++++++++++++++++-------------- jquery.ghostrelated.min.js | 2 +- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/jquery.ghostrelated.js b/jquery.ghostrelated.js index 2d43718..e62df18 100644 --- a/jquery.ghostrelated.js +++ b/jquery.ghostrelated.js @@ -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 */ @@ -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($('
  • ' + post.title + '
  • ')); + } + count++; + }); + + if (count == 0) { + $(this.element).append($('

    No related posts were found. ' + + 'Check the index.

    ')); + } + + }; + + 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($('
  • ' + post.title + '
  • ')); - } - count++; - }); + .done(function(data, textStatus, xhr) { + + var curId = $(data).find('item > guid').text(); - if (count == 0) { - $(that.element).append($('

    No related posts were found. ' + - 'Check the index.

    ')); + 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] != '.') { @@ -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)) { diff --git a/jquery.ghostrelated.min.js b/jquery.ghostrelated.min.js index f0553ae..78b3a35 100644 --- a/jquery.ghostrelated.min.js +++ b/jquery.ghostrelated.min.js @@ -1 +1 @@ -(function($){defaults={feed:"/rss",titleClass:".post-title",tagsClass:".post-meta",limit:5,debug:false};function RelatedPosts(element,options){this.element=element;this.options=$.extend({},defaults,options);this.displayRelated()}RelatedPosts.prototype.displayRelated=function(){this._currentPostTags=this.getCurrentPostTags(this.options.tagsClass);var that=this;$.ajax({url:this.options.feed,type:"GET"}).done(function(data){var posts=that.getPosts(data);var related=that.matchByTag(that._currentPostTags,posts);var count=0;related.forEach(function(post){if(count'+post.title+""))}count++});if(count==0){$(that.element).append($("

    No related posts were found. "+'Check the index.

    '))}}).fail(function(e){that.reportError(e)})};RelatedPosts.prototype.getCurrentPostTitle=function(titleClass){if(titleClass[0]!="."){titleClass="."+titleClass}var postTitle=$(titleClass).text();if(postTitle.length<1){this.reportError("Couldn't find the post title with class: "+titleClass)}return postTitle};RelatedPosts.prototype.getCurrentPostTags=function(tagsClass){if(tagsClass[0]!="."){tagsClass="."+tagsClass}var tags=[];$(tagsClass+" a").each(function(){tags.push($(this).text())});if(tags.length<1){this.reportError("Couldn't find any tags in this post")}return tags};RelatedPosts.prototype.getPosts=function(feed){var posts=[];var items=$(feed).find("item");for(var i=0;i"+error+""))}};$.fn.ghostRelated=function(options){return this.each(function(){new RelatedPosts(this,options)})}})(jQuery); \ No newline at end of file +!function(t){function e(e,r){this.element=e,this.options=t.extend({},defaults,r),this.parseRss()}defaults={feed:"/rss",titleClass:".post-title",tagsClass:".post-meta",limit:5,debug:!1},e.prototype.displayRelated=function(e){var r=this,n=0;this._currentPostTags=this.getCurrentPostTags(this.options.tagsClass);var o=this.matchByTag(this._currentPostTags,e);o.forEach(function(e){n'+e.title+"")),n++}),0==n&&t(this.element).append(t('

    No related posts were found. Check the index.

    '))},e.prototype.parseRss=function(e,r,n){var o=e||1,r=r||"",n=n||[],s=this;t.ajax({url:this.options.feed+"/"+o,type:"GET"}).done(function(e){var i=t(e).find("item > guid").text();if(i!=r)n.push(e),s.parseRss(o+1,i,n);else{var a=s.getPosts(n);s.displayRelated(a)}}).fail(function(t){s.reportError(t)})},e.prototype.getCurrentPostTitle=function(e){"."!=e[0]&&(e="."+e);var r=t(e).text();return r.length<1&&this.reportError("Couldn't find the post title with class: "+e),r},e.prototype.getCurrentPostTags=function(e){"."!=e[0]&&(e="."+e);var r=[];return t(e+" a").each(function(){r.push(t(this).text())}),r.length<1&&this.reportError("Couldn't find any tags in this post"),r},e.prototype.getPosts=function(e){var r=[],n=[];e.forEach(function(e){n=t.merge(n,t(e).find("item"))});for(var o=0;o"+e+""))},t.fn.ghostRelated=function(t){return this.each(function(){new e(this,t)})}}(jQuery); \ No newline at end of file