Skip to content

Instantly share code, notes, and snippets.

@zmack
Created October 2, 2008 17:29
Show Gist options
  • Save zmack/14401 to your computer and use it in GitHub Desktop.
Save zmack/14401 to your computer and use it in GitHub Desktop.
ze joos for ze flickr api
var Flickr = Class.create({
initialize: function(api_key) {
//this.api_key = api_key;
this.api_key = '4b14a0d06d79ae1cf8fafec76f32be4d';
this.image_url_template = new Template("http://farm#{farm}.static.flickr.com/#{server}/#{id}_#{secret}_#{size}.jpg")
},
// flickr callback
f_setUserId: function(flickr_response) {
if ( flickr_response.stat != 'ok' ) {
console.log('Nobody was home');
return;
}
this.user_id = flickr_response.user.id;
this.username = flickr_response.user.username._content;
},
f_getSets: function(flickr_response) {
console.log(flickr_response.photosets.photoset.length + ' sets');
flickr_response.photosets.photoset.each( function(photoset) {
this.getPhotosFromSet(photoset.id);
}.bind(this));
},
f_getPhotosFromSet: function(flickr_response) {
flickr_response.photoset.photo.each( function(photo) {
$('images').appendChild( new Element('img', { src: this.urlForImage(photo, 's') }) );
}.bind(this));
console.log(flickr_response);
},
setUsername: function(username) {
this.makeRequest('flickr.people.findByUsername', { username: username }, 'f_setUserId' );
},
getSets: function() {
this.makeRequest('flickr.photosets.getList', { user_id: this.user_id }, 'f_getSets' );
},
getPhotosFromSet: function(set) {
this.makeRequest('flickr.photosets.getPhotos', { photoset_id: set }, 'f_getPhotosFromSet' );
},
sortRequest: function(str) {
console.log(str);
},
makeRequest: function(func, params, callback) {
var url = this.getUrl(func) + '&' + $H(params).toQueryString();
var script = new Element('script', { language: 'Javascript', src: url });
window.FlickryCallback = this[callback].bind(this);
$$('body').first().appendChild(script);
},
getUrl: function(functionName, callback) {
if ( callback == null ) callback = 'FlickryCallback'
return "http://api.flickr.com/services/rest/?format=json&method=" + functionName + "&api_key=" + this.api_key + "&jsoncallback=" + callback
},
clearRequestJunk: function() {
$$('script[src^=http://api.flickr.com]').each( function(e) { Element.remove(e) });
},
// size ---
// s small square 75x75
// t thumbnail, 100 on longest side
// m small, 240 on longest side
// - medium, 500 on longest side
// b large, 1024 on longest side (only exists for very large original images)
// o original image, either a jpg, gif or png, depending on source format
urlForImage: function(image, size) {
return this.image_url_template.evaluate(Object.extend(image, { size: size }));
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment