PDA

View Full Version : Display and sort messages based on a total, not distributed equally among the feeds.



RioBrewster
03-10-2015, 05:22 PM
1) Script Title: gfeedfetcher

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex18/gajaxrssdisplayer.htm

3) Describe problem: I have two feeds. One is updated monthly, the other is updated much more frequently, but pretty randomly.

What I want is to display and sort the most recent 6 items total - not the most recent 3 from each of the two feeds.

So I currently have 1 test message in the new monthly feed, (Sales Tax Allocations) and many messages in the original feed (Comptroller News).

If I set filterfeed to (6, date) I only get 4 messages. The test message from the new feed, then 3 messages from the original feed.

I will probably never want to display 3 messages from the new feed. The most would be 1 but it could possibly be zero.

I just want the 6 most recent messages regardless of which feed they are coming from.

Looking at the gfeedfetcher.js I can see where the code is figuring out how to distribute the messages among the feeds, but I can't figure out how to make it NOT do that. Can someone help?

The new feed goes live tomorrow. I can bump up the number under filterfeed as a hack for tomorrow, but of course that will break as soon as the second message comes through the Sales Tax Allocations feed.


Here's the page: http://www.window.state.tx.us/

but it doesn't have the second feed integrated yet.

Here's the code from the page on the dev server:


<script type="text/javascript">
var newsfeed=new gfeedfetcher("feed", "home-feed")
newsfeed.addFeed("Comptroller News", "https://public.govdelivery.com/topics/TXCOMPT_1007/feed.rss") //Specify "label" plus URL to RSS feed
newsfeed.addFeed("Sales Tax Allocations", "https://public.govdelivery.com/topics/TXCOMPT_1026/feed.rss")
newsfeed.displayoptions("datetime label")
newsfeed.setentrycontainer("li") //Display each entry as a paragraph
newsfeed.filterfeed(6, "date") //Show 6 entries, sort by date
newsfeed.init() //Always call this last
</script>

vwphillips
03-11-2015, 04:17 PM
changes to two functions in red


gfeedfetcher.prototype.init=function(){
this.feedsfetched=0 //reset number of feeds fetched to 0 (in case init() is called more than once)
this.feeds=[] //reset feeds[] array to empty (in case init() is called more than once)
this.feedcontainer.innerHTML='<img src="'+gfeedfetcher_loading_image+'" /> Retrieving RSS feed(s)'
var displayer=this
for (var i=0; i<this.feedurls.length; i++){ //loop through the specified RSS feeds' URLs
var feedpointer=new google.feeds.Feed(this.feedurls[i]) //create new instance of Google Ajax Feed API
var items_to_show=25;//(this.feedlimit<=this.feedurls.length)? 1 : Math.floor(this.feedlimit/this.feedurls.length) //Calculate # of entries to show for each RSS feed
if (this.feedlimit%this.feedurls.length>0 && this.feedlimit>this.feedurls.length && i==this.feedurls.length-1) //If this is the last RSS feed, and feedlimit/feedurls.length yields a remainder
items_to_show+=(this.feedlimit%this.feedurls.length) //Add that remainder to the number of entries to show for last RSS feed
feedpointer.setNumEntries(items_to_show) //set number of items to display
feedpointer.load(function(label){
return function(r){
displayer._fetch_data_as_array(r, label)
}
}(this.feedlabels[i])) //call Feed.load() to retrieve and output RSS feed.
}
}




gfeedfetcher.prototype._displayresult=function(feeds){
var rssoutput=(this.containertag[0]=="li")? "<ul>\n" : ""
gfeedfetcher._sortarray(feeds, this.sortstring)
var itemurl=[], itemtitle=[], itemlabel=[], itemdate=[], itemdescription=[]
for (var i=0; i<this.feedlimit; i++){
itemurl.push(feeds[i].link)
itemtitle.push('<span class="titlefield"><a href="' + feeds[i].link + '" target="' + this.linktarget + '">' + feeds[i].title + '</a></span>\n')
itemlabel.push(/label/i.test(this.showoptions)? '<span class="labelfield">'+this.feeds[i].ddlabel+'</span>\n' : "")
itemdate.push(gfeedfetcher._formatdate(feeds[i].publishedDate, this.showoptions))
var itemdescriptionsingle=/description/i.test(this.showoptions)? feeds[i].content : /snippet/i.test(this.showoptions)? feeds[i].contentSnippet : ""
itemdescriptionsingle=(itemdescriptionsingle!="")? '<span class="descriptionfield">' + itemdescriptionsingle + '</span>\n' : ""
itemdescription.push(itemdescriptionsingle)
}
// create temp object to store references to rss components, for access dynamically:
var holder={urlfield: itemurl, titlefield: itemtitle, labelfield: itemlabel, datefield: itemdate, descriptionfield: itemdescription}
var regexprules=this.regexprules
for (var i=(this.regexprules && this.regexprules.length>0? this.regexprules.length-1 : -1); i>=0; i--){ // loop thru regexprules array
if (regexprules[i][2]=="titlefield" || regexprules[i][2]=="labelfield" || regexprules[i][2]=="datefield" || regexprules[i][2]=="descriptionfield"){
var targetarray=holder[regexprules[i][2]] // reference array containing said field type (ie: itemdescription if regexprules[i][2]=="descriptionfield")
targetarray=targetarray.join('***delimiter***') // combine array elements before doing search and replace
.replace(regexprules[i][0], regexprules[i][1])
.split('***delimiter***') // revert back to array
holder[regexprules[i][2]]=targetarray
regexprules.splice(i,1) // remove this rule from regexprules
}
}
for (var i=0; i<this.feedlimit; i++){ // loop thru feeds, molding each feed entry based on template
rssoutput+= this.containertag[1] + this.outputtemplate.replace(/({title})|({url})|({label})|({date})|({description})/ig, function(m){
if (m == "{title}")
return holder.titlefield[i]
else if (m == "{url}")
return holder.urlfield[i]
else if (m == "{label}")
return holder.labelfield[i]
else if (m == "{date}")
return holder.datefield[i]
else if (m == "{description}")
return holder.descriptionfield[i]
}) + "</" + this.containertag[0] + ">" + "\n\n"
}
rssoutput+=(this.containertag[0]=="li")? "</ul>" : ""
for (var i=0; i<this.regexprules.length; i++){ // loop thru remaining regexprules array that target the entire feed in general (versus specific field)
rssoutput=rssoutput.replace(this.regexprules[i][0], this.regexprules[i][1])
}
this.feedcontainer.innerHTML=rssoutput
}

RioBrewster
03-11-2015, 06:56 PM
Thank you very much!

One question though. Why items_to_show=25 ?

Just some randomly large number?

vwphillips
03-12-2015, 10:15 AM
Just some randomly large number?

yes