Created
July 19, 2015 09:06
-
-
Save karl-gustav/43dff5287a81b8a4a29c to your computer and use it in GitHub Desktop.
upload all image files in current folder to imgur (to install dependencies: `npm i request`)
This file contains hidden or 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
#! /usr/bin/env node | |
var request = require('request'), | |
fs = require('fs'), | |
endOfLine = require('os').EOL, | |
output = fs.createWriteStream('links.txt'), | |
filesInFolder = fs.readdirSync('.'), | |
URL = 'https://api.imgur.com/3/image/', | |
IMAGE_REGEX = /.*\.(jpg|jpeg|png|gif)/i, | |
IMGUR_CLIENT_ID = '<your client id>'; | |
filesInFolder | |
.filter(onlyImages) | |
.forEach(function (filePath) { | |
fs.createReadStream(filePath) | |
.pipe(request.post({ | |
url: URL, | |
headers: { | |
Authorization: 'Client-ID ' + IMGUR_CLIENT_ID | |
} | |
})) | |
.on('data', function (response) { | |
var data = JSON.parse(response.toString()).data; | |
output.write( | |
filePath + ":\t" + data.link + | |
"\t( delete url: " + URL + data.deletehash + ")" + | |
endOfLine | |
); | |
}) | |
.on('error', function (error) { | |
console.error("error:", error); | |
}) | |
}); | |
function onlyImages (path) { | |
return IMAGE_REGEX.test(path); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment