|
#!/usr/bin/python |
|
|
|
import os, sys, pynotify, hashlib, pygtk, mechanize |
|
pygtk.require('2.0') |
|
import gtk |
|
from re import match |
|
from urllib2 import urlopen, Request, HTTPError |
|
from urllib import urlencode |
|
import httplib2 |
|
|
|
try: |
|
import json |
|
except ImportError: |
|
try: |
|
import simplejson as json |
|
except ImportError: |
|
raise ImportError("You need to have a json parser, easy_install simplejson") |
|
|
|
def main(): |
|
|
|
# arguments |
|
full_name = sys.argv[1] |
|
name = full_name.split('/')[-1] |
|
ext = name.split('.')[-1] |
|
|
|
# md5 |
|
f = open(full_name, 'r') |
|
m = hashlib.md5() |
|
while True: |
|
data = f.read(2**20) |
|
if not data: |
|
break |
|
m.update(data) |
|
new_name = m.hexdigest() + '.' + ext |
|
|
|
url = 'http://dl.dropbox.com/u/11293376/screenshots/' + new_name |
|
url = Googl().shorten(url)['id'] |
|
|
|
# clipboard |
|
clipboard = gtk.clipboard_get() |
|
clipboard.set_text(url) |
|
clipboard.store() |
|
|
|
# notify |
|
n = pynotify.Notification("Screenshot", "Url in your clipboard\n" + url) |
|
n.show() |
|
|
|
# dropbox |
|
upload_file(full_name, "/Public/screenshots/", new_name, "[email protected]", "pAsSwOrD") |
|
|
|
# notify |
|
n = pynotify.Notification("Screenshot", "Uploaded!") |
|
n.show() |
|
|
|
# rm |
|
os.remove(full_name) |
|
|
|
|
|
|
|
# http://code.activestate.com/recipes/578030-dropbox-file-upload-via-web-interface-using-python/ |
|
def upload_file(local_file,remote_dir,remote_file,email,password): |
|
""" Upload a local file to Dropbox """ |
|
|
|
# Fire up a browser using mechanize |
|
br = mechanize.Browser() |
|
br.set_handle_robots(False) |
|
|
|
# Browse to the login page |
|
br.open('https://www.dropbox.com/login') |
|
|
|
# Enter the username and password into the login form |
|
isLoginForm = lambda l: l.action == "https://www.dropbox.com/login" and l.method == "POST" |
|
|
|
try: |
|
br.select_form(predicate=isLoginForm) |
|
except: |
|
print("Unable to find login form."); |
|
exit(1); |
|
|
|
br['login_email'] = email |
|
br['login_password'] = password |
|
|
|
# Send the form |
|
response = br.submit() |
|
|
|
# Add our file upload to the upload form once logged in |
|
isUploadForm = lambda u: u.action == "https://dl-web.dropbox.com/upload" and u.method == "POST" |
|
|
|
try: |
|
br.select_form(predicate=isUploadForm) |
|
except: |
|
print("Unable to find upload form."); |
|
print("Make sure that your login information is correct."); |
|
exit(1); |
|
|
|
br.form.find_control("dest").readonly = False |
|
br.form.set_value(remote_dir,"dest") |
|
br.form.add_file(open(local_file,"rb"),"",remote_file) |
|
|
|
# Submit the form with the file |
|
br.submit() |
|
|
|
# http://code.google.com/p/python-googl/source/browse/googl/__init__.py |
|
class Googl: |
|
"""Access Goo.gl url shorten""" |
|
|
|
def __init__(self, key=None, baseurl="https://www.googleapis.com/urlshortener/v1/url", |
|
user_agent="python-googl"): |
|
self.key = key |
|
self.conn = httplib2.Http() |
|
self.baseurl = baseurl |
|
self.user_agent = user_agent |
|
|
|
def _request(self, url="", method="GET", body="", headers=None, userip=None): |
|
"""send request and parse the json returned""" |
|
if not url: |
|
url = self.baseurl |
|
elif not url.startswith("http"): |
|
url = "%s?%s" % (self.baseurl, url) |
|
if self.key is not None: |
|
url += "%s%s" % ( ("?" if "?" not in url else "&"), "key=%s" % self.key) |
|
if userip: |
|
url += "%s%s" % ( ("?" if "?" not in url else "&"), "userip=%s" % userip) |
|
if headers is None: |
|
headers = {} |
|
if "user-agent" not in headers: |
|
headers['user-agent'] = self.user_agent |
|
return json.loads(self.conn.request(url, method, body=body, headers=headers)[1]) |
|
|
|
def shorten(self, url, userip=None): |
|
"""shorten the url""" |
|
body =json.dumps(dict(longUrl=url)) |
|
headers = {'content-type':'application/json'} |
|
return self._request(method="POST", body=body, headers=headers, userip=userip) |
|
|
|
def expand(self, url, analytics=False, userip=None): |
|
"""expand the url""" |
|
data = dict(shortUrl=url) |
|
if analytics: |
|
data['projection'] = 'FULL' |
|
if userip: |
|
data['userip'] = userip |
|
url = urlencode(data) |
|
|
|
return self._request(url) |
|
|
|
if __name__ == "__main__": |
|
main() |