Lasso Soft Inc. > Home

[yahoo_search]

Linkyahoo_search
AuthorJason Huck
CategoryUtility
Version8.5.x
Licensehttp://opensource.org/licenses/artistic-license.php
Posted20 Feb 2007
Updated15 Mar 2007
More by this author...

Description

This tag retrieves search results from Yahoo!'s REST Web Search API. Returns a dictionary containing the result details. Requires [dictionary], [xml_tree], and a valid Yahoo! Application ID. All of the parameters shown in the documentation can be passed to the tag. Note that if you wish to change the type of search performed,  you must use 'searchtype' as the param name instead of 'type' to avoid conflicts with Lasso.

The following values are accessible in the resulting object:

->query - The original query used to create the results.
->found - The total found count (note: tends to vary).
->shown - The number of results shown.
->first - The position of the first result shown.
->last - The position of the last result shown.
->previous - The position needed to recreate the previous set of results.
->next - The position needed to create the next set of results.
->results - An array of dictionary objects containing the result data.

Each item in the results array contains the following values:
->title - The title of the page.
->summary - A summary or excerpt of the page.
->url - The URL of the page.
->clickurl - The same URL  through Yahoo!'s click-tracking mechanism.

Sample Usage

var('sr') = yahoo_search(
	-appid='xxxxxx',
	-query=decode_url(action_param('query')),
	-start=(action_param('skip') ? integer(action_param('skip')) | 1),
	-site='www.yoursite.com'
);

if($sr->size);
	'
	

Displaying results ' + $sr->first + ' through ' + $sr->last + ' of ' + $sr->found + ' found for "' + $sr->query + '."

'; iterate($sr->results, local('i')); '
' + #i->title + '
\n'; '
' + #i->summary + '
\n'; /iterate; '
'; else; 'No results found.'; /if; $sr->previous ? '[Previous]'; $sr->next ? '[Next]';

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_tag(
	'search',
	-namespace='yahoo_',
	-req='appid',
	-req='query',
	-opt='region',
	-opt='searchtype',
	-opt='results',
	-opt='start',
	-opt='format',
	-opt='adult_ok',
	-opt='similar_ok',
	-opt='language',
	-opt='country',
	-opt='site',
	-opt='subscription',
	-opt='license',
	-opt='output',
	-opt='callback',
	-priority='replace',
	-description='Returns search results via Yahoo!'
);
	!local_defined('results') ? local('results') = 10;
	
	local('getparams') = array;
	
	iterate(params, local('i'));
		local('p') = #i->first;
		#p->removeleading('-');
		#p == 'searchtype' ? #p = 'type';
		#getparams->insert(#p = #i->second);
	/iterate;
	
	protect;
		local('resp') = xml_tree(
			string(
				include_url(
					'http://search.yahooapis.com/WebSearchService/V1/webSearch',
					-getparams=#getparams,
					-timeout=10
				)
			)
		);
		
		handle_error;
			return;
		/handle_error;
	/protect;
	
	local('out') = dictionary(
		'query' = #query,
		'found' = integer(#resp->totalResultsAvailable),
		'shown' = integer(#resp->totalResultsReturned),
		'first' = integer(#resp->firstResultPosition),
		'results' = array
	);
	
	#out->last = #out->first + #out->shown - 1;
	#out->previous = (#out->first > 1 ? #out->first - #results | null);
	#out->next = (#out->first + #results - 1 < #out->found ? #out->first + #results | null);
	
	// correct for yahoo's strange behavior on the last page of results
	#out->previous < 0 ? #out->previous = null;
	#out->shown < #results ? #out->next = null;
	
	iterate(#resp->Result, local('i'));
		#out->results->insert(
			dictionary(
				'title' = #i->Title->contents,
				'summary' = #i->Summary->contents,
				'url' = #i->Url->contents,
				'clickurl' = #i->ClickUrl->contents
			)
		);
	/iterate;
	
	return(#out);
/define_tag;

Related Tags

Comments

15 Mar 2007, Jason Huck

Updated.

Added some workarounds for how previous/next skip counts are calculated to account for some funky math returned by the service on the last set of search results.

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft