author | Michael Krelin <hacker@klever.net> | 2011-08-28 11:05:26 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2011-08-28 11:05:26 (UTC) |
commit | 4152a504609522fab4a269dc88d13a1078a09452 (patch) (side-by-side diff) | |
tree | a923baa662a97b865eddedb1394085d943289b0a /content/flickr.js | |
parent | 5d85cdc10a4affbebc8a06d655f820239ececf37 (diff) | |
download | fireflix-4152a504609522fab4a269dc88d13a1078a09452.zip fireflix-4152a504609522fab4a269dc88d13a1078a09452.tar.gz fireflix-4152a504609522fab4a269dc88d13a1078a09452.tar.bz2 |
Add json-responding api calls
It is somewhat more native to javascript. Or could be, at least.
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | content/flickr.js | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/content/flickr.js b/content/flickr.js index e09d5f0..545144c 100644 --- a/content/flickr.js +++ b/content/flickr.js @@ -1,248 +1,268 @@ /* * 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/', uploader_edit_url: 'http://www.flickr.com/tools/uploader_edit.gne', 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_json: function(params,on_success,on_failure) { + if(params.auth_token=='default') params.auth_token=this.token; + params.format = 'json'; params.nojsoncallback=1; + 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 rsp=JSON.parse(x.responseText); + if(rsp.stat=='ok') + on_success && on_success(x,rsp); + else + on_failure && on_failure(rsp,rsp.stat,rsp.code,rsp.message); + }else + on_failure && on_failure(x); + return true; + }; + x.send(null); + return true; + }, 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; }, |