forked from yql/yql-tables
/
hackernews.frontpage.xml
60 lines (52 loc) · 2.19 KB
/
hackernews.frontpage.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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>