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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/*
* Photoset
*/
function Photoset(s) {
if(s instanceof Photoset) {
for(var p in s) this[p]=s[p];
}else
this.fromNode(s);
}
Photoset.prototype = {
id: null,
primary: null,
secret: null,
server: null,
photos: null,
title: null,
description: null,
fromNode: function(n) {
this.id = n.getAttribute('id');
this.primary = n.getAttribute('primary');
this.secret = n.getAttribute('secret');
this.server = n.getAttribute('server');
this.photos = n.getAttribute('photos');
this.title = n.getElementsByTagName('title').item(0).firstChild.nodeValue;
this.description = n.getElementsByTagName('description').item(0).firstChild;
if(this.description) this.description = this.description.nodeValue;
}
};
/*
* Photo
*/
function Photo(s) {
if(s instanceof Photo) {
for(var p in s) this[p]=s[p];
}else
this.fromNode(s);
}
Photo.prototype = {
id: null, secret: null,
server: null,
title: null,
isprimary: null,
license: null,
dateupload: null, datetaken: null, datetakengranularity: null,
ownername: null,
iconserver: null,
originalformat: null,
lastupdate: null,
fromNode: function(n) {
this.id = n.getAttribute('id'); this.secret = n.getAttribute('secret');
this.server = n.getAttribute('server');
this.title = n.getAttribute('title');
this.isprimary = n.getAttribute('isprimary');
this.license = n.getAttribute('license');
this.dateupload = n.getAttribute('dateupload');
this.datetaken = n.getAttribute('datetaken'); this.datetakengranularity = n.getAttribute('datetakengranularity');
this.ownername = n.getAttribute('ownername');
this.iconserver = n.getAttribute('iconserver');
this.originalformat = n.getAttribute('originalformat');
this.lastupdate = n.getAttribute('lastupdate');
},
fromNode_: function(n) {
var t;
// TODO: @rotation @isfavorite
this.owner = {};
t = n.getElementsByTagName('owner').item(0);
if(t) {
this.owner.nsid=t.getAttribute('nsid');
this.owner.username=t.getAttribute('username');
this.owner.realname=t.getAttribute('realname');
this.owner.location=t.getAttribute.location;
}
t = n.getElementsByTagName('description').item(0);
if(t && t.firstChild) {
this.description = t.firstChild.nodeValue;
}
// TODO: visibility/@ispublic visibility/@isfriend visibility/@isfamily
// TODO: dates/@posted dates/@taken dates/@takengranularity dates/@lastupdate
// TODO: permissions/@permcomment permsiions/@permaddmeta
// TODO: editability/@canaddcomment editability/@canaddmeta
// TODO: comments
// TODO: notes/note/@id notes/note/@author notes/note/@authorname
// TODO: notes/note/@x notes/note/@y notes/note/@w notes/note/@h
// TODO: notes/note
// TODO: tags/tag/@id tags/tag/@author tags/tag/@raw tags/tag
// TODO: urls/url/@type urls/url
}
};
function Flickr() { }
Flickr.prototype = {
rest_url: 'http://www.flickr.com/services/rest/',
auth_url: 'http://flickr.com/services/auth/',
photo_url: 'http://static.flickr.com/',
photos_url: 'http://www.flickr.com/photos/',
upload_url: 'http://www.flickr.com/services/upload/',
api_sig: function(paramstr) {
return MD5(toutf8(this.api_shs+paramstr));
},
api_call_url: function(params,url) {
params.api_key = this.api_key;
var pp = new Array();
for(var p in params) {
pp.push(p);
}
var pstr = '';
var rv = (url?url:this.rest_url)+'?';
for(var p in pp.sort()) {
var pn = pp[p];
pstr += pn+params[pn];
rv += pn+'='+params[pn]+'&';
}
rv += 'api_sig='+this.api_sig(pstr);
return rv;
},
api_call: function(params, on_success, on_failure) {
if(params.auth_token == 'default')
params.auth_token = this.token;
var x = new XMLHttpRequest();
x.open("GET",this.api_call_url(params));
x.onreadystatechange=function() {
if(x.readyState!=4) return false;
if(x.status==200) {
var stat = x.responseXML.firstChild.getAttribute('stat');
if(stat=='ok') {
if(on_success) on_success(x);
}else{
var e = x.responseXML.getElementsByTagName('err').item(0);
var ecode = e.getAttribute('code');
var emsg = e.getAttribute('msg');
dump(params.method+' failed: '+ecode+' '+emsg+'\n');
if(on_failure) on_failure(x,stat,ecode,emsg);
}
}else{
if(on_failure) on_failure(x);
}
return true;
}
x.send(null);
return true;
},
frob: null,
authorize_0: function(perms, on_s, on_f) {
var _this = this;
this.api_call(
{ method: 'flickr.auth.getFrob' },
function(x) {
_this.frob = xp_str('/rsp/frob',x.responseXML);
var u = _this.api_call_url(
{ frob: _this.frob, perms: perms?perms:'delete' }, _this.auth_url );
if(on_s) on_s(x,_this.frob,u);
}, function(x,s,c,m) {
if(on_f) on_f(x,s,c,m);
}
);
},
token: null,
perms: null,
user: null,
authorize_1: function(on_s, on_f) {
var _this = this;
this.api_call(
{ method: 'flickr.auth.getToken', frob: this.frob },
function(x) {
_this.token = xp_str('/rsp/auth/token',x.responseXML);
_this.perms = xp_str('/rsp/auth/perms',x.responseXML);
var u = xp_node('/rsp/auth/user',x.responseXML);
_this.user = {
nsid: u.getAttribute('nsid'),
username: u.getAttribute('username'),
fullname: u.getAttribute('fullname')
};
if(on_s) on_s(x);
}, function(x,s,c,m) {
if(on_f) on_f(x,s,c,m);
}
);
},
prefs: Components.classes['@mozilla.org/preferences-service;1'].getService(
Components.interfaces.nsIPrefBranch
),
prefs_root: 'net.klever.kin.flickr',
save_token: function() {
// TODO: don't clear when there's nothing to clear or catch exceptions
if(this.token)
this.prefs.setCharPref(this.prefs_root+'.auth_token',this.token);
else
this.prefs.clearUserPref(this.prefs_root+'.auth_token');
if(this.perms)
this.prefs.setCharPref(this.prefs_root+'.auth_perms',this.perms);
else
this.prefs.clearUserPref(this.prefs_root+'.auth_perms');
if(this.user && this.user.nsid!=null && this.user.nsid!=undefined)
this.prefs.setCharPref(this.prefs_root+'.auth_user.nsid',this.user.nsid);
else
this.prefs.clearUserPref(this.prefs_root+'.auth_user.nsid');
if(this.user && this.user.username!=null && this.user.username!=undefined)
this.prefs.setCharPref(this.prefs_root+'.auth_user.username',this.user.username);
else
this.prefs.clearUserPref(this.prefs_root+'.auth_user.username');
if(this.user && this.user.fullname!=null && this.user.fullname!=undefined)
this.prefs.setCharPref(this.prefs_root+'.auth_user.fullname',this.user.fullname);
else
this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname');
},
_reset_token: function() {
this.token = null; this.perms = null; this.user = null;
return false;
},
load_token: function() {
try {
if(this.prefs.getPrefType(this.prefs_root+'.auth_token')!=this.prefs.PREF_STRING)
return this._reset_token();
this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token');
if(this.prefs.getPrefType(this.prefs_root+'.auth_perms')!=this.prefs.PREF_STRING)
return this._reset_token();
this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms');
if(this.prefs.getPrefType(this.prefs_root+'.auth_user.nsid')!=this.prefs.PREF_STRING)
return this._reset_token();
this.user = new Object();
this.user.nsid = this.prefs.getCharPref(this.prefs_root+'.auth_user.nsid');
if(this.prefs.getPrefType(this.prefs_root+'.auth_user.username')!=this.prefs.PREF_STRING)
return this._reset_token();
this.user.username = this.prefs.getCharPref(this.prefs_root+'.auth_user.username');
if(this.prefs.getPrefType(this.prefs_root+'.auth_user.fullname')!=this.prefs.PREF_STRING)
return this._reset_token();
this.user.fullname = this.prefs.getCharPref(this.prefs_root+'.auth_user.fullname');
}catch(e) { return this._reset_token(); }
return true;
},
reset_token: function() {
this._reset_token();
this.save_token();
},
get_photo_url: function(ser,id,sec,sfx,ext) {
var rv = this.photo_url + ser + '/' + id + '_' + sec;
if(sfx && sfx!='_') rv += '_'+sfx;
rv += ext?'.'+ext:'.jpg';
return rv;
},
get_image_url: function(o,sfx) {
return this.get_photo_url(
o.server,
(o instanceof Photoset)? o.primary : o.id,
o.secret,
sfx,
(sfx=='o')?o.originalformat:null
);
},
get_photo_page_url: function(p) {
if(p instanceof Photo) // TODO: half wrong, what if no owner?
return this.photos_url + (p.owner.nsid?p.owner.nsid:this.user.nsid) + '/' + p.id;
else // TODO: take owner into account?
return this.photos_url + this.user.nsid + '/' + p;
},
make_photo_url: function(p,sfx) {
if(sfx=='p')
return this.get_photo_page_url(p);
else
return this.get_image_url(p,sfx);
},
upload_file: function(f,fa,on_success,on_failure) {
try {
var fi = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
fi.initWithPath( f );
var st = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
st.init(fi,0x01,00004,null);
var bis = Components.classes["@mozilla.org/binaryinputstream;1"]
.createInstance(Components.interfaces.nsIBinaryInputStream);
bis.setInputStream(st);
// allocate and initialize temp storage string
var pbs = Components.classes["@mozilla.org/storagestream;1"]
.createInstance(Components.interfaces.nsIStorageStream);
pbs.init(1024,10000000,null);
// create output stream
var pbos = pbs.getOutputStream(0);
// and a binaryoutputstream interface
var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"]
.createInstance(Components.interfaces.nsIBinaryOutputStream);
pbbos.setOutputStream(pbos);
/* create POST body */
var boundarytoken = 'kadaroloongazaduviaxamma';
var boundary = '--'+boundarytoken;
var b = '';
var parms = { api_key: this.api_key, auth_token: this.token };
for(var p in fa) parms[p] = fa[p];
var pns = new Array();
for(var p in parms) pns.push(p);
var pstr = '';
for(var p in pns.sort()) {
var pn = pns[p];
pstr += pn+parms[pn];
b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n';
}
b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n';
b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n';
pbbos.writeBytes(b,b.length);
var bisbytes = bis.available();
pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes);
pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close();
pbbos.close(); pbos.close();
var x = new XMLHttpRequest();
x.open("POST",this.upload_url);
x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"');
x.setRequestHeader('Connection','close');
x.setRequestHeader('Content-Length',b.length);
x.onreadystatechange=function() {
if(x.readyState!=4) return false;
if(x.status==200) {
var stat = x.responseXML.firstChild.getAttribute('stat');
if(stat=='ok') {
var pid = xp_str('/rsp/photoid',x.responseXML);
if(on_success) on_success(x,pid);
}else{
var e = x.responseXML.getElementsByTagName('err').item(0);
var ecode = e.getAttribute('code');
var emsg = e.getAttribute('msg');
dump('upload failed: '+ecode+' '+emsg+'\n');
if(on_failure) on_failure(x,stat,ecode,emsg);
}
}else{
if(on_failure) on_failure(x);
}
return true;
};
x.send(pbs.newInputStream(0));
}catch(e) {
if(on_failure) on_failure(e,null,-1,e.message);
}
}
};
|