Skip to content

Instantly share code, notes, and snippets.

@katzefudder
Created August 23, 2014 09:41
Show Gist options
  • Save katzefudder/61678e153b8e324c4d91 to your computer and use it in GitHub Desktop.
Save katzefudder/61678e153b8e324c4d91 to your computer and use it in GitHub Desktop.
Get a randomized extract of a flickr set, for gallery purposes
var flickr = {
apiKey: null,
photosetId: null,
photos: null,
ownerName: null,
limit: null,
init: function(apiKey, photosetId, limit) {
this.apiKey = apiKey;
this.photosetId = photosetId;
this.limit = parseInt(limit);
this.photos = this.getPhotos(this.decode());
this.write(this.photos);
return this;
},
decode: function() {
var url = 'https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key='+flickr.apiKey+'&photoset_id='+flickr.photosetId+'&format=json&extras=url_m';
var response = this.httpGet(url);
var execute = 'this.'+response;
return eval(execute);
},
jsonFlickrApi : function(code) {
return code;
},
getPhotos : function(object) {
this.ownerName = object.photoset.ownername;
var photos = [];
object.photoset.photo.forEach(function(element){
photos.push([element.url_m, element.title]);
});
return photos;
},
httpGet: function(theUrl) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
},
getRandomNumber: function() {
return Math.floor((Math.random() * (this.photos.length -1)) + 1);
},
write: function(photos) {
var html = '<div class="flickr_images">';
for (var i = 0; i < this.limit; i++) {
var random = this.getRandomNumber();
html += '<img src="'+photos[random][0]+'" alt="'+photos[random][1]+'" />';
}
var flickUrl = 'https://www.flickr.com/photos/'+this.ownerName.toLowerCase()+'/sets/'+this.photosetId;
html += '<div class="taken_from">Random '+this.limit+' photos taken from <a href="'+flickUrl+'" target="_blank">'+flickUrl+'</a></div>';
html += '</div>';
document.write(html);
}
}
flickr.init('50b2c88d16b7d6765f8c2f9b9bce844f', '72157644508338618', 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment