Skip to content

Instantly share code, notes, and snippets.

@lu1s
Created March 17, 2012 17:21
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lu1s/2063001 to your computer and use it in GitHub Desktop.
Save lu1s/2063001 to your computer and use it in GitHub Desktop.
Get random flickr picture from given user id and append to given div id (needs jquery and flickr api key)
function getPicture(the_user_id, your_div_id){
var apiKey = "YOUR-API-KEY"; // replace this with your API key
var url_to_a_photo_head = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key="+apiKey+"&photo_id=";
var url_to_a_photo_tail = "&format=json&jsoncallback=?";
// get an array of random photos
$.getJSON(
"http://api.flickr.com/services/rest/",
{
method: 'flickr.people.getPublicPhotos',
api_key: apiKey,
user_id: the_user_id,
format: 'json',
nojsoncallback: 1,
per_page: 10 // you can increase this to get a bigger array
}
function(data){
// if everything went good
if(data.stat == 'ok'){
// get a random id from the array
var photoId = data.photos.photo[ Math.floor( Math.random() * data.photos.photo.length ) ];
// now call the flickr API and get the picture with a nice size
$.getJSON(
"http://api.flickr.com/services/rest/",
{
method: 'flickr.photos.getSizes'.
api_key: apiKey,
photo_id: photoId,
format: 'json',
nojsoncallback: 1
}
function(response){
if(response.stat == 'ok'){
var the_url = response.sizes.size[5].source;
$("#"+your_div_id).append('<img src="' + the_url + '" />');
}
else{
console.log(" The request to get the picture was not good :\ ")
}
}
);
}
else{
console.log(" The request to get the array was not good :( ");
}
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment