Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #8 from kaimallea/refactor
Browse files Browse the repository at this point in the history
Refactor code
  • Loading branch information
kaimallea committed Aug 12, 2014
2 parents d20b88b + b367c9c commit 93a0410
Show file tree
Hide file tree
Showing 9 changed files with 436 additions and 274 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
73 changes: 43 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,61 @@
## Installation
## Command-line Usage

npm install -g imgur
### Installation

## Command-line Usage
```bash
npm install imgur -g
```

**You must set an API key before CLI use; get one at http://imgur.com/register/api_anon**
### Usage

Pass binary image files, urls, and/or base64-encoded image strings as arguments. Globbing is supported.

Set your API key once and forget it (saved to ~/.imgurkey)
Upload a single image:

imgur -k aCs53GSs4tga0ikp
```bash
imgur cat.png
```

Upload a single image
Upload all the jpegs in a particular folder:

imgur GooglePlus.png
```bash
imgur ~/Pictures/kittens/*.jpg
```

Upload an entire directory (**not recursive yet**; automatically chooses pics)
Upload an image from another place on the web:

imgur ~/Pictures/
```bash
imgur --url http://lolz.pics.com/troll.png
```

Upload all .png files in the current directory
Upload a Base-64 encoded image:

imgur *.png

```bash
imgur --base64 iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAmUlEQVQ4je2TsQ3CMBBFnxMa08WR2IQKJskIUNwMZAcYwWIQMs65JCUpEEIYW4pJy6v+6e6+/hVnnGsAzsCBMi7AsbbW/rIMsAU2xrnmkeruuzW7zgIw+JGbv6fGQpWzfy3HOsJlDQY/AlCv3jpF9oS5ZBOICKoB1YCIlCdQDR9127qyBHP5Gyw3CBXPr/qi709JHXE1S995AsqoJu8x78GsAAAAAElFTkSuQmCC
```

## Module Usage

```javascript
var imgur = require('imgur');
### Installation

imgur.setKey('aCs53GSs4tga0ikp');

imgur.upload('/Users/kmallea/Pictures/Manga_Kai.jpg', function (response) {

if (response.error) {
console.log(response.error);
return;
}

console.log('Direct link: ' + response.links.original);
console.log('Imgur page: ' + response.links.imgur_page);
});
```bash
npm install imgur
```
## Contributors

- Helge S. Holm (https://github.com/deestan)
- Nicolas Bevacqua (https://github.com/bevacqua)
### Usage

```javascript
var imgur = require('imgur');

// Set a Client ID for your app
imgur.setClientId('aCs53GSs4tga0ikp');

// Methods return promises
imgur.uploadFile('/home/kai/*.png')
.then(function(json) {
console.log(json.data.link);
})
.catch(function(err) {
console.error(err.message);
});
```
88 changes: 88 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env node
var imgur = require('./lib/imgur.js');
var commander = require('commander');
var util = require('util');

// Used to collect args for specific options
function collect (val, arr) {
arr.push(val);
return arr;
}

commander
.version(imgur.VERSION)
.option('-i, --info [id ...]', 'Lookup images by ID', collect, [])
.option('-b, --base64 [string ...]', 'Upload a base64-encoded images', collect, [])
.option('-u, --url [url ...]', 'Upload URLs', collect, [])
.option('-f, --file [file ...]', 'Upload binary image files', collect, [])
.parse(process.argv);

if (commander.info.length) {
commander.info.forEach(function(id, index, array) {
imgur.info(id)
.then(function (json) {
console.log(json.data);
})
.catch(function (err) {
console.error(err.message);
});
});
}


if (commander.url.length) {
commander.url.forEach(function(url, index, array) {
imgur.uploadUrl(url)
.then(function (json) {
var output;
if (commander.url.length > 1) {
output = util.format('%s -> %s', url, json.data.link);
} else {
output = json.data.link;
}
console.log(output);
})
.catch(function (err) {
console.error(err.message);
});
});
}


if (commander.base64.length) {
commander.base64.forEach(function(str, index, array) {
imgur.uploadBase64(str)
.then(function (json) {
var output;
if (commander.base64.length > 1) {
output = util.format('%s... -> %s', str.substr(0, 7), json.data.link);
} else {
output = json.data.link;
}
console.log(output);
})
.catch(function (err) {
console.error(err.message);
});
});
}


if (commander.file.length || commander.args.length) {
var args = commander.file.concat(commander.args);
args.forEach(function(file, index, array) {
imgur.uploadFile(file)
.then(function (json) {
var output;
if (args.length > 1) {
output = util.format('%s -> %s', file, json.data.link);
} else {
output = json.data.link;
}
console.log(output);
})
.catch(function (err) {
console.error(err.message);
});
});
}
Loading

0 comments on commit 93a0410

Please sign in to comment.