Skip to content

Instantly share code, notes, and snippets.

@afontcu
Created May 26, 2018 17:07
Show Gist options
  • Save afontcu/cee4c4485e1880a92c7263863c5d7405 to your computer and use it in GitHub Desktop.
Save afontcu/cee4c4485e1880a92c7263863c5d7405 to your computer and use it in GitHub Desktop.
const fs = require('fs')
const request = require('request')
const download = (uri, filename, callback) => {
request.head(uri, (err, res, body) => {
if (err) throw err
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback)
})
}
fs.readFile('data/data.json', 'utf8', (err, data) => {
if (err) throw err
const resources = JSON.parse(data).resources
Object.values(resources).forEach(({ name, image }) => {
if (image) {
const extension = image.split('.').pop()
const normalizedName = name.replace(/\W+/g, '-').toLowerCase()
const filename = `assets/logos/${normalizedName}.${extension}`
download(image, filename, () => { console.log('done') })
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment