My favorites | Sign in
Logo
          
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
#!/usr/bin/env python

usage = '''
Takes the URL of the frontmost Safari (or Firefox) window/tab and
shortens using the service at bit.ly. The shortened URL is put on
the clipboard, ready for pasting. Lastly, you are notified via
growlnotify that your link is ready. You can also call this tool
from the command line and pass it the link to be shortened.
'''

from urllib import urlopen, urlencode
from os import popen
from os import system
import simplejson as json
import sys

LOGIN = 'jolleyjoe'
API_KEY = 'R_95b836f0e3a16b327483d93754cab545'
ERROR = 0

# Get the URL of the frontmost Safari window/tab though AppleScript.
#applescript = '''tell application "Safari"
#URL of front document
#end tell'''

# Get the URL of the frontmost Firefox window/tab though AppleScript.
applescript = '''tell application "Firefox"
set myFirefox to properties of front window as list
get item 3 of myFirefox
end tell'''

if len(sys.argv) == 1:
#rawUrl = popen("osascript -e '" + applescript + "'").read().strip()
rawUrl = popen('pbpaste', "r").read()
elif len(sys.argv) == 2:
rawUrl = sys.argv[1]
else:
print 'ERROR: Incorrect number of arguments, only one or none are allowed.'
sys.exit(1)


def ascii2hex(ascii):
return hex(ord(ascii))


# Fix url to work with bit.ly
url = rawUrl
symbols = ['=', '/', '$', '+', ';', '@', '?', ',', '&', '#']
hexlist = map(ascii2hex, symbols)
for symbol in symbols:
url = url.replace(symbol, '%' + hex(ord(symbol))[2:])

for hex in hexlist:
toReplace = '%' + hex[2:]
upper = toReplace.upper()
rawUrl = rawUrl.replace(toReplace, chr(int(hex, 16)))
rawUrl = rawUrl.replace(upper, chr(int(hex, 16)))

'''
url = rawUrl
url = url.replace('=', '%3d')
url = url.replace('?', '%3f')
url = url.replace(',', '%2c')
url = url.replace('&', '%26')
url = url.replace('#', '%23')
'''

# Get the shortened URL from bit.ly.
query = 'http://api.bit.ly/shorten?version=2.0.1&longUrl=' + url + '&login=' + LOGIN + '&apiKey=' + API_KEY + '&history=1'
result = json.load(urlopen(query))

if result['errorCode'] != 0:
print 'ERROR: ' + result['errorMessage']
ERROR = 1

if "errorCode" in result['results'][rawUrl]:
if result['results'][rawUrl]['errorCode'] != 0:
print 'ERROR: ' + result['results'][rawUrl]['errorMessage']
ERROR = 1

if ERROR:
sys.exit(1)

shortURL = result['results'][rawUrl]['shortUrl']

# Put the shortened URL on the clipboard.
popen('pbcopy', 'w').write(shortURL)

# Display growl notification
cmd = '/usr/local/bin/growlnotify -i HTML -m \'Shortened ' + rawUrl + 'to \n' + shortURL + '\''
system(cmd)
print shortURL
Show details Hide details

Change log

r16 by chanj87 on Sep 03, 2009   Diff
Significant updates to burl.py. Better
error handling. Handles URLs with symbols
better.
Go to: 
Sign in to write a code review

Older revisions

r12 by chanj87 on May 12, 2009   Diff
Updated usage description to better
fit what this tool really does now.
r11 by chanj87 on May 12, 2009   Diff
Fixed it to work with URLs containing
&, =, #, and ?'s
r10 by chanj87 on Apr 26, 2009   Diff
Removed useless print statement.
Changed my api key so the one in this
file no longer works.
All revisions of this file

File info

Size: 2560 bytes, 92 lines

File properties

svn:executable
*
Hosted by Google Code