Skip to content

Commit f68e38d

Browse files
author
Claudio
committed
adding proxy support
1 parent c711084 commit f68e38d

File tree

4 files changed

+468
-6
lines changed

4 files changed

+468
-6
lines changed

bitly.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import urllib
2222
import httplib2
2323
import socket
24+
import socks
2425
from urllib2 import URLError
2526
import urlparse
2627
import string
@@ -59,17 +60,20 @@ class Api(object):
5960
}
6061

6162
domain = 'bit.ly'
63+
proxy_info = None
6264

6365
def get_api_domain(self):
6466
return self.ALLOWED_API_DOMAINS[self.domain]
6567

6668
""" API class for bit.ly """
67-
def __init__(self, login, apikey, domain=None):
69+
def __init__(self, login, apikey, domain=None, proxy_info=None):
6870
self.login = login
6971
self.apikey = apikey
7072
self._urllib = httplib2
7173
if domain is not None:
7274
self.domain = domain
75+
if proxy_info:
76+
self.proxy_info = proxy_info
7377

7478
def shorten(self, longURLs, params={}):
7579
"""
@@ -84,6 +88,7 @@ def shorten(self, longURLs, params={}):
8488

8589
request = self._getURL("shorten", longURLs, params)
8690
result = self._fetchUrl(request)
91+
8792
json = simplejson.loads(result)
8893
Api._CheckForError(json)
8994

@@ -176,16 +181,30 @@ def _fetchUrl(self, url):
176181
# Open and return the URL
177182
try:
178183
if self._urllib is httplib2:
179-
http = httplib2.Http(timeout=TIMEOUT)
184+
params = {
185+
'timeout': TIMEOUT
186+
}
187+
188+
if self.proxy_info is not None:
189+
try:
190+
host = self.proxy_info['HOST']
191+
port = self.proxy_info['PORT']
192+
except KeyError, err:
193+
raise ValueError('You should supply a value for HOST and PORT when working with proxies. %s' % err)
194+
195+
params.update(
196+
{'proxy_info': httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, host, port)}
197+
)
198+
http = httplib2.Http(**params)
180199
resp, content = http.request(url)
181200
url_data = content
182201
else:
183202
url_data = self._urllib.urlopen(url=url).read()
184203

185204
except (URLError, socket.error), err:
186-
# nasty bit of hack, i know but unfortunatly urllib2 has no smart way of telling me
187-
# that it was an timeout error
188-
if hasattr(err,'reason') and err.reason == 'urlopen error timed out' or unicode(err) == u'timed out':
205+
# nasty bit of hack, i know but unfortunatly neither urllib2 ou httplib2 has a smart way of telling me
206+
# that it was a timeout error
207+
if hasattr(err, 'reason') and err.reason == 'urlopen error timed out' or unicode(err) == u'timed out':
189208
raise BitlyTimeoutError('The url %s has timed out' % url)
190209
raise err
191210

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
author=u'Yoav Aviram ',
1313
author_email='[email protected]',
1414
url='http://github.com/jcfigueiredo/python-bitly',
15-
py_modules=['bitly'],
15+
py_modules=['bitly','socks'],
1616
install_requires=[
1717
'simplejson',
1818
'httplib2'

0 commit comments

Comments
 (0)