forked from Flexget/Flexget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: http://svn.flexget.com/trunk@2747 3942dd89-8c5d-46d7-aeed-044bccf3e60c
- Loading branch information
1 parent
82360b6
commit 977ea3c
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
import logging | ||
from flexget import plugin | ||
from flexget import validator | ||
|
||
log = logging.getLogger('proxy') | ||
|
||
PROTOCOLS = ['http', 'https', 'ftp'] | ||
|
||
|
||
class Proxy(object): | ||
"""Adds a proxy to the requests session.""" | ||
|
||
def validator(self): | ||
root = validator.factory() | ||
# Accept one proxy for everything | ||
root.accept('url') | ||
# Accept a dict mapping protocol to proxy | ||
advanced = root.accept('dict') | ||
for prot in PROTOCOLS: | ||
advanced.accept('url', key=prot) | ||
return root | ||
|
||
@plugin.priority(255) | ||
def on_feed_start(self, feed, config): | ||
if not config: | ||
# If no configuration is provided, see if there are any proxy env variables | ||
proxies = {} | ||
for prot in PROTOCOLS: | ||
if os.environ.get(prot + '_proxy'): | ||
proxies[prot] = os.environ[prot + '_proxy'] | ||
if not proxies: | ||
# If there were no environment variables set, do nothing | ||
return | ||
elif isinstance(config, dict): | ||
proxies = config | ||
else: | ||
# Map all protocols to the configured proxy | ||
proxies = dict((prot, config) for prot in PROTOCOLS) | ||
log.verbose('Setting proxy to %s' % proxies) | ||
feed.requests.proxies = proxies | ||
|
||
|
||
plugin.register_plugin(Proxy, 'proxy', builtin=True, api_ver=2) |