forked from yql/yql-tables
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd"> | ||
<meta> | ||
<sampleQuery>select * from {table};</sampleQuery> | ||
<author>Tom Hughes-Croucher (@sh1mmer, croucher@yahoo-inc.com)</author> | ||
</meta> | ||
<bindings> | ||
<select itemPath="stories.story" produces="XML"> | ||
<urls> | ||
<url>http://news.ycombinator.com/news</url> | ||
</urls> | ||
<inputs> | ||
</inputs> | ||
<execute><![CDATA[ | ||
url = "http://news.ycombinator.com/news"; | ||
page = y.query("select * from html where url='" + url + "'").results; | ||
table = page..table[2]; | ||
rows = table..tr; | ||
output = <stories></stories> | ||
/* | ||
* iterate three TRs at a time | ||
* each triplet represents one story | ||
* i is the title row, i+1 is the meta info, i+2 is a spacer | ||
*/ | ||
for(var i=0;i<90;i=i+3) { | ||
titleRow = rows[i] | ||
metaRow = rows[i+1] | ||
title = titleRow..td[2].a; | ||
url = titleRow..td[2].a.@href; | ||
submitter = metaRow..a[0] | ||
score = /^\d+/.exec(metaRow..span[0].toString())[0] | ||
comments = /^\d+/.exec(metaRow..a[1].toString()) | ||
if(comments) { | ||
comments = comments[0] | ||
} else { | ||
comments = 0; | ||
} | ||
id = /\d+$/.exec(metaRow..a[1].@href.toString())[0] | ||
story = <story></story> | ||
story.id = id; | ||
story.title = title.toString(); | ||
story.url = url; | ||
story.submitter = submitter.toString(); | ||
story.score = score; | ||
story.comments = comments; | ||
output.stories += story; | ||
} | ||
response.object = output; | ||
]]></execute> | ||
</select> | ||
</bindings> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd"> | ||
<meta> | ||
<sampleQuery>select * from {table} where id = 1160643</sampleQuery> | ||
<author>Tom Hughes-Croucher (@sh1mmer, croucher@yahoo-inc.com)</author> | ||
</meta> | ||
<bindings> | ||
<select itemPath="" produces="XML"> | ||
<urls> | ||
<url></url> | ||
</urls> | ||
<inputs> | ||
<key id='id' type='xs:string' paramType='variable' required="true" /> | ||
</inputs> | ||
<execute><![CDATA[ | ||
url = "http://news.ycombinator.com/item?id=" + id | ||
page = y.query("select * from html where url='" + url + "'").results; | ||
postInfo = page..table[2]; | ||
comments = page..table[3]..table; | ||
values = {} | ||
values['id'] = id; | ||
values['title'] = y.xpath(postInfo, "//td[@class='title']/a/text()"); | ||
values['url'] = y.xpath(postInfo, "//td[@class='title']/a/@href"); | ||
//match the numbers in the span | ||
values['score'] = /^\d+/.exec(y.xpath(postInfo, "//td[@class='subtext']/span/text()")); | ||
values['submitter'] = y.xpath(postInfo, "//td[@class='subtext']//a[1]/text()"); | ||
//get the age by looking for a number followed by minute/minutes/hour/hours/etc | ||
values['age'] = /\d+ (?:minute|hour|day|week|month)s?/.exec(y.xpath(postInfo, "//td[@class='subtext']/p/text()[2]")); | ||
for(table in comments) { | ||
depth = y.xpath(postInfo, "//td//img"); | ||
if(depth > 0) { | ||
//not root comment | ||
} | ||
} | ||
output = keyValsToXml(values, "story"); | ||
//output.raw += <raw>{comments}</raw>; | ||
response.object = output; | ||
//take a native object and turn its attributes into a chunk of xml wrapped by a root node with name rootName | ||
function keyValsToXml(keyVals, rootName) { | ||
output = <{rootName}></{rootName}>; | ||
for(name in keyVals) { | ||
output.node += <{name}>{keyVals[name]}</{name}>; | ||
} | ||
return output; | ||
} | ||
]]></execute> | ||
</select> | ||
</bindings> | ||
</table> |