-
-
Save sebastianst/4342078 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
import gtk | |
import simplejson | |
import urllib2 | |
import subprocess | |
import datetime | |
import string | |
import random | |
import time | |
from os.path import expanduser | |
# !!! DEPENDS ON NOTIFY OSD BINARIES FOR NOTIFICATIONS !!! | |
# sudo apt-get install libnotify-bin | |
############################################ | |
# CONFIGURATION # | |
############################################ | |
DROPBOX_PUBLIC_PATH = expanduser("~/Dropbox/Public/") # ensure path ends in / | |
WAIT_FOR_DROPBOX = True # don't exit script and return URL until upload is complete | |
SUB_PATH = "screenshots/" # set this if you don't want screengrabs in root public dir, make sure this path exists and ends in / | |
# URL SHORTENER API SETTINGS | |
USE_URL_SHORTENER = True | |
API_PATH = "http://is.gd/create.php?format=json&url=$longurl" # $longurl will be replaced with image URL | |
API_RESPONSE_TYPE = "json" # or "plain" | |
JSON_SHORTURL_KEY = "shorturl" # if using "json" above provide the keyname of the short response | |
############################################ | |
# END CONFIGURATION # | |
############################################ | |
subprocess.call(["gnome-screenshot", "-c", "-a"]) | |
clipboard = gtk.Clipboard() | |
content = clipboard.wait_for_image() | |
filename = "%s_%s.png" % ( | |
datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S"), | |
''.join(random.choice(string.ascii_uppercase) for x in range(5)) | |
) | |
file = "%s%s%s" % ( | |
DROPBOX_PUBLIC_PATH, | |
SUB_PATH, | |
filename) | |
content.save(file,"png") | |
longurl = subprocess.check_output(['dropbox', 'puburl', file]).rstrip() | |
if USE_URL_SHORTENER: | |
api_url = string.Template(API_PATH).substitute(longurl=longurl) | |
response = urllib2.urlopen(api_url) | |
if API_RESPONSE_TYPE == "json": | |
res_dict = simplejson.loads(response.read()) | |
shorturl = res_dict[JSON_SHORTURL_KEY] | |
elif API_RESPONSE_TYPE == "plain": | |
shorturl = response.read() | |
else: | |
shorturl = longurl | |
clipboard.set_text(shorturl) | |
clipboard.store() | |
if WAIT_FOR_DROPBOX: | |
subprocess.call(["notify-send", "Screengrab is being uploaded...", "--hint=int:transient:1"]) | |
while subprocess.check_output(['dropbox', 'status'])[:4] != "Idle": | |
time.sleep(2) | |
subprocess.call(["notify-send", "Screengrab posted. URL copied to clipboard", shorturl, "--hint=int:transient:1"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for this useful script! I know this is a fork, but this is improved a lot.