forked from zotero/translators
/
Flickr.js
242 lines (234 loc) · 7.79 KB
/
Flickr.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
{
"translatorID": "5dd22e9a-5124-4942-9b9e-6ee779f1023e",
"label": "Flickr",
"creator": "Sean Takats",
"target": "^http://(?:www\\.)?flickr\\.com/",
"minVersion": "1.0.0b4.r5",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "g",
"lastUpdated": "2011-11-01 16:31:25"
}
function detectWeb(doc, url) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
if (elmt = doc.evaluate('//h1[@property="dc:title" and starts-with(@id, "title_div")]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
return "artwork";
} else if (doc.evaluate('//td[@class="DetailPic"]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
return "multiple";
} else if (doc.evaluate('//div[contains(@class, "StreamView")]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
return "multiple";
} else if (doc.evaluate('//div[@id="setThumbs"]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
if (!doc.URL.match('/comments/')) {
return "multiple";
}
} else if (doc.evaluate('//p[@class="StreamList" or @class="UserTagList"]/span/a', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
return "multiple";
}
}
function doWeb(doc, url) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
var items = new Object();
var photo_ids = new Array();
var uris = new Array();
var key = "3cde2fca0879089abf827c1ec70268b5";
var elmts;
var elmt;
// single result
if (elmt = doc.evaluate('//h1[@property="dc:title" and starts-with(@id, "title_div")]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
var photo_id = elmt.id;
photo_id = photo_id.substr(9);
photo_ids.push(photo_id);
} else { //multiple results
var photoRe = /\/photos\/[^\/]*\/([0-9]+)\//;
//search results
if (doc.evaluate('//td[@class="DetailPic"]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
elmts = doc.evaluate('//td[@class="DetailPic"]//a', doc, nsResolver, XPathResult.ANY_TYPE, null);
while (elmt = elmts.iterateNext()){
var title = elmt.title;
title = Zotero.Utilities.trimInternal(title);
var link = elmt.href;
var m = photoRe(link);
var photo_id = m[1];
items[photo_id] = title;
}
// photo stream
} else if (doc.evaluate('//div[contains(@class, "StreamView")]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
if (doc.evaluate('//div[contains(@class, "StreamView") and starts-with(@id, "sv_title_")]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
elmts = doc.evaluate('//div[contains(@class, "StreamView") and starts-with(@id, "sv_title_")]', doc, nsResolver, XPathResult.ANY_TYPE, null);
} else {
elmts = doc.evaluate('//div[contains(@class, "StreamView") and starts-with(@id, "sv_body_")]', doc, nsResolver, XPathResult.ANY_TYPE, null);
}
while (elmt = elmts.iterateNext()){
//var title = Zotero.Utilities.trimInternal(elmt.textContent);
var title = elmt.getElementsByTagName("h4")[0].textContent
var photo_id = elmt.id;
photo_id = photo_id.replace(/(sv_body_|sv_title_)/,'');
Zotero.debug("id="+photo_id)
items[photo_id] = title;
}
// photo set
} else if (doc.evaluate('//div[@class="setThumbs-indv"]/span', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
elmts = doc.evaluate('//div[@class="setThumbs-indv"]/span', doc, nsResolver, XPathResult.ANY_TYPE, null);
while (elmt = elmts.iterateNext()){
var title = doc.evaluate('./a/@title', elmt, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
var photo_id = elmt.id.substr(11);
items[photo_id] = title;
}
// tagged with
} else if (doc.evaluate('//p[@class="StreamList" or @class="UserTagList"]/span/a', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
var elmts = doc.evaluate('//p[@class="StreamList" or @class="UserTagList"]//a[img]', doc, nsResolver, XPathResult.ANY_TYPE, null);
while (elmt = elmts.iterateNext()){
var title = Zotero.Utilities.trimInternal(elmt.title);
var link = elmt.href;
var m = photoRe(link);
var photo_id = m[1];
items[photo_id] = title;
}
}
items = Zotero.selectItems(items);
if(!items) return true;
for(var i in items) {
photo_ids.push(i);
}
}
for each(var photo_id in photo_ids){
uris.push("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key="+key+"&photo_id="+photo_id);
}
Zotero.Utilities.HTTP.doGet(uris, function(text) {
text = text.replace(/<\?xml[^>]*\?>/, "");
var xml = new XML(text);
var newItem = new Zotero.Item("artwork");
var title = "";
if (xml..title.length()){
var title = Zotero.Utilities.trimInternal(xml..title[0].text().toString());
if (title == ""){
title = " ";
}
newItem.title = title;
}
for(var i=0; i<xml..tag.length(); i++) {
newItem.tags.push(Zotero.Utilities.trimInternal(xml..tag[i].text().toString()));
}
if (xml..dates.length()){
var date = xml..dates[0].@taken.toString();
newItem.date = date.substr(0, 10);
}
if (xml..owner.length()){
var author = xml..owner[0].@realname.toString();
if (author == ""){
author = xml..owner[0].@username.toString();
}
newItem.creators.push(Zotero.Utilities.cleanAuthor(author, "artist"));
}
if (xml..url.length()){
newItem.url = xml..url[0].text().toString();
}
if (xml..description.length()){
newItem.abstractNote = xml..description[0].text().toString();
}
var format = xml..photo[0].@originalformat.toString();
var photo_id = xml..photo[0].@id.toString();
// get attachment code
var uri = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key="+key+"&photo_id="+photo_id;
Zotero.Utilities.HTTP.doGet(uri, function(text) {
text = text.replace(/<\?xml[^>]*\?>/, "");
var xml = new XML(text);
var last = xml..size.length() - 1;
var attachmentUri = xml..size[last].@source.toString();
newItem.attachments = [{title:title, url:attachmentUri}];
newItem.complete();
}, function(){Zotero.done();});
});
Zotero.wait();
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://www.flickr.com/photos/doug88888/3122503680/in/set-72157624194059533",
"items": [
{
"itemType": "artwork",
"creators": [
{
"firstName": "",
"lastName": "doug88888",
"creatorType": "artist"
}
],
"notes": [],
"tags": [
"nature",
"plant",
"living",
"green",
"frosty",
"blue",
"bokeh",
"canon",
"eos",
"400d",
"18mm",
"55mm",
"gimp",
"pretty",
"beautiful",
"tones",
"ham",
"house",
"richmond",
"south",
"west",
"bloom",
"flower",
"grass",
"strand",
"lone",
"isolated",
"isolation",
"uk",
"england",
"doug88888",
"southwest",
"leaf",
"fall",
"bright",
"blossom",
"fresh",
"december",
"dec07",
"buy",
"purchase",
"picture",
"pictures",
"image",
"images",
"creative",
"commons"
],
"seeAlso": [],
"attachments": [
{
"title": "The blues and the greens EXPLORED",
"url": "http://farm4.static.flickr.com/3123/3122503680_739103322d_o.jpg"
}
],
"title": "The blues and the greens EXPLORED",
"date": "2008-12-07",
"url": "http://www.flickr.com/photos/doug88888/3122503680/",
"abstractNote": "More xmas shopping today - gulp.\n\nCheck out my <a href=\"http://doug88888.blogspot.com/\">blog</a> if you like.",
"libraryCatalog": "Flickr",
"accessDate": "CURRENT_TIMESTAMP"
}
]
}
]
/** END TEST CASES **/