forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshorturl-gi3
executable file
·135 lines (109 loc) · 4.29 KB
/
shorturl-gi3
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
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
# 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.
# This is a version written for GTK3 via GObject introspection
# and for the new python3 version of urllib.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
import sys
import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse
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.parse.quote(url)
# Allow for an optional keyword, instead of getting a random
# string of characters:
if keyword:
isgdurl += '&shorturl=' + keyword
page = urllib.request.urlopen(isgdurl)
shorturl = page.read().decode('utf-8')
page.close()
# Now check to make sure it worked:
isgdurl = 'http://is.gd/forward.php?format=simple&shorturl=' + shorturl
page = urllib.request.urlopen(isgdurl)
longurl = page.read().decode('utf-8')
page.close()
if longurl != url:
print("Yikes: original url was '%s', decoded one is '%s'"
% (url, longurl))
return shorturl, longurl
def shorturl(url):
apiurl = "http://shorturl.com/make_url.php?longurl=" + urllib.parse.quote(url)
page = urllib.request.urlopen(apiurl)
contents = page.read().decode('utf-8')
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 = urllib.request.urlopen(tiny).decode('utf-8')
tiny = page.read().decode('utf-8')
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 = [
('isgd', isgd),
# ('shorturl', shorturl),
# ('tinyurl', tinyurl)
]
def SimpleURLWindow(shorturl, longurl, servicename=None):
window = Gtk.Window()
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),
True, True, 0)
entry = Gtk.Entry()
entry.set_text(shorturl)
entry.select_region(0, -1)
vbox.pack_start(entry, True, True, 0)
#vbox.pack_start(Gtk.Label(shorturl))
vbox.pack_start(Gtk.Label(longurl), True, True, 0)
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:
primary = Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY)
primary.set_text(shorturl, -1)
# There's no actual documentation, so probably no one knows what the -1 is.
# 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__":
# SimpleURLWindow("shorturl", "longurl", "noservice")
# sys.exit(0)
# 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(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 as e:
print("Failed on", name)
print(e)
SimpleURLWindow(shorturl, longurl, name)