FlickrRestStore.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.data.FlickrRestStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.data.FlickrRestStore"] = true;
  8. dojo.provide("dojox.data.FlickrRestStore");
  9. dojo.require("dojox.data.FlickrStore");
  10. dojo.declare("dojox.data.FlickrRestStore",
  11. dojox.data.FlickrStore, {
  12. constructor: function(/*Object*/args){
  13. // summary:
  14. // Initializer for the FlickrRestStore store.
  15. // description:
  16. // The FlickrRestStore is a Datastore interface to one of the basic services
  17. // of the Flickr service, the public photo feed. This does not provide
  18. // access to all the services of Flickr.
  19. // This store cannot do * and ? filtering as the flickr service
  20. // provides no interface for wildcards.
  21. if(args){
  22. if(args.label){
  23. this.label = args.label;
  24. }
  25. if(args.apikey){
  26. this._apikey = args.apikey;
  27. }
  28. }
  29. this._cache = [];
  30. this._prevRequests = {};
  31. this._handlers = {};
  32. this._prevRequestRanges = [];
  33. this._maxPhotosPerUser = {};
  34. this._id = dojox.data.FlickrRestStore.prototype._id++;
  35. },
  36. // _id: Integer
  37. // A unique identifier for this store.
  38. _id: 0,
  39. // _requestCount: Integer
  40. // A counter for the number of requests made. This is used to define
  41. // the callback function that Flickr will use.
  42. _requestCount: 0,
  43. // _flickrRestUrl: String
  44. // The URL to the Flickr REST services.
  45. _flickrRestUrl: "http://www.flickr.com/services/rest/",
  46. // _apikey: String
  47. // The users API key to be used when accessing Flickr REST services.
  48. _apikey: null,
  49. // _storeRef: String
  50. // A key used to mark an data store item as belonging to this store.
  51. _storeRef: "_S",
  52. // _cache: Array
  53. // An Array of all previously downloaded picture info.
  54. _cache: null,
  55. // _prevRequests: Object
  56. // A HashMap used to record the signature of a request to prevent duplicate
  57. // request being made.
  58. _prevRequests: null,
  59. // _handlers: Object
  60. // A HashMap used to record the handlers registered for a single remote request. Multiple
  61. // requests may be made for the same information before the first request has finished.
  62. // Each element of this Object is an array of handlers to call back when the request finishes.
  63. // This prevents multiple requests being made for the same information.
  64. _handlers: null,
  65. // _sortAttributes: Object
  66. // A quick lookup of valid attribute names in a sort query.
  67. _sortAttributes: {
  68. "date-posted": true,
  69. "date-taken": true,
  70. "interestingness": true
  71. },
  72. _fetchItems: function( /*Object*/ request,
  73. /*Function*/ fetchHandler,
  74. /*Function*/ errorHandler){
  75. // summary: Fetch flickr items that match to a query
  76. // request:
  77. // A request object
  78. // fetchHandler:
  79. // A function to call for fetched items
  80. // errorHandler:
  81. // A function to call on error
  82. var query = {};
  83. if(!request.query){
  84. request.query = query = {};
  85. } else {
  86. dojo.mixin(query, request.query);
  87. }
  88. var primaryKey = [];
  89. var secondaryKey = [];
  90. //Build up the content to send the request for.
  91. var content = {
  92. format: "json",
  93. method: "flickr.photos.search",
  94. api_key: this._apikey,
  95. extras: "owner_name,date_upload,date_taken"
  96. };
  97. var isRest = false;
  98. if(query.userid){
  99. isRest = true;
  100. content.user_id = request.query.userid;
  101. primaryKey.push("userid"+request.query.userid);
  102. }
  103. if(query.groupid){
  104. isRest = true;
  105. content.group_id = query.groupid;
  106. primaryKey.push("groupid" + query.groupid);
  107. }
  108. if(query.apikey){
  109. isRest = true;
  110. content.api_key = request.query.apikey;
  111. secondaryKey.push("api"+request.query.apikey);
  112. }else if(content.api_key){
  113. isRest = true;
  114. request.query.apikey = content.api_key;
  115. secondaryKey.push("api"+content.api_key);
  116. }else{
  117. throw Error("dojox.data.FlickrRestStore: An API key must be specified.");
  118. }
  119. request._curCount = request.count;
  120. if(query.page){
  121. content.page = request.query.page;
  122. secondaryKey.push("page" + content.page);
  123. }else if(("start" in request) && request.start !== null){
  124. if(!request.count){
  125. request.count = 20;
  126. }
  127. var diff = request.start % request.count;
  128. var start = request.start, count = request.count;
  129. // If the count does not divide cleanly into the start number,
  130. // more work has to be done to figure out the best page to request
  131. if(diff !== 0) {
  132. if(start < count / 2){
  133. // If the first record requested is less than half the
  134. // amount requested, then request from 0 to the count record
  135. count = start + count;
  136. start = 0;
  137. }else{
  138. var divLimit = 20, div = 2;
  139. for(var i = divLimit; i > 0; i--){
  140. if(start % i === 0 && (start/i) >= count){
  141. div = i;
  142. break;
  143. }
  144. }
  145. count = start/div;
  146. }
  147. request._realStart = request.start;
  148. request._realCount = request.count;
  149. request._curStart = start;
  150. request._curCount = count;
  151. }else{
  152. request._realStart = request._realCount = null;
  153. request._curStart = request.start;
  154. request._curCount = request.count;
  155. }
  156. content.page = (start / count) + 1;
  157. secondaryKey.push("page" + content.page);
  158. }
  159. if(request._curCount){
  160. content.per_page = request._curCount;
  161. secondaryKey.push("count" + request._curCount);
  162. }
  163. if(query.lang){
  164. content.lang = request.query.lang;
  165. primaryKey.push("lang" + request.lang);
  166. }
  167. if(query.setid){
  168. content.method = "flickr.photosets.getPhotos";
  169. content.photoset_id = request.query.setid;
  170. primaryKey.push("set" + request.query.setid);
  171. }
  172. if(query.tags){
  173. if(query.tags instanceof Array){
  174. content.tags = query.tags.join(",");
  175. }else{
  176. content.tags = query.tags;
  177. }
  178. primaryKey.push("tags" + content.tags);
  179. if(query["tag_mode"] && (query.tag_mode.toLowerCase() === "any" ||
  180. query.tag_mode.toLowerCase() === "all")){
  181. content.tag_mode = query.tag_mode;
  182. }
  183. }
  184. if(query.text){
  185. content.text=query.text;
  186. primaryKey.push("text:"+query.text);
  187. }
  188. //The store only supports a single sort attribute, even though the
  189. //Read API technically allows multiple sort attributes
  190. if(query.sort && query.sort.length > 0){
  191. //The default sort attribute is 'date-posted'
  192. if(!query.sort[0].attribute){
  193. query.sort[0].attribute = "date-posted";
  194. }
  195. //If the sort attribute is valid, check if it is ascending or
  196. //descending.
  197. if(this._sortAttributes[query.sort[0].attribute]) {
  198. if(query.sort[0].descending){
  199. content.sort = query.sort[0].attribute + "-desc";
  200. }else{
  201. content.sort = query.sort[0].attribute + "-asc";
  202. }
  203. }
  204. }else{
  205. //The default sort in the Dojo Data API is ascending.
  206. content.sort = "date-posted-asc";
  207. }
  208. primaryKey.push("sort:"+content.sort);
  209. //Generate a unique key for this request, so the store can
  210. //detect duplicate requests.
  211. primaryKey = primaryKey.join(".");
  212. secondaryKey = secondaryKey.length > 0 ? "." + secondaryKey.join(".") : "";
  213. var requestKey = primaryKey + secondaryKey;
  214. //Make a copy of the request, in case the source object is modified
  215. //before the request completes
  216. request = {
  217. query: query,
  218. count: request._curCount,
  219. start: request._curStart,
  220. _realCount: request._realCount,
  221. _realStart: request._realStart,
  222. onBegin: request.onBegin,
  223. onComplete: request.onComplete,
  224. onItem: request.onItem
  225. };
  226. var thisHandler = {
  227. request: request,
  228. fetchHandler: fetchHandler,
  229. errorHandler: errorHandler
  230. };
  231. //If the request has already been made, but not yet completed,
  232. //then add the callback handler to the list of handlers
  233. //for this request, and finish.
  234. if(this._handlers[requestKey]){
  235. this._handlers[requestKey].push(thisHandler);
  236. return;
  237. }
  238. this._handlers[requestKey] = [thisHandler];
  239. //Linking this up to Flickr is a PAIN!
  240. var handle = null;
  241. var getArgs = {
  242. url: this._flickrRestUrl,
  243. preventCache: this.urlPreventCache,
  244. content: content,
  245. callbackParamName: "jsoncallback"
  246. };
  247. var doHandle = dojo.hitch(this, function(processedData, data, handler){
  248. var onBegin = handler.request.onBegin;
  249. handler.request.onBegin = null;
  250. var maxPhotos;
  251. var req = handler.request;
  252. if(("_realStart" in req) && req._realStart != null){
  253. req.start = req._realStart;
  254. req.count = req._realCount;
  255. req._realStart = req._realCount = null;
  256. }
  257. //If the request contains an onBegin method, the total number
  258. //of photos must be calculated.
  259. if(onBegin){
  260. var photos = null;
  261. if(data){
  262. photos = (data.photoset ? data.photoset : data.photos);
  263. }
  264. if(photos && ("perpage" in photos) && ("pages" in photos)){
  265. if(photos.perpage * photos.pages <= handler.request.start + handler.request.count){
  266. //If the final page of results has been received, it is possible to
  267. //know exactly how many photos there are
  268. maxPhotos = handler.request.start + photos.photo.length;
  269. }else{
  270. //If the final page of results has not yet been received,
  271. //it is not possible to tell exactly how many photos exist, so
  272. //return the number of pages multiplied by the number of photos per page.
  273. maxPhotos = photos.perpage * photos.pages;
  274. }
  275. this._maxPhotosPerUser[primaryKey] = maxPhotos;
  276. onBegin(maxPhotos, handler.request);
  277. }else if(this._maxPhotosPerUser[primaryKey]){
  278. onBegin(this._maxPhotosPerUser[primaryKey], handler.request);
  279. }
  280. }
  281. //Call whatever functions the caller has defined on the request object, except for onBegin
  282. handler.fetchHandler(processedData, handler.request);
  283. if(onBegin){
  284. //Replace the onBegin function, if it existed.
  285. handler.request.onBegin = onBegin;
  286. }
  287. });
  288. //Define a callback for the script that iterates through a list of
  289. //handlers for this piece of data. Multiple requests can come into
  290. //the store for the same data.
  291. var myHandler = dojo.hitch(this, function(data){
  292. //The handler should not be called more than once, so disconnect it.
  293. //if(handle !== null){ dojo.disconnect(handle); }
  294. if(data.stat != "ok"){
  295. errorHandler(null, request);
  296. }else{ //Process the items...
  297. var handlers = this._handlers[requestKey];
  298. if(!handlers){
  299. console.log("FlickrRestStore: no handlers for data", data);
  300. return;
  301. }
  302. this._handlers[requestKey] = null;
  303. this._prevRequests[requestKey] = data;
  304. //Process the data once.
  305. var processedData = this._processFlickrData(data, request, primaryKey);
  306. if(!this._prevRequestRanges[primaryKey]){
  307. this._prevRequestRanges[primaryKey] = [];
  308. }
  309. this._prevRequestRanges[primaryKey].push({
  310. start: request.start,
  311. end: request.start + (data.photoset ? data.photoset.photo.length : data.photos.photo.length)
  312. });
  313. //Iterate through the array of handlers, calling each one.
  314. dojo.forEach(handlers, function(i){
  315. doHandle(processedData, data, i);
  316. });
  317. }
  318. });
  319. var data = this._prevRequests[requestKey];
  320. //If the data was previously retrieved, there is no need to fetch it again.
  321. if(data){
  322. this._handlers[requestKey] = null;
  323. doHandle(this._cache[primaryKey], data, thisHandler);
  324. return;
  325. }else if(this._checkPrevRanges(primaryKey, request.start, request.count)){
  326. //If this range of data has already been retrieved, reuse it.
  327. this._handlers[requestKey] = null;
  328. doHandle(this._cache[primaryKey], null, thisHandler);
  329. return;
  330. }
  331. var deferred = dojo.io.script.get(getArgs);
  332. deferred.addCallback(myHandler);
  333. //We only set up the errback, because the callback isn't ever really used because we have
  334. //to link to the jsonFlickrFeed function....
  335. deferred.addErrback(function(error){
  336. dojo.disconnect(handle);
  337. errorHandler(error, request);
  338. });
  339. },
  340. getAttributes: function(item){
  341. // summary:
  342. // See dojo.data.api.Read.getAttributes()
  343. return [
  344. "title", "author", "imageUrl", "imageUrlSmall", "imageUrlMedium",
  345. "imageUrlThumb", "imageUrlLarge", "imageUrlOriginal", "link", "dateTaken", "datePublished"
  346. ];
  347. },
  348. getValues: function(item, attribute){
  349. // summary:
  350. // See dojo.data.api.Read.getValue()
  351. this._assertIsItem(item);
  352. this._assertIsAttribute(attribute);
  353. switch(attribute){
  354. case "title":
  355. return [ this._unescapeHtml(item.title) ]; // String
  356. case "author":
  357. return [ item.ownername ]; // String
  358. case "imageUrlSmall":
  359. return [ item.media.s ]; // String
  360. case "imageUrl":
  361. return [ item.media.l ]; // String
  362. case "imageUrlOriginal":
  363. return [ item.media.o ]; // String
  364. case "imageUrlLarge":
  365. return [ item.media.l ]; // String
  366. case "imageUrlMedium":
  367. return [ item.media.m ]; // String
  368. case "imageUrlThumb":
  369. return [ item.media.t ]; // String
  370. case "link":
  371. return [ "http://www.flickr.com/photos/" + item.owner + "/" + item.id ]; // String
  372. case "dateTaken":
  373. return [ item.datetaken ];
  374. case "datePublished":
  375. return [ item.datepublished ];
  376. default:
  377. return undefined;
  378. }
  379. },
  380. _processFlickrData: function(/* Object */data, /* Object */request, /* String */ cacheKey){
  381. // summary: Processes the raw data from Flickr and updates the internal cache.
  382. // data:
  383. // Data returned from Flickr
  384. // request:
  385. // The original dojo.data.Request object passed in by the user.
  386. // If the data contains an 'item' object, it has not come from the REST
  387. // services, so process it using the FlickrStore.
  388. if(data.items){
  389. return dojox.data.FlickrStore.prototype._processFlickrData.apply(this,arguments);
  390. }
  391. var template = ["http://farm", null, ".static.flickr.com/", null, "/", null, "_", null];
  392. var items = [];
  393. var photos = (data.photoset ? data.photoset : data.photos);
  394. if(data.stat == "ok" && photos && photos.photo){
  395. items = photos.photo;
  396. //Add on the store ref so that isItem can work.
  397. for(var i = 0; i < items.length; i++){
  398. var item = items[i];
  399. item[this._storeRef] = this;
  400. template[1] = item.farm;
  401. template[3] = item.server;
  402. template[5] = item.id;
  403. template[7] = item.secret;
  404. var base = template.join("");
  405. item.media = {
  406. s: base + "_s.jpg",
  407. m: base + "_m.jpg",
  408. l: base + ".jpg",
  409. t: base + "_t.jpg",
  410. o: base + "_o.jpg"
  411. };
  412. if(!item.owner && data.photoset){
  413. item.owner = data.photoset.owner;
  414. }
  415. }
  416. }
  417. var start = request.start ? request.start : 0;
  418. var arr = this._cache[cacheKey];
  419. if(!arr){
  420. this._cache[cacheKey] = arr = [];
  421. }
  422. dojo.forEach(items, function(i, idx){
  423. arr[idx+ start] = i;
  424. });
  425. return arr; // Array
  426. },
  427. _checkPrevRanges: function(primaryKey, start, count){
  428. var end = start + count;
  429. var arr = this._prevRequestRanges[primaryKey];
  430. return (!!arr) && dojo.some(arr, function(item){
  431. return ((start >= item.start)&&(end <= item.end));
  432. });
  433. }
  434. });
  435. }