Yahoo! search interface: Difference between revisions

→‎{{header|Oz}}: simplified, commented
m (→‎{{header|Oz}}: removed debug stmt.)
(→‎{{header|Oz}}: simplified, commented)
Line 122:
{{libheader|OzHttpClient}}
 
Instead of a class the implementation defines a function which returns a lazy list of result pages. This also makes it possible to request e.g. the first and the third page without any ressourcesresources wasted on an unneeded second page.
 
We implement some simple parsing with logic programming. Regular expressions in Oz don't seem to support lazy quantification which makes parsing the result pages with them difficult.
Line 130:
[Regex] = {Module.link ['x-oz://contrib/regex']}
 
%% Displays page 1 and 3 of the search results.
%% The user can request and display more with context menu->Actions->Make Needed.
proc {ExampleUsage}
Pages = {YahooSearch "Rosetta code"}
in
{Inspector.configure widgetShowStrings true}
%% Make pages 1 and 3 needed, i.e. they are retrieved and displayed
%% without further user interaction
{ForAll {Nth Pages 1} Value.makeNeeded}
{ForAll {Nth Pages 3} Value.makeNeeded}
Line 143:
 
%% Returns a lazy list of pages.
%% A page is a lazy list of entries like this: result(url:U title:T content:C).
fun {YahooSearch Query}
FetchURL = {CreateURLFetcher}
local
Client = {CreateClient}
in
fun {OpenURL Url Params}
OutParams
in
{Client getService(Url Params ?OutParams ?_)}
OutParams.sOut
end
end
fun {Page Nr}
StartResult = (Nr-1)*10+1
%% only retrieve it whewhen really needed
Doc = {Value.byNeed fun {$}
{OpenURLFetchURL "http://search.yahoo.com/search"
["p"#Query "b"#{Int.toString StartResult}]}
end}
RE = "<a class=\"yschttl spt\" href="
in
%% WeLazily yield thereturns results lazily because we want to be able.
%% In this way it is possible to build the pages list structure without necessarily building
%% without creating the single elements (e.g. retrieve page 1 and 3 but not 2).
%% (e.g. retrieve page 1 and 3 but not 2).
for match(0:_#E ...)Match in {Regex.allMatches RE Doc} yield:Yield do
Xs = {List.drop Doc EMatch.0.2}
in
{Yield {ParseEntry Xs}}
Line 180 ⟶ 172:
end
 
fun {CreateClientCreateURLFetcher}
Client = {New HTTPClient.cgiGET
init(inPrms(toFile:false toStrm:true)
httpReqPrms
)}
%% close when no longer used
{Finalize.register Client proc {$ C} {C closeAll(true)} end}
 
fun {OpenURLFetchURL Url Params}
OutParams
localin
{Client getService(Url Params ?OutParams ?_)}
OutParams.sOut
inend
in
ClientFetchURL
end
 
%% Xs: String containing HtmL
%% Result: "result(url:U title:T content:C)" or "parseError"
fun {ParseEntry Xs}
proc {Parse Root}
Line 240 ⟶ 242:
end
 
%% AssertAsserts that Xs starts with C. ReturnReturns the restremainder in Ys.
proc {Const C Xs ?Ys}
{List.takeDrop Xs {Length C} C Ys}
Line 246 ⟶ 248:
 
%% Assert that a quoted string follows.
%% Returns the unquoted string and binds Ys to the restremainder of Xs.
fun {QuotedString &"|Xs ?Ys}
fun {Loop Xs Ys}
Anonymous user