Skip to content

Commit

Permalink
[common] make get_location accept headers
Browse files Browse the repository at this point in the history
  • Loading branch information
soimort committed Jul 1, 2018
1 parent 37e2a79 commit 3e89279
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/you_get/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,16 @@ def get_decoded_html(url, faker=False):
return data


def get_location(url):
def get_location(url, headers=None, get_method='HEAD'):
logging.debug('get_location: %s' % url)

response = request.urlopen(url)
# urllib will follow redirections and it's too much code to tell urllib
# not to do that
return response.geturl()
if headers:
req = request.Request(url, headers=headers)
else:
req = request.Request(url)
req.get_method = lambda: get_method
res = urlopen_with_retry(req)
return res.geturl()


def urlopen_with_retry(*args, **kwargs):
Expand Down Expand Up @@ -1594,7 +1597,10 @@ def url_to_module(url):
url
)
else:
location = get_location(url)
try:
location = get_location(url) # t.co isn't happy with fake_headers
except:
location = get_location(url, headers=fake_headers)

if location and location != url and not location.startswith('/'):
return url_to_module(location)
Expand Down

0 comments on commit 3e89279

Please sign in to comment.