I saw a post on hackernews this morning where a guy had built a little screenshot uploader for dropbox. Unfortunately, his script is for Mac OS and I use linux.
So, for linux folks out there, here is a little wrapper around scrot, a linux screenshot utility. It will allow you to capture the full screen, the current window, or free-select a region, then take the resulting image and put it in your dropbox folder or upload it to Imgur:
#!/usr/bin/env python
import base64
import json
import optparse
import os
import subprocess
import sys
import time
import urllib
import urllib2
BINARY = 'scrot'
HOME = os.environ['HOME']
# Imgur API -- register your app and paste the client id and secret:
CLIENT_ID = ''
CLIENT_SECRET = ''
# Location of your dropbox folder and your dropbox user id:
DROPBOX_DIR = os.path.join(HOME, 'Dropbox/Public/screens/')
DROPBOX_URL_TEMPLATE = 'http://dl.dropbox.com/u/%s/screens/%s'
DROPBOX_UID = ''
def upload_file(filename):
with open(filename, 'rb') as fh:
contents = fh.read()
payload = urllib.urlencode((
('image', base64.b64encode(contents)),
('key', CLIENT_SECRET),
))
request = urllib2.Request('https://api.imgur.com/3/image', payload)
request.add_header('Authorization', 'Client-ID ' + CLIENT_ID)
try:
resp = urllib2.urlopen(request)
except urllib2.HTTPError, exc:
return False, 'Returned status: %s' % exc.code
except urllib2.URLError, exc:
return False, exc.reason
resp_data = resp.read()
try:
resp_json = json.loads(resp_data)
except ValueError:
return False, 'Error decoding response: %s' % resp_data
if resp_json['success']:
return True, resp_json['data']['link']
return False, 'Imgur failure: %s' % resp_data
def get_parser():
parser = optparse.OptionParser('Screenshot helper')
parser.add_option('-s', '--select', action='store_true', default=True,
dest='select', help='Select region to capture')
parser.add_option('-f', '--full', action='store_true', dest='full',
help='Capture entire screen')
parser.add_option('-c', '--current', action='store_true', dest='current',
help='Capture currently selected window')
parser.add_option('-d', '--delay', default=0, dest='delay', type='int',
help='Seconds to wait before capture')
parser.add_option('-p', '--public', action='store_true', dest='dropbox',
help='Store in dropbox public folder')
parser.add_option('-x', '--no-upload', action='store_false', default=True,
dest='upload', help='Do not upload to imgur')
parser.add_option('-k', '--keep-local', action='store_true', default=False,
dest='keep', help='Keep local copy after upload')
return parser
def get_scrot_command(filename, options):
args = [BINARY]
if options.current:
args.append('-u')
elif not options.full:
args.append('-s')
if options.delay:
args.append('-d %s' % options.delay)
args.append(dest)
return args
if __name__ == '__main__':
parser = get_parser()
options, args = parser.parse_args()
filename = 's%s.png' % time.time()
if options.dropbox:
dest = os.path.join(DROPBOX_DIR, filename)
else:
dest = os.path.join(HOME, 'tmp', filename)
if not options.current and not options.full:
print 'Select a region to capture...'
scrot_args = get_scrot_command(dest, options)
p = subprocess.Popen(scrot_args)
p.wait()
if options.dropbox:
print DROPBOX_URL_TEMPLATE % (DROPBOX_UID, filename)
if options.upload:
success, res = upload_file(dest)
if not success:
print 'Error uploading image: %s' % res
print 'Image stored in: %s' % dest
sys.exit(1)
else:
if not options.keep:
os.unlink(dest)
print res
else:
print dest
Nice one.
As a side note: if you have Dropbox installed then you can do:
$> dropbox puburl $HOME/Dropbox/Public/thefile
to get the public url of thefile.
This way you can avoid using DROPBOX_URL_TEMPLATE and DROPBOX_UID.
Thanks for the tip, very cool! At some point I need to get into their API and see what else is possible.
Commenting has been closed, but please feel free to contact me