-
Notifications
You must be signed in to change notification settings - Fork 767
/
Flickr.js
149 lines (142 loc) · 5.69 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
{
"translatorID":"5dd22e9a-5124-4942-9b9e-6ee779f1023e",
"translatorType":4,
"label":"Flickr",
"creator":"Sean Takats",
"target":"^http://(?:www\\.)?flickr\\.com/",
"minVersion":"1.0.0b4.r5",
"maxVersion":"",
"priority":100,
"inRepository":true,
"lastUpdated":"2008-06-01 17:20:00"
}
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[@class="StreamView"]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
return "multiple";
} else if (doc.evaluate('//div[@id="setThumbs"]/a[starts-with(@id, "set_thumb_link_")]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
return "multiple";
} else if (doc.evaluate('//p[@class="StreamList" or @class="UserTagList"]/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[@class="StreamView"]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
elmts = doc.evaluate('//div[@class="StreamView" and starts-with(@id, "sv_title_")]', doc, nsResolver, XPathResult.ANY_TYPE, null);
while (elmt = elmts.iterateNext()){
var title = Zotero.Utilities.trimInternal(elmt.textContent);
var photo_id = elmt.id;
photo_id = photo_id.substr(9);
items[photo_id] = title;
}
// photo set
} else if (doc.evaluate('//div[@id="setThumbs"]/a[starts-with(@id, "set_thumb_link_")]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()){
elmts = doc.evaluate('//div[@id="setThumbs"]/a[starts-with(@id, "set_thumb_link_")]', doc, nsResolver, XPathResult.ANY_TYPE, null);
while (elmt = elmts.iterateNext()){
var title = Zotero.Utilities.trimInternal(elmt.title);
var photo_id = elmt.id.substr(15);
items[photo_id] = title;
}
// tagged with
} else if (doc.evaluate('//p[@class="StreamList" or @class="UserTagList"]/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.cleanString(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.cleanString(xml..tag[i].text().toString()));
}
if (xml..dates.length()){
var date = xml..dates[0][email protected]();
newItem.date = date.substr(0, 10);
}
if (xml..owner.length()){
var author = xml..owner[0][email protected]();
if (author == ""){
author = xml..owner[0][email protected]();
}
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][email protected]();
var photo_id = xml..photo[0][email protected]();
// 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][email protected]();
newItem.attachments = [{title:title, url:attachmentUri}];
newItem.complete();
}, function(){Zotero.done();});
});
Zotero.wait();
}