Saturday, September 13, 2008

Add a newsfeed to your site using coldfusion

you can use the moreover news service to add a news feed to your site using cfhttp and wddx.
To begin, you need to know what type of news feed your are interested in. Moreover has a list of pre-built news feeds that are encoded using the wdd standard available here.
Once you've chosen the specific news feed you are interested in, set the url for that feed into the feed variable. The feed below is for "Tech Latest".

<cfset feed = "http://p.moreover.com/cgi-local/page?c=Tech%20latest&o=wddx">
<cftry>
<!--- use cfhttp to get the feed --->
<cfhttp url="#feed#" method="GET" resolveurl="false"></cfhttp>
<!--- use cfwddx to convert the feed results into a query. The query name is defined in the "output" paramter in the tag below --->
<cfwddx action="WDDX2CFML" input="#CFHTTP.FileContent#" output="newsfeed_data">

<!---
if no results were found or any other errors occur, make a fake query -
this allows us to avoid having to do additional error handling when the feed results are displayed.
--->

<cfcatch>
<CFSET newsfeed_data = QueryNew("headline_text, source, harvest_time, URL")>
<!--- make a row in the query --->
<CFSET newRow = QueryAddRow(newsfeed_data, 1)>
<!--- set the cells in the query --->
<CFSET temp = QuerySetCell(newsfeed_data, "headline_text", "No News Items Available",1)>
</cfcatch>
</cftry>
<style type="text/css">
th {
font-family: Verdana, Helvitica, Arial;
font-size: 78%;
color: white;
background-color: #336600;
font-weight: bold;
}
.data {
border: 1px solid #003366;
background-color: #003366;
}
</style>


<!---
If you want to see what data is available in our query, dump the whole query
<cfdump var="#newsfeed_data#">
<cfabort>
--->
To set how many stories will display at one time, you can set the maxrows property of the output query.
To show all stories, set a default to the recordcount of the query. To decrease the number, just type in the
number of stories you want to display.

<cfparam name="maxrows" default="#newsfeed_data.recordcount#">
<!--- display our results --->
<table cellpadding="4" cellspacing="1" class="data" summary="news feed results">
<tr>
<th>Current <cfoutput>#newsfeed_data.cluster#</cfoutput> Stories</th>
</tr>
<cfoutput query="newsfeed_data" maxrows="#maxrows#">
<tr bgcolor="##<cfif currentrow mod 2 is 1>FFFFFF<cfelse>CCCCCC</cfif>">
<td>
<a href='#url#' target='_blank'>#headline_text#</a><br><span class='news'>(<strong>#source#</strong>) on #harvest_time#</span><br>
</td>
</tr>
</cfoutput>
</table>

No comments: