diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ef4fcce --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +node_modules +npm-debug.log diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..97a82b6 --- /dev/null +++ b/.npmignore @@ -0,0 +1,5 @@ +.env +test +test.js +example +examples diff --git a/README.md b/README.md new file mode 100644 index 0000000..7338bab --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +## flickr-photo-urls + +Get source urls with varied sizes of a Flickr photo + +## Install + +```bash +$ npm install flickr-photo-urls +``` + +## Usage + +```js +var urls = require('flickr-photo-urls')({ + key: 'api-key' +}) + +urls('14321741011', function (error, result) { + if (error) return callback(error); + + result.square + // => { "label": "Square", "width": 75, "height": 75, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_aeb3b41d62_s.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/sq\/", "photo": true } + + result.large2048 + // => { "label": "Large 2048", "width": 2048, "height": 1365, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_3c581b37ec_k.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/k\/", "photo": true } +}) +``` + +See `test.js` for more info. + +[flickr-client](http://github.com/npm-flickr/flickr-client) can be passed to avoid repeating auth options: + +```js +var client = require('flickr-client')({ + key: 'api-key' +}); + +var urls = require('flickr-photo-urls')(client) +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..82a7353 --- /dev/null +++ b/index.js @@ -0,0 +1,34 @@ +var createClient = require("flickr-client"); +var variableName = require("variable-name"); + +var client; + +module.exports = setup; + +function setup (options) { + client = createClient(options); + return photoURLs; +} + +function photoURLs (photoId, callback) { + client('photos.getSizes', { photo_id: photoId }, function (error, response) { + if (error) return callback(error); + + var result = {}; + + var i = response.sizes.size.length; + while (i--) { + if (response.sizes.size[i].media == 'video') response.sizes.size[i].video = true; + if (response.sizes.size[i].media == 'photo') response.sizes.size[i].photo = true; + delete response.sizes.size[i].media == 'photo'; + + response.sizes.size[i].width = Number(response.sizes.size[i].width); + response.sizes.size[i].height = Number(response.sizes.size[i].height); + + result[variableName(response.sizes.size[i].label)] = response.sizes.size[i]; + } + + callback(undefined, result); + + }); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..cb65ebd --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "flickr-photo-urls", + "version": "0.0.0", + "description": "Get source urls with varied sizes of a Flickr photo", + "main": "index.js", + "scripts": { + "test": "node test" + }, + "keywords": [ + "flickr", + "photos", + "photography", + "api", + "client" + ], + "repository": { + "url": "git@github.com:azer/flickr-photo-urls.git", + "type": "git" + }, + "author": "azer", + "license": "BSD", + "dependencies": { + "variable-name": "0.0.1", + "with-env": "^0.1.0", + "flickr-client": "0.0.2" + }, + "devDependencies": { + "prova": "^1.11.1" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..23db2ef --- /dev/null +++ b/test.js @@ -0,0 +1,28 @@ +require('with-env')(); + +var test = require("prova"); +var urls = require("./")({ + key: process.env.FLICKR_API_KEY +}); + +test('urls', function (t) { + + urls('14321741011', function (error, result) { + t.plan(13); + t.error(error); + t.deepEqual(result.square, { "label": "Square", "width": 75, "height": 75, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_aeb3b41d62_s.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/sq\/", "photo": true }); + t.deepEqual(result.largeSquare, { "label": "Large Square", "width": 150, "height": 150, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_aeb3b41d62_q.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/q\/", "photo": true }); + t.deepEqual(result.thumbnail, { "label": "Thumbnail", "width": 100, "height": 67, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_aeb3b41d62_t.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/t\/", "photo": true }); + t.deepEqual(result.small, { "label": "Small", "width": 240, "height": 160, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_aeb3b41d62_m.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/s\/", "photo": true }); + t.deepEqual(result.small320, { "label": "Small 320", "width": 320, "height": 213, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_aeb3b41d62_n.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/n\/", "photo": true }); + t.deepEqual(result.medium, { "label": "Medium", "width": 500, "height": 333, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_aeb3b41d62.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/m\/", "photo": true }); + t.deepEqual(result.medium640, { "label": "Medium 640", "width": 640, "height": 427, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_aeb3b41d62_z.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/z\/", "photo": true }); + t.deepEqual(result.medium800, { "label": "Medium 800", "width": 800, "height": 534, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_aeb3b41d62_c.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/c\/", "photo": true }); + t.deepEqual(result.large, { "label": "Large", "width": 1024, "height": 683, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_aeb3b41d62_b.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/l\/", "photo": true }); + t.deepEqual(result.large1600, { "label": "Large 1600", "width": 1600, "height": 1067, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_5d4a393159_h.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/h\/", "photo": true }); + t.deepEqual(result.large2048, { "label": "Large 2048", "width": 2048, "height": 1365, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_3c581b37ec_k.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/k\/", "photo": true }); + t.deepEqual(result.original, { "label": "Original", "width": 5472, "height": 3648, "source": "https:\/\/farm3.staticflickr.com\/2922\/14321741011_0ddc14584b_o.jpg", "url": "https:\/\/www.flickr.com\/photos\/azer\/14321741011\/sizes\/o\/", "photo": true }); + + }); + +});