-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.py
114 lines (93 loc) · 2.96 KB
/
common.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
## Shorty
## Copyright 2009 Joshua Roesslein
## See LICENSE
## Common code used in all compiled versions of shorty.
class ShortyError(Exception):
def __init__(self, reason):
self.reason = str(reason)
def __str__(self):
return repr(self.reason)
"""Do a http request"""
def request(url, parameters=None, username_pass=None, post_data=None, headers={}):
# build url + parameters
if parameters:
url_params = '%s?%s' % (url, urlencode(parameters))
else:
url_params = url
# if username and pass supplied, build basic auth header
if username_pass:
headers['Authorization'] = 'Basic %s' % base64.b64encode('%s:%s' % username_pass)
# send request
try:
req = Request(url_params, headers=headers)
if post_data:
req.add_data(post_data)
return urlopen(req)
except URLError, e:
raise ShortyError(e)
def get_redirect(url):
class StopRedirectHandler(HTTPRedirectHandler):
def http_error_301(self, req, fp, code, smg, headers):
return None
def http_error_302(self, req, fp, code, smg, headers):
return None
o = build_opener(StopRedirectHandler())
try:
o.open(url)
except HTTPError, e:
if e.code == 301 or e.code == 302:
return e.headers['Location']
else:
raise ShortyError(e)
except URLError, e:
raise ShortyError(e)
return None
"""Base interface that all services implement."""
class Service(object):
tested = False
def _test(self):
self.tested = True
# first shrink an url
try:
turl = self.shrink('http://test.com')
except ShortyError, e:
raise ShortyError('@shrink ' + e.reason)
# second expand url and verify
try:
if self.expand(turl) == 'http://test.com':
return True
elif self.expand(turl) == 'http://test.com/':
return True
else:
return False
except ShortyError, e:
raise ShortyError('@expand ' + e.reason)
def shrink(self, bigurl):
"""Take a big url and make it smaller"""
return None
def expand(self, tinyurl):
"""Take a tiny url and make it bigger"""
return get_redirect(tinyurl)
"""
Shrink the given url for the specified service domain.
Returns the tiny url OR None if service not supported.
"""
def shrink(service_domain, bigurl, *args, **kargs):
s = services.get(service_domain)
if not s:
return None
return s.shrink(bigurl, *args, **kargs)
"""
Expand tiny url into its full url.
Returns long url if successful or None if failure.
"""
def expand(tinyurl):
turl = urlparse(tinyurl)
domain = turl.netloc.lower()
if domain.startswith('www.'):
# strip off www. if present
domain = domain[4:]
s = services.get(domain)
if not s:
return get_redirect(tinyurl)
return s.expand(tinyurl)