/
bitly.py
139 lines (122 loc) · 4.4 KB
/
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
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
136
137
138
139
import simplejson, urllib, string
API_KEY = ''
API_LOGIN = ''
API_VERSION = '2.0.1'
API_BASE = 'http://api.bit.ly'
class BitlyAPIError(Exception):
pass
def shorten(longUrl, **kwargs):
"""
Given a longUrl, returns a shorter one using the optionally provided keyword if possible.
If the keyword is not available a standard hashed URL will be returned.
Parameters:
longUrl (required): URL to shorten
ie: shorten('http://cnn.com')
keyword (optional): preferred keyword
ie: shorten('http://cnn.com', keyword='cnn')
"""
kwargs.update({
'version': API_VERSION,
'format': 'json',
'login': API_LOGIN,
'apiKey': API_KEY,
'longUrl': longUrl,
})
url = API_BASE + '/shorten?' + urllib.urlencode(kwargs)
result = simplejson.load(urllib.urlopen(url))
if 'ERROR' in result:
raise BitlyAPIError, result['errorMessage']
if result['results'][longUrl]['shortKeywordUrl'] == "":
return result['results'][longUrl]['shortUrl']
else:
return result['results'][longUrl]['shortKeywordUrl']
def expand(**kwargs):
"""
Given a bit.ly url or hash(es), return the long source url(s).
Parameters:
shortUrl: a single URL to expand
ie: expand(shortUrl='http://bit.ly/QJhM')
hash: one or more URL hashes to expand
ie: expand(hash='QJhM,3el7')
"""
kwargs.update({
'version': API_VERSION,
'format': 'json',
'login': API_LOGIN,
'apiKey':API_KEY,
})
if not (kwargs.has_key('shortUrl') or kwargs.has_key('hash')):
raise BitlyAPIError, "You must provide either a shortUrl or hash."
if (kwargs.has_key('shortUrl') and kwargs.has_key('hash')):
raise BitlyAPIError, "You must provide either a shortUrl or hash, not both."
url = API_BASE + '/expand?' + urllib.urlencode(kwargs)
result = simplejson.load(urllib.urlopen(url))
if 'ERROR' in result:
raise BitlyAPIError, result['errorMessage']
if kwargs.has_key('hash'):
return result['results']
else:
return result['results'][string.split(kwargs['shortUrl'], '/')[-1]]['longUrl']
def info(**kwargs):
"""
Given a bit.ly url or hash, return information about that page.
Parameters:
shortUrl: a single URL to get info for
ie: expand(shortUrl='http://bit.ly/QJhM')
hash: one or more URL hashes to get info for
ie: expand(hash='QJhM,3el7')
keys (optional): one or more keys to limit attributes about each document.
ie: expand(hash='QJhM', keys='htmlTitle,users')
"""
kwargs.update({
'version': API_VERSION,
'format': 'json',
'login': API_LOGIN,
'apiKey': API_KEY,
})
if not (kwargs.has_key('shortUrl') or kwargs.has_key('hash')):
raise BitlyAPIError, "You must provide either a shortUrl or hash."
if (kwargs.has_key('shortUrl') and kwargs.has_key('hash')):
raise BitlyAPIError, "You must provide either a shortUrl or hash, not both."
url = API_BASE + '/info?' + urllib.urlencode(kwargs)
result = simplejson.load(urllib.urlopen(url))
if kwargs.has_key('hash'):
return result['results']
else:
return result['results'][string.split(kwargs['shortUrl'], '/')[-1]]
def stats(**kwargs):
"""
Given a bit.ly url or hash, return traffic and referrer data.
Parameters:
shortUrl: a single URL to get stats for
ie: expand(shortUrl='http://bit.ly/QJhM')
hash: one or more URL hashes to get stats for
ie: expand(hash='QJhM,3el7')
"""
kwargs.update({
'version': API_VERSION,
'format': 'json',
'login': API_LOGIN,
'apiKey': API_KEY,
})
if not (kwargs.has_key('shortUrl') or kwargs.has_key('hash')):
raise BitlyAPIError, "You must provide either a shortUrl or hash."
if (kwargs.has_key('shortUrl') and kwargs.has_key('hash')):
raise BitlyAPIError, "You must provide either a shortUrl or hash, not both."
url = API_BASE + '/stats?' + urllib.urlencode(kwargs)
result = simplejson.load(urllib.urlopen(url))
if kwargs.has_key('hash'):
return result['results']
else:
return result['results'][string.split(kwargs['shortUrl'], '/')[-1]]
def errors(**kwargs):
""" Get a list of API error codes. """
kwargs.update({
'version': API_VERSION,
'format': 'json',
'login': API_LOGIN,
'apiKey': API_KEY,
})
url = API_BASE + '/errors?' +urllib.urlencode(kwargs)
result = simplejson.load(urllib.urlopen(url))
return result['results']