Source: site.view [edit]
Function name: findCheapestBook
Arguments: topic
Description: A command to find cheapest book for a topic -> finds ref:cheapest matching amazon book, returns detail page to it. -> Example input: topic='Harry Potter'
Page type: webl
Render function:   tlidRender
Module: global

Page source:

var P;
var tmp1;
var context;

context = NLUtil_InitContext();

//|| go to http://www.amazon.com
context = NLUtil_GetUrl(context, "http://www.amazon.com", nil, nil);
P = NLUtil_GetContext(context, `P`);

//|| find all forms
P = NLUtil_GetContext(context,`P`);
context = NLUtil_SetContext(context,`form`, Elem(P, `form`) );

//|| select the first one
context = NLUtil_SetContext(context,NLUtil_LastType(context), Select(NLUtil_LastSet(context),0,1));

//|| field url is Books
context = NLUtil_SetField(context, "url", `Books`);

//|| field keywords is the topic
context = NLUtil_SetField(context, "keywords", topic);

//|| submit the form
context = NLUtil_submitForm(context);
P = NLUtil_GetContext(context, `P`);

//|| find all forms that contain "sort by"
P = NLUtil_GetContext(context,`P`);
context = NLUtil_SetContext(context,`form`, Elem(P, `form`)  contain Pat(P, `(?i)` + `sort by`));

//|| field sort is "Price: Low to High"
context = NLUtil_SetField(context, "sort", `Price: Low to High`);

//|| submit
context = NLUtil_submitForm(context);
P = NLUtil_GetContext(context, `P`);

//|| find all links that contain "Buy"
P = NLUtil_GetContext(context,`P`);
context = NLUtil_SetContext(context,`a`, Elem(P, `a`)  contain Pat(P, `(?i)` + `Buy`));

//|| select the first one
context = NLUtil_SetContext(context,NLUtil_LastType(context), Select(NLUtil_LastSet(context),0,1));

//|| click it
context = NLUtil_clickLink(context);
P = NLUtil_GetContext(context, `P`);

context = NLUtil_SetContext(context, `P`, P);

//|| return the page

if Size(OPTIONALARGS) == 0 then
   NLUtil_LastValue(context);
else
   context;
end;