forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshorturl
executable file
·122 lines (98 loc) · 3.61 KB
/
shorturl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/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)