Modified http util to support additional headers

Inverted some header logic

Removed user_agent and referer from http, in favor of headers dict

http: formatting

http: Fixed missing part in conditional
This commit is contained in:
Parker Smith 2014-01-19 01:49:25 -05:00 committed by Parker Smith
parent cb8c437772
commit d878779d50
2 changed files with 10 additions and 12 deletions

View File

@ -9,7 +9,7 @@ def urban(inp):
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
url = 'http://www.urbandictionary.com/iphone/search/define'
page = http.get_json(url, term=inp, referer="http://m.urbandictionary.com")
page = http.get_json(url, term=inp, headers={'Referer': 'http://m.urbandictionary.com'})
defs = page['list']
if page['result_type'] == 'no_results':

View File

@ -42,15 +42,11 @@ def get_json(*args, **kwargs):
return json.loads(get(*args, **kwargs))
def open(url, query_params=None, user_agent=None, referer=None, post_data=None,
get_method=None, cookies=False, oauth=False, oauth_keys=None, **kwargs):
def open(url, query_params=None, post_data=None,
get_method=None, cookies=False, oauth=False, oauth_keys=None, headers=None, **kwargs):
if query_params is None:
query_params = {}
if user_agent is None:
user_agent = ua_skybot
query_params.update(kwargs)
url = prepare_url(url, query_params)
@ -60,10 +56,12 @@ def open(url, query_params=None, user_agent=None, referer=None, post_data=None,
if get_method is not None:
request.get_method = lambda: get_method
request.add_header('User-Agent', user_agent)
if headers is not None:
for header_key, header_value in headers.iteritems():
request.add_header(header_key, header_value)
if referer is not None:
request.add_header('Referer', referer)
if 'User-Agent' not in request.headers:
request.add_header('User-Agent', ua_skybot)
if oauth:
nonce = oauth_nonce()