forked from yql/yql-tables
/
flickr.photos.astro.xml
130 lines (119 loc) · 4.66 KB
/
flickr.photos.astro.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?xml version = "1.0"?>
<table xmlns = "http://query.yahooapis.com/v1/schema/table.xsd" >
<meta >
<author > Jim O 'Donnell</author>
<documentationURL>http://eatyourgreens.org.uk/archives/2009/05/searching-the-sky-with-yql-execute.html</documentationURL>
<sampleQuery description="Get up to 50 photos of M 42 (the Orion Nebula), starting at number 10.">select * from flickr.photos.astro(10,50) where astro_name='M 42'</sampleQuery>
</meta>
<bindings>
<select produces="XML" itemPath="photos.photo">
<urls>
<url></url>
</urls>
<paging model="offset">
<start id="start" default="0"/>
<pagesize id="count" max="250"/>
<total default="20"/>
</paging>
<inputs>
<key id="text" type="xs:string" paramType="variable" />
<key id="astro_name" type="xs:string" paramType="variable" />
<key id="group_id" type="xs:string" paramType="variable" />
<key id="api_key" type="xs:string" paramType="variable" />
</inputs>
<execute><![CDATA[
var name_query = '';
var api_query = '';
if (text && text != '') name_query += " and text = '"+text.replace("'","\'")+"'";
if (api_key && api_key != '') api_query = " and api_key = '"+api_key+"'";
if (group_id && group_id != '') name_query += " and group_id = '"+group_id.replace("'","\'")+"'";
var machine_tags = 'astro:RA=';
if (astro_name && astro_name != '') machine_tags = 'astro:name="'+astro_name.replace("'","\'")+'"';
var querystring = "select farm, server, id, secret, title, owner.username, urls.url.content, tags.tag.raw from flickr.photos.info where (tags.tag.raw like 'astro:%')"+api_query+" and photo_id in (select id from flickr.photos.search("+start+","+count+") where machine_tags='"+machine_tags+"'"+name_query+api_query+")";
var data = y.query(querystring);
var photos = parseAstrotags(data);
default xml namespace = '';
var respdata = <photos/>;
for each (var photo in photos) {
var node = <photo>
<id>{photo.id}</id>
<title>{photo.title}</title>
<url>{photo.url}</url>
<imgroot>{photo.imgroot}</imgroot>
<username>{photo.username}</username>
<ra>{photo.RA}</ra>
<dec>{photo.Dec}</dec>
<orientation>{photo.orientation}</orientation>
</photo>;
if(photo.fov) {
node.photo += <fov>
<x>{photo.fov.x}</x>
<y>{photo.fov.y}</y>
</fov>;
}
for(i=0; i<photo.name.length; i++) {
node.photo += <name>{photo.name[i]}</name>;
}
respdata.photos += node;
}
// try extending cache time as these
// results don't change very often.
response.maxAge = 1800;
response.object = respdata;
function parseAstrotags(data) {
var tag;
var tmp;
var photo;
var id;
var o = {};
o.id = '';
var photos = [];
/* Walk through the returned data and build an array of
photos. */
for each (var photo in data.results.photo) {
id = photo.@['id'].toString();
if (o.id != id) {
o = {};
photos.push(o);
o['name'] = [];
o['id'] = id;
o['title']=photo.title.toString();
o['url'] = photo.urls.url.toString();
o['username'] = photo.owner.@['username'].toString();
o['imgroot'] = 'http://farm'+photo.@['farm'].toString()+'.static.flickr.com/'+photo.@['server'].toString()+'/'+id+'_'+photo.@['secret'].toString();
}
tag = photo.tags.tag.@['raw'].toString();
//split machine tag name and value
tmp = tag.split('=');
//discard namespace from tag name by splitting tmp[0] on :
// store data as o[id][predicate] = value eg. o[id].RA = 85.123456
var tagname = tmp[0].split(':')[1];
if (tagname == 'name') {
o['name'].push(tmp[1]);
} else {
o[tagname] = tmp[1];
}
//fieldsize is a string in degrees, arcminutes or arcseconds.
//We will standardise on a value in degrees.
if (tmp[0] == 'astro:fieldsize') {
//convert fieldsize string into a numeric value in degrees, using the width of the photo.
tmp = o.fieldsize.split(' ');
o.fov = {};
o.fov.x = parseFloat(tmp[0]);
o.fov.y = parseFloat(tmp[2]);
if (tmp[3] == 'arcminutes') {
o.fov.x = o.fov.x/60;
o.fov.y = o.fov.y/60;
}
if (tmp[3] == 'arcseconds') {
o.fov.x = o.fov.x/3600;
o.fov.y = o.fov.y/3600;
}
}
}
return photos;
}
]]></execute>
</select>
</bindings>
</table>