1 /** 2 * a library to get direct image urls for various image hosting servces 3 * @constructor 4 */ 5 function SpazImageURL(args) { 6 7 this.apis = {}; 8 9 this.initAPIs(); 10 11 }; 12 13 /** 14 * Creates the initial default set of API descriptions 15 */ 16 SpazImageURL.prototype.initAPIs = function() { 17 this.addAPI('drippic', { 18 'url_regex' : new RegExp("http://drippic.com/([a-zA-Z0-9]+)", "gi"), 19 'getThumbnailUrl' : function(id) { 20 var url = 'http://drippic.com/drippic/show/thumb/'+id; 21 return url; 22 }, 23 'getImageUrl' : function(id) { 24 var url = 'http://drippic.com/drippic/show/full/'+id; 25 return url; 26 } 27 }); 28 29 this.addAPI('twitpic', { 30 'url_regex' : new RegExp("http://twitpic.com/([a-zA-Z0-9]+)", "gi"), 31 'getThumbnailUrl' : function(id) { 32 var url = 'http://twitpic.com/show/thumb/'+id; 33 return url; 34 }, 35 'getImageUrl' : function(id) { 36 var url = 'http://twitpic.com/show/large/'+id; 37 return url; 38 } 39 }); 40 41 42 this.addAPI('yfrog', { 43 'url_regex' : new RegExp("http://yfrog.(?:com|us)/([a-zA-Z0-9]+)", "gim"), 44 'getThumbnailUrl' : function(id) { 45 var url = 'http://yfrog.com/'+id+'.th.jpg'; 46 return url; 47 }, 48 'getImageUrl' : function(id) { 49 var url = 'http://yfrog.com/'+id+':iphone'; 50 return url; 51 } 52 }); 53 54 55 this.addAPI('twitgoo', { 56 'url_regex' : /http:\/\/twitgoo.com\/([a-zA-Z0-9]+)/gi, 57 'getThumbnailUrl' : function(id) { 58 var url = 'http://twitgoo.com/show/thumb/'+id; 59 return url; 60 }, 61 'getImageUrl' : function(id) { 62 var url = 'http://twitgoo.com/show/img/'+id; 63 return url; 64 } 65 }); 66 67 68 69 this.addAPI('pikchur', { 70 'url_regex' : /http:\/\/(?:pikchur\.com|pk\.gd)\/([a-zA-Z0-9]+)/gi, 71 'getThumbnailUrl' : function(id) { 72 // http://img.pikchur.com/pic_GPT_t.jpg 73 var url = 'http://img.pikchur.com/pic_'+id+'_s.jpg'; 74 return url; 75 }, 76 'getImageUrl' : function(id) { 77 //http://img.pikchur.com/pic_GPT_l.jpg 78 var url = 'http://img.pikchur.com/pic_'+id+'_l.jpg'; 79 return url; 80 } 81 }); 82 83 84 this.addAPI('tweetphoto', { 85 'url_regex' : /http:\/\/tweetphoto.com\/([a-zA-Z0-9]+)/gi, 86 'getThumbnailUrl' : function(id) { 87 // http://TweetPhotoAPI.com/api/TPAPI.svc/json/imagefromurl?size=thumbnail&url=http://tweetphoto.com/iyb9azy4 88 var url = 'http://TweetPhotoAPI.com/api/TPAPI.svc/imagefromurl?size=thumbnail&url=http://tweetphoto.com/'+id; 89 return url; 90 }, 91 'getImageUrl' : function(id) { 92 // http://TweetPhotoAPI.com/api/TPAPI.svc/imagefromurl?size=big&url=http://tweetphoto.com/iyb9azy4 93 var url = 'http://TweetPhotoAPI.com/api/TPAPI.svc/imagefromurl?size=big&url=http://tweetphoto.com/'+id; 94 return url; 95 } 96 }); 97 98 99 this.addAPI('pic.gd', { 100 'url_regex' : /http:\/\/pic.gd\/([a-zA-Z0-9]+)/gi, 101 'getThumbnailUrl' : function(id) { 102 // http://TweetPhotoAPI.com/api/TPAPI.svc/json/imagefromurl?size=thumbnail&url=http://pic.gd/iyb9azy4 103 var url = 'http://TweetPhotoAPI.com/api/TPAPI.svc/imagefromurl?size=thumbnail&url=http://pic.gd/'+id; 104 return url; 105 }, 106 'getImageUrl' : function(id) { 107 // http://TweetPhotoAPI.com/api/TPAPI.svc/imagefromurl?size=big&url=http://pic.gd/iyb9azy4 108 var url = 'http://TweetPhotoAPI.com/api/TPAPI.svc/imagefromurl?size=big&url=http://pic.gd/'+id; 109 return url; 110 } 111 }); 112 }; 113 114 115 /** 116 * retrieve APIs 117 * @return {array} 118 */ 119 SpazImageURL.prototype.getAPIs = function() { 120 return this.apis; 121 }; 122 123 /** 124 * get an api for a service 125 * @param {string} service_name 126 * @return {object} 127 */ 128 SpazImageURL.prototype.getAPI = function(service_name) { 129 130 return this.apis[service_name]; 131 132 }; 133 134 /** 135 * add a new API for a service 136 * @param {string} service_name 137 * @param {object} opts (url_regex regexp, getThumbnailUrl method, getImageUrl method) 138 */ 139 SpazImageURL.prototype.addAPI = function(service_name, opts) { 140 141 var newapi = {}; 142 newapi.url_regex = opts.url_regex; // a regex used to look for this service's urls, must provide a parens match for image ID code 143 newapi.getThumbnailUrl = opts.getThumbnailUrl; // a function 144 newapi.getImageUrl = opts.getImageUrl; // a function 145 146 this.apis[service_name] = newapi; 147 148 }; 149 150 /** 151 * find the image service URLs that work with our defined APIs in a given string 152 * @param {string} str 153 * @return {object|null} an object of services (keys) and an array of their matches (vals) 154 */ 155 SpazImageURL.prototype.findServiceUrlsInString = function(str) { 156 157 var matches = {}, num_matches = 0, re_matches, key, thisapi; 158 159 for (key in this.apis) { 160 161 thisapi = this.getAPI(key); 162 sch.dump(key); 163 sch.dump(thisapi.url_regex); 164 while( (re_matches = thisapi.url_regex.exec(sch.trim(str))) != null) { 165 sch.dump(re_matches); 166 matches[key] = re_matches; 167 num_matches++; 168 } 169 } 170 sch.dump('num_matches:'+num_matches); 171 sch.dump(matches); 172 if (num_matches > 0) { 173 return matches; 174 } else { 175 return null; 176 } 177 178 }; 179 180 /** 181 * find the image service URLs that work with our defined APIs in a given string 182 * @param {object} matches 183 * @return {object|null} fullurl:thumburl key:val pairs 184 * 185 */ 186 SpazImageURL.prototype.getThumbsForMatches = function(matches) { 187 var x, service, api, thumburl, thumburls = {}, num_urls = 0; 188 189 for (service in matches) { 190 sch.dump('SERVICE:'+service); 191 api = this.getAPI(service); 192 urls = matches[service]; // an array 193 sch.dump("URLS:"+urls); 194 thumburls[urls[0]] = api.getThumbnailUrl(urls[1]); 195 num_urls++; 196 } 197 198 sch.dump('num_urls:'+num_urls); 199 sch.dump(thumburls); 200 201 if (num_urls > 0) { 202 return thumburls; 203 } else { 204 return null; 205 } 206 }; 207 208 209 /** 210 * given a string, this returns a set of key:val pairs of main url:thumbnail url 211 * for image hosting services for urls within the string 212 * @param {string} str 213 * @return {object|null} fullurl:thumburl key:val pairs 214 */ 215 SpazImageURL.prototype.getThumbsForUrls = function(str) { 216 var matches = this.findServiceUrlsInString(str); 217 if (matches) { 218 return this.getThumbsForMatches(matches); 219 } else { 220 return null; 221 } 222 223 }; 224 225 /** 226 * given a single image hosting service URL, this returns a URL to the thumbnail image itself 227 * @param {string} url 228 * @return {string|null} 229 */ 230 SpazImageURL.prototype.getThumbForUrl = function(url) { 231 var urls = this.getThumbsForUrls(url); 232 if (urls) { 233 return urls[url]; 234 } else { 235 return null; 236 } 237 }; 238 239 240 241 /** 242 * find the image service URLs that work with our defined APIs in a given string 243 * @param {object} matches 244 * @return {object|null} fullurl:thumburl key:val pairs 245 */ 246 SpazImageURL.prototype.getImagesForMatches = function(matches) { 247 var x, service, api, imageurl, imageurls = {}, num_urls = 0; 248 249 for (service in matches) { 250 sch.dump('SERVICE:'+service); 251 api = this.getAPI(service); 252 urls = matches[service]; // an array 253 sch.dump("URLS:"+urls); 254 imageurls[urls[0]] = api.getImageUrl(urls[1]); 255 num_urls++; 256 } 257 258 sch.dump('num_urls:'+num_urls); 259 sch.dump(imageurls); 260 261 if (num_urls > 0) { 262 return imageurls; 263 } else { 264 return null; 265 } 266 }; 267 268 269 /** 270 * given a string, this returns a set of key:val pairs of main url:image url 271 * for image hosting services for urls within the string 272 * @param {string} str 273 * @return {object|null} fullurl:imageurl key:val pairs 274 */ 275 SpazImageURL.prototype.getImagesForUrls = function(str) { 276 var matches = this.findServiceUrlsInString(str); 277 if (matches) { 278 return this.getImagesForMatches(matches); 279 } else { 280 return null; 281 } 282 }; 283 284 285 /** 286 * given a single image hosting service URL, this returns a URL to the image itself 287 * @param {string} url 288 * @return {string|null} 289 */ 290 SpazImageURL.prototype.getImageForUrl = function(url) { 291 var urls = this.getImagesForUrls(url); 292 if (urls) { 293 return urls[url]; 294 } else { 295 return null; 296 } 297 }; 298