Created
April 15, 2010 16:39
-
-
Save kaiuhl/367336 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /****************************************** | |
| Flickr Javascript Library, alpha 1 | |
| Copyright (c) 2008 Kyle Meyer (kylemeyer.com) | |
| Dual licensed under the MIT (MIT-LICENSE.txt) | |
| and GPL (GPL-LICENSE.txt) licenses. | |
| Last updated November, 2008 | |
| ******************************************/ | |
| // Your Flickr API key goes here. | |
| // Don't have one? Get one at http://www.flickr.com/services/api/keys/apply/ | |
| var api = "2ee1829d2354a3f09c6ef9316acb94ba"; | |
| var Flickr = { | |
| base_url: '', | |
| api_key: '', | |
| here: '', | |
| size: '', | |
| quantity: 0, | |
| descriptions: false, | |
| titles: false, | |
| init: function() { | |
| this.api_key = api; | |
| this.base_url = "http://api.flickr.com/services/rest/?method="; | |
| this.load(); | |
| }, | |
| load: function() { | |
| $.getJSON(this.request("flickr.test.echo"), | |
| function(data){ | |
| if (data.stat == "ok") { | |
| return true; | |
| } else { | |
| alert("Invalid Flickr API key\n\nDid you enter your key in Flickr.js?"); | |
| } | |
| } | |
| ); | |
| }, | |
| request: function(method, parameters) { | |
| var params = ''; | |
| if (parameters) { | |
| for (param in parameters) { | |
| switch(param) { | |
| case "username": | |
| params += "&" + "user_id=" + this.username(parameters[param]); | |
| break; | |
| case "size": | |
| switch(parameters[param]) { | |
| case "small": | |
| this.size = "t"; | |
| break; | |
| case "medium": | |
| this.size = ""; | |
| break; | |
| case "large": | |
| this.size = "b"; | |
| break; | |
| case "thumbnail": | |
| this.size = "s"; | |
| break; | |
| case "original": | |
| params += "&extras=original_format"; | |
| this.size = "o"; | |
| break; | |
| } | |
| break; | |
| case "quantity": | |
| this.quantity = parameters[param]; | |
| break; | |
| case "target": | |
| this.here = parameters[param]; | |
| break; | |
| case "descriptions": | |
| this.descriptions = parameters[param]; | |
| break; | |
| case "titles": | |
| this.titles = parameters[param]; | |
| break; | |
| default: | |
| params += "&" + param + "=" + parameters[param]; | |
| } | |
| } | |
| } | |
| return this.base_url + method + "&api_key=" + this.api_key + params + "&per_page=500&format=json&jsoncallback=?"; | |
| }, | |
| username: function(username) { | |
| var params = {user_id: username}; | |
| $.getJSON(this.request("flickr.people.findByUsername", params), | |
| function(data){ | |
| alert(data.user.id); | |
| } | |
| ); | |
| }, | |
| search: function(parameters) { | |
| $.getJSON(this.request("flickr.photos.search", parameters), | |
| function(data){ | |
| return data; | |
| } | |
| ); | |
| }, | |
| random: function(parameters, target) { | |
| this.here = target; | |
| $.getJSON(this.request("flickr.photos.search", parameters), | |
| function(data){ | |
| var used = []; | |
| for(var i=0; i < Flickr.quantity; i++) { | |
| do { | |
| var num = Math.floor(Math.random() * parseInt(data.photos.total)); | |
| for(var j=0; j<=i; j++) { | |
| if (used[j] == num) { | |
| break; | |
| } | |
| } | |
| } while (false); | |
| used[i] = num; | |
| Flickr.url(data.photos.photo[num]); | |
| } | |
| } | |
| ); | |
| }, | |
| url: function(photo) { | |
| this.photo_url = "http://farm"; | |
| this.photo_url += photo.farm; | |
| this.photo_url += ".static.flickr.com/"; | |
| this.photo_url += photo.server; | |
| this.photo_url += "/" + photo.id + "_"; | |
| this.photo_url += (this.size == "o" ? photo.originalsecret : photo.secret); | |
| this.photo_url += (this.size ? "_" + this.size : ""); | |
| this.photo_url += (this.size == "o" ? "." + photo.originalformat : ".jpg"); | |
| this.photo_title = photo.title; | |
| this.photo_description = photo.description | |
| this.link(photo); | |
| }, | |
| link: function(photo) { | |
| this.photo_page_url = "http://www.flickr.com/photos/" + photo.owner + "/" + photo.id; | |
| this.target != '' ? this.to(photo) : alert("Flickrror: You have no specified a target."); | |
| }, | |
| to: function(photo) { | |
| if (this.titles == true) { | |
| $("<p class='flickr-img-title'>" + this.photo_title + "</p>") | |
| .appendTo(this.here); | |
| }; | |
| $("<img src='" + this.photo_url + "' />") | |
| .appendTo(this.here) | |
| .wrap("<div class='flickr-img'></div>") | |
| .after() | |
| .wrap("<a href='" + this.photo_page_url + "' title='" + this.photo_title + "'></a>"); | |
| if (this.descriptions == true) | |
| this.description(photo); | |
| }, | |
| description: function(photo) { | |
| $.getJSON(this.request("flickr.photos.getInfo", { | |
| photo_id: photo.id, | |
| secret: photo.secret | |
| }), | |
| function(data){ | |
| $("<p class='flickr-img-description'></p>") | |
| .html(data.photo.description._content.replace(/\n/g, "<br />")) | |
| .appendTo(Flickr.here); | |
| }); | |
| }, | |
| latest: function(parameters) { | |
| $.getJSON(this.request("flickr.photos.search", parameters), | |
| function(data){ | |
| for(var i=0; i < Flickr.quantity; i++) { | |
| Flickr.url(data.photos.photo[i]); | |
| } | |
| }); | |
| } | |
| } | |
| Flickr.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment