Anonymous user
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
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}
{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▼
in▼
fun {OpenURL Url Params}▼
OutParams▼
{Client getService(Url Params ?OutParams ?_)}▼
OutParams.sOut▼
fun {Page Nr}
StartResult = (Nr-1)*10+1
%% only retrieve it
Doc = {Value.byNeed fun {$}
{
["p"#Query "b"#{Int.toString StartResult}]}
end}
RE = "<a class=\"yschttl spt\" href="
in
%%
%% In this way it is possible to build the pages list structure
%% without creating the single elements
%% (e.g. retrieve page 1 and 3 but not 2).
for
Xs = {List.drop Doc
in
{Yield {ParseEntry Xs}}
Line 180 ⟶ 172:
end
fun {
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}
in
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
%%
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
fun {QuotedString &"|Xs ?Ys}
fun {Loop Xs Ys}
|