Created
July 20, 2017 22:27
-
-
Save tedchoward/d5a06e4f54cbb4b889e6487567c1e08e to your computer and use it in GitHub Desktop.
Download all your images from Flickr. (Does not work for videos)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Flickr = require('flickrapi'); | |
const Bluebird = require('bluebird'); | |
const request = require('request'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const DOWNLOAD_PATH = path.resolve(process.env.HOME, 'Downloads/flickr'); | |
const flickrOptions = { | |
api_key: process.env.FLICKR_API_KEY, | |
secret: process.env.FLICKR_API_SECRET | |
}; | |
function getPhotoUrl({ farm, server, id, originalsecret, originalformat }) { | |
return `https://farm${farm}.staticflickr.com/${server}/${id}_${originalsecret}_o.${originalformat}`; | |
} | |
async function main() { | |
const flickr = await Bluebird.fromCallback(callback => Flickr.authenticate(flickrOptions, callback)); | |
const getPhotos = Bluebird.promisify(flickr.people.getPhotos); | |
let currentPage = 1; | |
let totalPages = 1; | |
let results = []; | |
for (currentPage = 1; currentPage <= totalPages; currentPage++) { | |
const result = await getPhotos({ | |
user_id: flickr.options.user_id, | |
authenticated: true, | |
extras: 'original_format', | |
page: currentPage | |
}); | |
totalPages = result.photos.pages; | |
results = results.concat(result.photos.photo); | |
} | |
await Bluebird.map(results, photo => new Bluebird((resolve, reject) => { | |
const url = getPhotoUrl(photo); | |
const fileName = url.substr(url.lastIndexOf('/') + 1); | |
const filePath = path.resolve(DOWNLOAD_PATH, fileName); | |
console.log(`Downloading ${url} to ${filePath}`); | |
request | |
.get(url) | |
.on('error', reject) | |
.pipe(fs.createWriteStream(filePath)) | |
.on('finish', () => { | |
console.log(`Downloaded ${filePath}`); | |
resolve(); | |
}); | |
}), { concurrency: 5 }); | |
console.log('All Photos Downloaded!'); | |
} | |
main().catch(err => console.error(err)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install bluebird flickrapi request |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment