Followup to Yahoo Search API Post

Yesterday I wrote a quick post about using Yahoo’s search API. I wanted to follow it up with a slightly more useful example. On of the commenters on my last post noted that Yahoo seemed to respond pretty quickly, and I have to agree. The results take little to no time to return. Anyway, on to the example. This example once again uses the search API to return results. However, this time I’m doing something different with the results. Let me show the code first and then I’ll explain.

<cfset searchTerm = "coldfusion blog"> <cfset mysite = "camdenfamily.com"> <cfset results = "100"> <cfset appid = "no_you_cant_have_this">

<cfset matches = arrayNew(1)>

<cfhttp url=”http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=#appid#&query=#urlEncodedFormat(searchTerm)#&results=#results#” result=”result” charset=”utf-8” />

<cfif len(result.fileContent) and isXml(result.fileContent)>

&lt;cfset xmlResult = xmlParse(result.fileContent)&gt;

&lt;cfloop index="x" from="1" to="#xmlResult.resultSet.xmlAttributes.totalResultsReturned#"&gt;
	&lt;cfset node = xmlResult.resultSet.xmlChildren[x]&gt;
	&lt;cfset title = node.title.xmlText&gt;
	&lt;cfset summary = node.summary.xmlText&gt;
	&lt;cfset iUrl = node.url.xmlText&gt;
	&lt;cfset clickurl = node.clickurl.xmlText&gt;
	
	&lt;cfif findNoCase(mysite, iURL)&gt;
		&lt;cfset match = structNew()&gt;
		&lt;cfset match.title = title&gt;
		&lt;cfset match.summary = summary&gt;
		&lt;cfset match.url = iurl&gt;
		&lt;cfset match.position = x&gt;
		&lt;cfset arrayAppend(matches, match)&gt;
	&lt;/cfif&gt;
	
&lt;/cfloop&gt;

</cfif>

<cfif arrayLen(matches)> <cfmail to=”ray@camdenfamily.com” from=”egowatcher@egoaddict.com” subject=”Your daily ego check.”> Here is your daily ego match for #dateFormat(now(), “long”)#

&lt;cfloop index="x" from="1" to="#arrayLen(matches)#"&gt;
#matches[x].title# (#matches[x].url#)
Position: #matches[x].position#
#matches[x].summary#	
&lt;/cfloop&gt;
&lt;/cfmail&gt; &lt;/cfif&gt; </code>

The code begins pretty much as yesterday’s did, except I added a new variable, “mysite”. Yahoo lets you filter search results by site, but that is not what I’m doing. (Jeff posted a perfect example of that in this comment.) Instead, scroll down to this line:

<cfif findNoCase(mysite, iURL)>

What I’ve done here is to say that if my site is found in the url then I consider it a match. I add the relevant data to an array of matches. The end of the code block simply checks to see if any matches were found and if so - mails it to me. Here is an example of the output:

Here is your daily ego match for September 25, 2006

Raymond Camden's ColdFusion Blog: BSG spin off in the works (and a quick note) (http://ray.camdenfamily.com/index.cfm/2006/4/27/BSG-spin-off-in-the-works-and-a-quick-note)
Position: 14
A blog for ColdFusion, Java, and other topics.: BSG spin off in the works (and a ... Subscribe to Raymond Camden's ColdFusion Blog. BSG spin off in the works ... Raymond Camden's ColdFusion Blog: BlogCFC 5 Beta Announced (http://ray.camdenfamily.com/index.cfm/2006/4/28/BlogCFC-5-Beta-Announced)
Position: 18
A blog for ColdFusion, Java, and other topics.: BlogCFC 5 Beta Announced ... Subscribe to Raymond Camden's ColdFusion Blog. BlogCFC 5 Beta Announced ...

So what’s the point of all this? I can set up this script to run daily and check the position of my site on Yahoo’s search engine when people are searching for “coldfusion blog”. (And geeze - 14th? What am I doing wrong? :) This can let me monitor my marketing efforts and see what is working and what is not working.

I hope this “real” example helps!

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments