Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 123 lines (98 sloc) 3.61 KB
#!/usr/bin/env python
# URL shortener. Saves needing to copy a link, go to a bookmarked web page,
# paste the link, read the result page, copy that link, go paste it in
# twitter, close the tab.
#import urllib, urllib2, httplib
import sys
import urllib, urllib2
import gtk
import re
# We have three different shortening services:
# isgd API reference: http://is.gd/apishorteningreference.php
def isgd(url, keyword=None):
isgdurl = 'http://is.gd/create.php?format=simple&url=' + \
urllib.quote(url)
# Allow for an optional keyword, instead of getting a random
# string of characters:
if keyword:
isgdurl += '&shorturl=' + keyword
page = urllib2.urlopen(isgdurl)
shorturl = page.read()
page.close()
# Now check to make sure it worked:
isgdurl = 'http://is.gd/forward.php?format=simple&shorturl=' + shorturl
page = urllib2.urlopen(isgdurl)
longurl = page.read()
page.close()
return shorturl, longurl
def shorturl(url):
apiurl = "http://shorturl.com/make_url.php?longurl=" + urllib.quote(url)
page = urllib2.urlopen(apiurl)
contents = page.read()
page.close()
shorturl = re.findall("<a href=(http://alturl.com/\w+) target=\"_blank\">", contents)[0]
longurl = re.findall('<INPUT NAME="longurl".* VALUE="(.*?)"', contents)[0]
return shorturl, longurl
def tinyurl(url):
tiny = "http://tinyurl.com/api-create.php?url=%s" %(url)
page = urllib2.urlopen(tiny)
tiny = page.read()
page.close()
# Just return the original long URL and hope that's really
# where the short link goes -- I can't find any way of verifying.
return tiny, url
# The list of shorteners, in the order we want to try them:
shorteners = [
('shorturl', shorturl),
('isgd', isgd),
('tinyurl', tinyurl)
]
def SimpleURLWindow(shorturl, longurl, servicename=None):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title('Shortened URL')
window.connect("delete_event", gtk.main_quit)
vbox = gtk.VBox(True, 2)
window.add(vbox)
if servicename:
vbox.pack_start(gtk.Label('Shortened with ' + servicename))
entry = gtk.Entry()
entry.set_text(shorturl)
entry.select_region(0, -1)
vbox.pack_start(entry)
#vbox.pack_start(gtk.Label(shorturl))
vbox.pack_start(gtk.Label(longurl))
def key_press(widget, event) :
if event.string == "q" :
sys.exit(0)
return True
return False
window.connect("key-press-event", key_press)
# And select the short URL:
clipboard = gtk.Clipboard(selection=gtk.gdk.SELECTION_PRIMARY)
clipboard.set_text(shorturl)
# Aguably, we might want to set a timer and make the window
# auto-disappear after a minute or so.
window.show_all()
gtk.main()
if __name__ == "__main__":
# Should we take the URL as an argument, or get it from the clipboard?
if len(sys.argv) < 2 or sys.argv[1].startswith('-'):
# Get it from the clipboard
primary = gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY)
if not primary.wait_is_text_available() :
print "Nothing on the primary selection!"
sys.exit(0)
url = primary.wait_for_text()
else:
url = sys.argv[1]
for (name, func) in shorteners:
try:
shorturl, longurl = func(url)
if shorturl and longurl:
print "Shortened with", name
print '%s (from %s)' % (shorturl, longurl)
break
except Exception, e:
print "Failed on", name
print e
SimpleURLWindow(shorturl, longurl, name)