public
Last active

Flickr API experiments

  • Download Gist
1flickrApi.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
"use strict";
 
// Uses jQuery and Q.
 
var API_KEY = "whatever";
 
function getFlickrResponseAsync(method, responseProperty, params) {
var deferred = Q.defer();
 
$.ajax("http://www.flickr.com/services/rest/", {
data: $.extend({
method: method,
format: "json",
api_key: API_KEY
}, params),
dataType: "jsonp",
jsonp: "jsoncallback"
})
.done(function (result) {
if (result.stat === "ok") {
deferred.resolve(result[responseProperty]);
} else {
deferred.reject(new Error("Flickr API failure: " + result.message + "(" + result.code + ")."));
}
})
.fail(function (xhr, textStatus, errorThrown) {
var error = new Error("Ajax request to the Flickr API failed.");
error.textStatus = textStatus;
error.errorThrown = errorThrown;
deferred.reject(error);
});
 
return deferred.promise;
}
 
function getPhotosetsAsync(userId) {
return getFlickrResponseAsync("flickr.photosets.getList", "photosets", { user_id: userId }).get("photoset");
}
 
function getPhotosetPhotosAsync(photosetId) {
var params = { photoset_id: photosetId, extras: "description" };
return getFlickrResponseAsync("flickr.photosets.getPhotos", "photoset", params).get("photo");
}
 
function makePhotoUri(dto, size, idProperty) {
idProperty = idProperty || "id";
return "http://farm" + dto.farm + ".staticflickr.com/" + dto.server + "/" +
dto[idProperty] + "_" + dto.secret + (size ? "_" + size : "") + ".jpg";
}
2example.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
"use strict";
 
var USER_ID = "whatever";
 
function photosetViewModel(photosetDto) {
return {
id: photosetDto.id,
title: photosetDto.title._content,
description: photosetDto.description._content,
primaryImageUri: makePhotoUri(photosetDto, "m", "primary")
};
}
 
function photoViewModel(photoDto, index) {
return {
index: index,
title: photoDto.title,
description: photoDto.description._content,
thumbnailUri: makePhotoUri(photoDto, "s"),
uri: makePhotoUri(photoDto)
};
}
 
getPhotosetsAsync(USER_ID).then(function (photosetDtos) {
var photosetViewModels = photosetDtos.map(photosetViewModel);
// do stuff with the photoset view models.
 
return Q.all(photosetDtos.map(function (photosetDto) {
return getPhotosetPhotosAsync(photosetDto.id).then(function (photosDto) {
var photoViewModels = photosDto.map(photoViewModel);
// do stuff with the photo view models.
});
})).then(function () {
// good to go; load the page.
});
}).done();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.