Skip to content

Instantly share code, notes, and snippets.

@jmoiron
Last active September 29, 2015 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmoiron/1602141 to your computer and use it in GitHub Desktop.
Save jmoiron/1602141 to your computer and use it in GitHub Desktop.
Script to run in compiz screenshot "launch app" to upload screenshots to my server
#!/bin/bash
# capture an area on OSX and run quickup on the produced file
CAPPATH="$HOME/tmp-capture.png"
if [ -n `which screencapture` ]; then
screencapture -i $CAPPATH
# esc exits screencapture without writing, don't try to upload in this case
if [ -f "$CAPPATH" ]; then
# make sure that it can find terminal-notifier on the PATH
export PATH=$PATH:/usr/local/bin/
$HOME/.local/bin/quickup $CAPPATH > $HOME/quickup.log
rm $CAPPATH
fi
fi
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Quickly upload shit to my server."""
import os, platform, sys
import string
import random
import subprocess
import optparse
OS = platform.system()
enable_notifications = False
def which(program):
"""Return the path of program or the empty string if not found."""
for path in os.environ["PATH"].split(os.pathsep):
if os.path.exists(os.path.join(path, program)):
return os.path.join(path, program)
return ""
# do OS/version checks up front so we know we can do notifications
if OS == 'Darwin':
version = map(int, platform.mac_ver()[0].split('.'))
if version < [10, 9]:
print "ERROR: OSX support only for 10.9+ (new notification-center required)"
sys.exit(-1)
if not which("pbcopy"):
print "ERROR: could not find pbcopy, which is required for quickup."
sys.exit(-1)
if not which("terminal-notifier"):
print "ERROR: notifications require `terminal-notifier`, install via brew."
else:
enable_notifications = True
elif OS == 'Linux':
if not which("xclip"):
print "ERROR: Could not find xclip, which is required for quickup."
sys.exit(-1)
try:
import pynotify
enable_notifications = True
except ImportError:
import traceback
print "Could not import pynotify, error was:"
traceback.print_exc()
print "Continuing without notifications."
else:
print "Sorry, I haven't tested on your OS. Please email me for support."
def copy(text):
"""Copy some text to the clipboard. Return True if it succeeded,
False otherwise."""
prepared = text.strip().replace("'", "\\'")
if OS == "Darwin":
proc = subprocess.Popen("echo '%s' | pbcopy" % prepared, shell=True)
else:
proc = subprocess.Popen("echo '%s' | xclip" % prepared, shell=True)
proc.wait()
return not proc.returncode
def base36(number):
def convert(num, dd=False):
if not dd:
dd = dict(zip(range(36), list(string.digits+string.ascii_lowercase)))
if num == 0: return ''
num,rem = divmod(num, 36)
return convert(num, dd) + dd[rem]
return convert(number)
def notify(message):
if not enable_notifications:
return
if OS == 'Darwin':
# call terminal-notifier; use the terminal -sender because some
# versions don't work without it
subprocess.call(['terminal-notifier', '-open', 'quickup', '-message',
message, '-sender', 'com.apple.Terminal'])
else:
if not pynotify.init("quickup"):
return
n = pynotify.Notification(message)
n.set_timeout(3500)
n.show()
def make_filename(hint):
if hint != "auto":
return hint
return base36(random.randint(2**16,2**36))
def parse_args():
parser = optparse.OptionParser(version="1.0")
parser.add_option("-d", "--destination", default="jmoiron.net:images/")
parser.add_option("-u", "--url-base", default="http://jmoiron.net/i/")
parser.add_option("-f", "--filename", default="auto")
opts, args = parser.parse_args()
return opts, args
def main():
opts, args = parse_args()
local_name = args[0]
filetype = local_name.rsplit('.', 1)[-1]
remote_name = make_filename(opts.filename) + '.%s' % (filetype.lower())
remote_url = opts.url_base + remote_name
remote_dir = opts.destination.rstrip('/')
if remote_dir.endswith(':'):
remote_dest = remote_dir + remote_name
else:
remote_dest = '%s/%s' % (remote_dir, remote_name)
p = subprocess.Popen(['scp', local_name, remote_dest])
p.wait()
if not p.returncode:
copy(remote_url)
print "Uploaded %s to %s" % (local_name, remote_url)
notify("Uploaded %s to %s" % (local_name, remote_url))
else:
notify("Something went wrong uploading %s to %s" % (local_name, remote_dest))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment