Skip to content

Commit

Permalink
adding hacker news tables
Browse files Browse the repository at this point in the history
  • Loading branch information
sh1mmer committed Mar 2, 2010
1 parent 03b50ce commit b40a590
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
60 changes: 60 additions & 0 deletions hackernews/hackernews.frontpage.xml
@@ -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>
57 changes: 57 additions & 0 deletions hackernews/hackernews.story.xml
@@ -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>

0 comments on commit b40a590

Please sign in to comment.