From 0298857602b5a80890c0b7c3be542a3cad289e06 Mon Sep 17 00:00:00 2001 From: hydrog3n Date: Sat, 4 Jul 2015 18:30:43 +0200 Subject: [PATCH 1/7] Start multi-service Pour le moment que chevereto. Imgur en cours --- app.js | 84 +++++++++++++++---------------------------- package.json | 1 + services/chevereto.js | 40 +++++++++++++++++++++ settings.json.default | 12 +++++-- 4 files changed, 79 insertions(+), 58 deletions(-) create mode 100644 services/chevereto.js diff --git a/app.js b/app.js index 80bfe9e..67f27c5 100644 --- a/app.js +++ b/app.js @@ -1,23 +1,43 @@ var chokidar = require('chokidar'); -var request = require('request'); -var fs = require('fs'); var copy = require("copy-paste"); var notifier = require('node-notifier'); +var fs = require('fs'); var settings = require('./settings.json'); var p = require('path'); var debug = require('debug')('upload-screenshot'); var async = require('async'); +var service = settings.services[settings.used]; + var watcher = chokidar.watch(settings.dir, { ignoreInitial: true, persistent: true, ignored: /[\/\\]\./ //ignore dotfiles }); -var q = async.queue(function(task, callback) { - callback(); +var serv = require('./services/'+service.name+'.js'); + +var q = async.queue(function(path, callback) { + + serv.upload(service, path, function(){ + copyShortLink(serv.shortlink, path); + }); + }, 1); +function copyShortLink(shortlink, path) { + + copy.copy(shortlink, function() { + notifier.notify({ + 'title': 'Uploaded', + 'message': shortlink, + 'appIcon': __dirname + '/icones/up.png', + 'contentImage': path, + 'open': shortlink + }); + }); +} + function isPicture(path) { return !!~['jpeg','jpg','png','gif','bmp','ico'] .indexOf(p.extname(path).substring(1)); @@ -27,56 +47,10 @@ watcher.on('add', function(path) { if(!isPicture(path)) return; - q.push({name:'exist'}, fs.exists(path, function(exist) { + fs.exists(path, function(exist) { if(!exist) return; - })); - - q.push({name:'request'}, function() { - - var formData = { - upload: fs.createReadStream(path) - }; - - var form = { - key: settings.key ? settings.key : undefined - }; - - debug('Posting picture from path %s to url %s', path, settings.urlapi) - - request.post(settings.urlapi, {formData: formData, form: form}, function (err, res, body) { - - if (err) { - notifier.notify({ - 'title': 'Error !', - 'message': err - }); - - return console.error('upload failed:', err); - } - - var response = JSON.parse(body); - - if (response.status_code !== 200) { - return notifier.notify({ - 'title': 'Error while updating screenshot!', - 'message': response.status_txt - }); - - } - - var shortlink = response.data.image_short_url; - - //copy to clipboard - copy.copy(shortlink, function() { - notifier.notify({ - 'title': 'Uploaded', - 'message': shortlink, - 'appIcon': __dirname + '/icones/up.png', - 'contentImage': path, - 'open': shortlink - }); - }); - }); - }) -}) + + q.push(path); + }); +}); diff --git a/package.json b/package.json index fe4d0b8..f0182d6 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "copy-paste": "^1.1.2", "debug": "^2.2.0", "fs": "0.0.2", + "imgur": "^0.1.5", "node-notifier": "^4.2.3", "path": "^0.11.14", "request": "^2.58.0" diff --git a/services/chevereto.js b/services/chevereto.js new file mode 100644 index 0000000..551035e --- /dev/null +++ b/services/chevereto.js @@ -0,0 +1,40 @@ +var chevereto = exports; +var request = require('request'); +var fs = require('fs'); +var notifier = require('node-notifier'); + +chevereto.response = null; +chevereto.shortlink = null; +chevereto.upload = function(service, path, callback) { + var formData = { + upload: fs.createReadStream(path) + }; + + var form = { + key: service.key ? service.key : undefined + }; + + request.post(service.urlapi, {formData: formData, form: form}, function (err, res, body) { + if (err) { + notifier.notify({ + 'title': 'Error !', + 'message': err + }); + + return console.error('upload failed:', err); + } + + chevereto.response = JSON.parse(body); + + if (chevereto.response.status_code !== 200) { + return notifier.notify({ + 'title': 'Error while updating screenshot!', + 'message': chevereto.response.status_txt + }); + } + + chevereto.shortlink = chevereto.response.data.image_short_url; + + callback(); + }); +}; \ No newline at end of file diff --git a/settings.json.default b/settings.json.default index 46df0ba..4d60aa3 100644 --- a/settings.json.default +++ b/settings.json.default @@ -1,5 +1,11 @@ { - "urlapi":"http://pix.hydrog3n.fr/api", - "key":"", - "dir":"/Users/hydrog3n/Screenshots" + "used":0, + "dir":"/Users/hydrog3n/Screenshots", + "services": [ + { + "name":"chevereto", + "urlapi":"http://pix.hydrog3n.fr/api", + "key":"" + } + ] } \ No newline at end of file From a6e7cf3cafc44cdf1468ba9d6d5e495e63f8f55f Mon Sep 17 00:00:00 2001 From: hydrog3n Date: Sun, 5 Jul 2015 02:11:35 +0200 Subject: [PATCH 2/7] Utilisation de classe & ajout imgur --- app.js | 13 ++++++++++--- readme.md | 10 +++++++--- services/chevereto.js | 29 +++++++++++++++-------------- services/imgur.js | 25 +++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 services/imgur.js diff --git a/app.js b/app.js index 67f27c5..38ea5ef 100644 --- a/app.js +++ b/app.js @@ -15,12 +15,19 @@ var watcher = chokidar.watch(settings.dir, { ignored: /[\/\\]\./ //ignore dotfiles }); -var serv = require('./services/'+service.name+'.js'); +try { + var Serv = require('./services/'+service.name); + var s = new Serv(service); + +} catch(e) { + console.error(e); + process.exit(1); +} var q = async.queue(function(path, callback) { - serv.upload(service, path, function(){ - copyShortLink(serv.shortlink, path); + s.upload(path, function(shortlink){ + copyShortLink(shortlink, path); }); }, 1); diff --git a/readme.md b/readme.md index 00d67a9..0411573 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,6 @@ # Upload screenshot -Multi OS app for upload your screen on a chevereto hosting (if API enable). -You can use with my image hosting. +Multi-OS app for upload your screen on multiple image hosting (chevereto, imgur). # Requirements @@ -43,4 +42,9 @@ vim settings.json ```bash npm install pm2 start app.js -``` \ No newline at end of file +``` + +# BUGS + +I test my app on Mac OS X (10.10.4). +Please if you use an others OS add a new issue with your OS. \ No newline at end of file diff --git a/services/chevereto.js b/services/chevereto.js index 551035e..59cc17b 100644 --- a/services/chevereto.js +++ b/services/chevereto.js @@ -1,20 +1,21 @@ -var chevereto = exports; var request = require('request'); var fs = require('fs'); var notifier = require('node-notifier'); -chevereto.response = null; -chevereto.shortlink = null; -chevereto.upload = function(service, path, callback) { +function Chevereto(service) { + this.service = service; +}; + +Chevereto.prototype.upload = function(path, callback) { var formData = { upload: fs.createReadStream(path) }; var form = { - key: service.key ? service.key : undefined + key: this.service.key ? this.service.key : undefined }; - request.post(service.urlapi, {formData: formData, form: form}, function (err, res, body) { + request.post(this.service.urlapi, {formData: formData, form: form}, function (err, res, body) { if (err) { notifier.notify({ 'title': 'Error !', @@ -24,17 +25,17 @@ chevereto.upload = function(service, path, callback) { return console.error('upload failed:', err); } - chevereto.response = JSON.parse(body); + var response = JSON.parse(body); - if (chevereto.response.status_code !== 200) { + if (response.status_code !== 200) { return notifier.notify({ 'title': 'Error while updating screenshot!', - 'message': chevereto.response.status_txt + 'message': response.status_txt }); } - - chevereto.shortlink = chevereto.response.data.image_short_url; - - callback(); + + callback(response.data.image_short_url); }); -}; \ No newline at end of file +}; + +module.exports = Chevereto; \ No newline at end of file diff --git a/services/imgur.js b/services/imgur.js new file mode 100644 index 0000000..1b1601b --- /dev/null +++ b/services/imgur.js @@ -0,0 +1,25 @@ +var img = require('imgur'); +var notifier = require('node-notifier'); + +function Imgur(service) { + this.service = service; +}; + +Imgur.prototype.upload = function(path,callback) { + + img.setClientId(this.service.clientId); + + img.uploadFile(path) + .then(function (json) { + callback(json.data.link); + }) + .catch(function (err) { + console.error(err); + notifier.notify({ + 'title': 'Error !', + 'message': err + }); + }); +}; + +module.exports = Imgur; From eced496f181d32a08662e7154583a7cc34f5be4b Mon Sep 17 00:00:00 2001 From: hydrog3n Date: Sun, 5 Jul 2015 02:12:28 +0200 Subject: [PATCH 3/7] Update Settings default for imgur --- settings.json.default | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/settings.json.default b/settings.json.default index 4d60aa3..47761e9 100644 --- a/settings.json.default +++ b/settings.json.default @@ -6,6 +6,10 @@ "name":"chevereto", "urlapi":"http://pix.hydrog3n.fr/api", "key":"" + }, + { + "name":"imgur", + "clientId":"YourClientID" } ] } \ No newline at end of file From a2ff9ec8570020722ccd011a8613a26f46371774 Mon Sep 17 00:00:00 2001 From: hydrog3n Date: Sun, 5 Jul 2015 02:27:22 +0200 Subject: [PATCH 4/7] Ajout de zupmage.eu --- services/zupmage.js | 40 ++++++++++++++++++++++++++++++++++++++++ settings.json.default | 5 +++++ 2 files changed, 45 insertions(+) create mode 100644 services/zupmage.js diff --git a/services/zupmage.js b/services/zupmage.js new file mode 100644 index 0000000..d560d56 --- /dev/null +++ b/services/zupmage.js @@ -0,0 +1,40 @@ +var request = require('request'); +var fs = require('fs'); +var notifier = require('node-notifier'); + +function Zupmage(service) { + this.service = service; +}; + +Zupmage.prototype.upload = function(path, callback) { + + var formData = { + f: fs.createReadStream(path), + k: this.service.k, + prive: this.service.prive + }; + + request.post("http://www.zupmage.eu/api", {formData: formData}, function (err, res, body) { + if (err) { + notifier.notify({ + 'title': 'Error !', + 'message': err + }); + + return console.error('upload failed:', err); + } + + var response = JSON.parse(body); + + if (response.error !== false) { + return notifier.notify({ + 'title': 'Error while updating screenshot!', + 'message': response.error + }); + } + + callback(response.url.viewer); + }); +}; + +module.exports = Zupmage; \ No newline at end of file diff --git a/settings.json.default b/settings.json.default index 47761e9..d69793b 100644 --- a/settings.json.default +++ b/settings.json.default @@ -10,6 +10,11 @@ { "name":"imgur", "clientId":"YourClientID" + }, + { + "name":"zupmage", + "k":"yTbRtAPI0ZMlsOaulVoicoiNniYI4b", + "prive":1 } ] } \ No newline at end of file From 434557ca9fd6a004ebbcdd3069d93ddc24536ba9 Mon Sep 17 00:00:00 2001 From: hydrog3n Date: Sun, 5 Jul 2015 02:33:14 +0200 Subject: [PATCH 5/7] Edit settings default --- settings.json.default | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings.json.default b/settings.json.default index d69793b..435069d 100644 --- a/settings.json.default +++ b/settings.json.default @@ -13,7 +13,7 @@ }, { "name":"zupmage", - "k":"yTbRtAPI0ZMlsOaulVoicoiNniYI4b", + "k":"YourAPIKey", "prive":1 } ] From 4066837a3b8fbdb7298e015e032ffc04614f0634 Mon Sep 17 00:00:00 2001 From: hydrog3n Date: Wed, 8 Jul 2015 17:11:37 +0200 Subject: [PATCH 6/7] Ignore ide settings --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8030b48..0c5fe47 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -settings.json \ No newline at end of file +settings.json +.settings/ \ No newline at end of file From 711174e60e5d82699c716728e0aadf6379a50fa8 Mon Sep 17 00:00:00 2001 From: hydrog3n Date: Wed, 8 Jul 2015 17:20:37 +0200 Subject: [PATCH 7/7] Oublie du callback Le callback pour relancer la queue. --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 38ea5ef..d528dcd 100644 --- a/app.js +++ b/app.js @@ -29,7 +29,7 @@ var q = async.queue(function(path, callback) { s.upload(path, function(shortlink){ copyShortLink(shortlink, path); }); - + callback(); }, 1); function copyShortLink(shortlink, path) {