Skip to content

Instantly share code, notes, and snippets.

@karl-gustav
Created July 19, 2015 09:06
Show Gist options
  • Save karl-gustav/43dff5287a81b8a4a29c to your computer and use it in GitHub Desktop.
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`)
#! /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