Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion wikipedia/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
RATE_LIMIT_MIN_WAIT = None
RATE_LIMIT_LAST_CALL = None
USER_AGENT = 'wikipedia (https://github.com/goldsmith/Wikipedia/)'
PROXY_CONF = {}


def set_lang(prefix):
Expand Down Expand Up @@ -47,6 +48,18 @@ def set_user_agent(user_agent_string):
USER_AGENT = user_agent_string


def set_proxy(proxy_dict):
'''
Set the proxy config to be used for all requests.

Arguments:

* proxy_dict - (dict) a dict specifying the proxy config
'''
global PROXY_CONF
PROXY_CONF = proxy_dict


def set_rate_limiting(rate_limit, min_wait=timedelta(milliseconds=50)):
'''
Enable or disable rate limiting on requests to the Mediawiki servers.
Expand Down Expand Up @@ -716,6 +729,7 @@ def _wiki_request(params):
'''
global RATE_LIMIT_LAST_CALL
global USER_AGENT
global PROXY_CONF

params['format'] = 'json'
if not 'action' in params:
Expand All @@ -734,7 +748,7 @@ def _wiki_request(params):
wait_time = (RATE_LIMIT_LAST_CALL + RATE_LIMIT_MIN_WAIT) - datetime.now()
time.sleep(int(wait_time.total_seconds()))

r = requests.get(API_URL, params=params, headers=headers)
r = requests.get(API_URL, params=params, headers=headers, proxies=PROXY_CONF)

if RATE_LIMIT:
RATE_LIMIT_LAST_CALL = datetime.now()
Expand Down