Skip to content

Instantly share code, notes, and snippets.

@funmaker
Created August 31, 2018 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funmaker/1325d785c2bc30dbc85330a487fcebeb to your computer and use it in GitHub Desktop.
Save funmaker/1325d785c2bc30dbc85330a487fcebeb to your computer and use it in GitHub Desktop.
#!/bin/node
// Requires:
// imagemagic
// xdotool
// xclip
const email ='EMAIL';
const apikey ='APIKEY';
const saveFolder = 'STORAGE_FOLDER';
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
const request = require('request-promise-native');
const dateFormat = require('dateformat');
const argv = require('minimist')(process.argv.slice(2));
if(argv.h || argv.help) {
console.log("usage:\tpuush [options] [filename]");
console.log("");
console.log("Flags:");
console.log("\t-m MODE\t--mode=MODE\tchoose mode");
console.log("\t-h\t--help\t\tshow this message");
console.log("");
console.log("Avaliable Modes:");
console.log("\tfile\tspecified file(default)");
console.log("\tscreen\tentire screen");
console.log("\twindow\tcurrently window");
console.log("\tarea\tscreen area");
console.log("\tclip\tclipboard content");
}
function getTimeDate() {
return dateFormat(new Date(), 'yyyy-mm-dd') + " at " + dateFormat(new Date(), 'H:MM:ss');
}
function panic(msg) {
console.error(msg || "Something happened");
process.exit(-1);
}
async function puush(value, options) {
cp.execSync("notify-send -i up 'Puushing...'");
let response;
try {
response = await request({
url: 'https://puush.me/api/up',
method: 'POST',
formData: {
'z': 'lol',
'e': email,
'k': apikey,
'f': {
value,
options
}
}
});
} catch(e) {
cp.execSync('notify-send -i dialog-no "Puush failed!"');
throw e;
}
const url = /.*,(https:\/\/[^,]*),.*/.exec(response)[1];
console.log(url);
const clip = cp.exec(`xclip -selection clipboard`, {timeout: 1000});
clip.stdin.write(url);
clip.stdin.end();
cp.execSync('notify-send -i dialog-ok "Puush complete!" "Link has been copied to clipboard."');
return url;
}
const mode = argv.mode || argv.m || "file";
(async() => {
switch(mode) {
case "file": {
let [file, ...rest] = argv._;
if(!file) panic("No file specified.");
if(rest.length > 0) panic("Too many files specified.");
let content = fs.readFileSync(file);
await puush(content, {
filename: path.basename(file),
});
break;
}
case "screen": {
const ss = cp.execSync("import -window root png:-");
const filename = `ss (${getTimeDate()}).png`;
await puush(ss, {
filename,
});
fs.writeFileSync(path.resolve(saveFolder, filename), ss);
break;
}
case "window": {
const window = cp.execSync("xdotool getactivewindow").toString().trim();
const ss = cp.execSync(`import -window ${window} -frame png:-`);
const filename = `ss (${getTimeDate()}).png`;
await puush(ss, {
filename,
});
fs.writeFileSync(path.resolve(saveFolder, filename), ss);
break;
}
case "area": {
let ss = cp.execSync("import png:-");
const filename = `ss (${getTimeDate()}).png`;
await puush(ss, {
filename,
});
fs.writeFileSync(path.resolve(saveFolder, filename), ss);
break;
}
case "clip": {
let targets;
let selection;
try {
targets = cp.execSync("xclip -o -t TARGETS -selection clipboard").toString().trim().split("\n");
selection = "clipboard";
} catch(e) {
targets = cp.execSync("xclip -o -t TARGETS -selection primary").toString().trim().split("\n");
selection = "primary";
}
if(targets.includes('text/uri-list') || targets.includes('x-special/gnome-copied-files') || targets.includes('application/x-kde-cutselection')) {
let file, rest;
if(targets.includes('text/uri-list')) {
[file, ...rest] = cp.execSync(`xclip -o -selection ${selection} -target text/uri-list`).toString().trim().split("\n");
} else if(targets.includes('x-special/gnome-copied-files')) {
[file, ...rest] = cp.execSync(`xclip -o -selection ${selection} -target x-special/gnome-copied-files`).toString().trim().split("\n").slice(1);
} else if(targets.includes('application/x-kde-cutselection')) {
[file, ...rest] = cp.execSync(`xclip -o -selection ${selection} -target application/x-kde-cutselection`).toString().trim().split("\n").slice(1);
}
if(!file) panic("No file specified.");
if(rest.length > 0) panic("Too many files specified.");
file = /file:\/\/(.*)/.exec(file)[1];
const content = fs.readFileSync(file);
await puush(content, {
filename: path.basename(file),
});
} else {
const accepts = [
"image/png",
"image/jpeg",
"text/plain",
"STRING",
];
const target = accepts.find(ac => targets.includes(ac));
if(!target) panic(`Not acceptable.\nTypes:${JSON.stringify(targets, null, 4)}\n`);
const content = cp.execSync(`xclip -o -selection ${selection} -target ${target}`);
await puush(content, {
filename: `clip (${getTimeDate()})`,
});
}
break;
}
default:
console.error(`Unknown Mode: ${mode}`);
break;
}
})().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment