-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitly.py
29 lines (26 loc) · 979 Bytes
/
bitly.py
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
import urllib
class Bitly(object):
'''
class that manages the bit.ly services
'''
def __init__(self, login_user, api_key):
self.login_user = login_user
self.api_key = api_key
def shorten_url(self, long_url):
'''
shortens the given url
'''
try:
longUrl = urllib.urlencode(dict(longUrl=long_url))
login = urllib.urlencode(dict(login=self.login_user))
apiKey = urllib.urlencode(dict(apiKey=self.api_key))
encodedurl = "http://api.bit.ly/shorten?version=2.0.1&%s&%s&%s" % \
(longUrl, login, apiKey)
request = urllib.urlopen(encodedurl)
responde = request.read()
request.close()
responde_dict = eval(responde)
short_url = responde_dict["results"][long_url]["shortUrl"]
return short_url
except IOError, e:
raise "url shortening failed (urllib error)"