Permalink
Browse files

Added short url generator.

  • Loading branch information...
1 parent 4ad65ff commit d1fd1e4c99b3a64a392d8a34d92a34b18f8b125e @botic committed Sep 25, 2010
Showing with 43 additions and 3 deletions.
  1. +43 −3 lib/parr.js
View
@@ -9,7 +9,7 @@ function FlickrClient(apiKey, apiEndpoint, cacheSize, cacheLease) {
var apiKey = apiKey;
var apiEndpoint = apiEndpoint || "http://api.flickr.com/services/rest/";
- var cache = new SimpleCache(cacheSize || 100, cacheLease || 5000);
+ var cache = new SimpleCache(cacheSize || 100, cacheLease || 10000);
var callApi = function(method, params, useCache) {
var values = {};
@@ -33,13 +33,36 @@ function FlickrClient(apiKey, apiEndpoint, cacheSize, cacheLease) {
result = JSON.parse(data);
cache.put(toCall, result);
});
- log.info("cache miss");
+ log.debug("cache miss");
} else {
- log.info("cache hit");
+ log.debug("cache hit");
}
return result;
};
+ /**
+ * Converts the given ID into a base58 encoded string.
+ * @author Xenocryst - Antares Scorpii
+ * @see http://www.flickr.com/groups/api/discuss/72157616713786392/
+ */
+ var base58 = function (num) {
+ if (typeof num !== 'number') {
+ num = parseInt(num, 10);
+ }
+ var enc = '', alpha='123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
+ var div = num, mod;
+ while (num >= 58) {
+ div = num / 58;
+ mod = num - (58 * Math.floor(div));
+ enc = '' + alpha.substr(mod,1) + enc;
+ num = Math.floor(div);
+ }
+ return (div) ? '' + alpha.substr(div, 1) + enc : enc;
+ }
+
+ /**
+ * Calls the Flickr-API
+ */
this.call = function (apiMethod, params, useCache) {
if (!apiKey) {
throw "Flickr API key missing!";
@@ -51,4 +74,21 @@ function FlickrClient(apiKey, apiEndpoint, cacheSize, cacheLease) {
return callApi(apiMethod, params, useCache || true);
};
+
+ /**
+ * Return the absolute URL to the Flickr farm.
+ */
+ this.photoSourceURL = function(photo, format) {
+ var fmString = format ? "_" + format : "";
+ return ["http://farm", photo["farm"], ".static.flickr.com/",
+ photo["server"], "/", photo["id"], "_", photo["secret"],
+ fmString, ".jpg"].join("");
+ }
+
+ /**
+ * Returns the short url to the given photo
+ */
+ this.photoShortUrl = function(photo) {
+ return "http://flic.kr/p/" + base58(photo["id"]);
+ }
};

0 comments on commit d1fd1e4

Please sign in to comment.