Skip to content

Commit

Permalink
Finetuned the AuthHook, added _custom_auth_check()
Browse files Browse the repository at this point in the history
See GenericProvider for the self.auth_hooks options, that I came up wiht now. These can be extended if needed.

For Ex:
self.auth_hooks = {'min_amount_cookies': 4, 'check_cookie_expired': True}

If you want to do something really fancy, you can overwrite the _custom_auth_check(self) method in the provider class.
  • Loading branch information
p0psicles committed Mar 3, 2016
1 parent 0a2af85 commit 527d1b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
20 changes: 9 additions & 11 deletions sickbeard/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1433,28 +1433,26 @@ def _setUpSession(session, headers):

class RequestAuth(requests.auth.AuthBase):
"""Attaches Provider Authentication to the given Requests object"""
def __init__(self, provider, **auth_settings):
def __init__(self, provider):
# setup any auth-related data here
self.provider = provider
self.login_required = auth_settings.get('login_required')
self.min_amount_cookies = auth_settings.get('min_amount_cookies')
self.username = auth_settings.get('username')
self.password = auth_settings.get('password')
self.check_cookie_expired = auth_settings.get('check_cookie_expired')

def __call__(self, r):
# modify and return the request
if self.min_amount_cookies:
if len(self.session.cookies) < self.min_amount_cookies:
if self.provider.auth_hooks.get('min_amount_cookies'):
if len(self.provider.session.cookies) < self.provider.auth_hooks.get('min_amount_cookies'):
self.provider.login()

if self.login_required:
if self.provider.auth_hooks.get('login_required'):
self.provider.login()

if self.check_cookie_expired:
if all([not cookie.is_expired() for cookie in r.cookies]):
if self.provider.auth_hooks.get('check_cookie_expired'):
if all([not cookie.is_expired() for cookie in self.provider.session.cookies]):
self.provider.login()

if self.provider.auth_hooks.get('custom_auth_check'):
self.provider.auth_hooks.get('custom_auth_check')()

return r

def getURL(url, post_data=None, params=None, headers=None, # pylint:disable=too-many-arguments, too-many-return-statements, too-many-branches
Expand Down
1 change: 1 addition & 0 deletions sickbeard/providers/tvchaosuk.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(self):
'index': self.url + 'index.php',
'search': self.url + 'browse.php'
}
self.auth_hooks = {'min_amount_cookies': 3, 'check_cookie_expired': True}

# Proper Strings

Expand Down
24 changes: 18 additions & 6 deletions sickrage/providers/GenericProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from sickbeard.classes import Proper, SearchResult
from sickbeard.common import MULTI_EP_RESULT, Quality, SEASON_RESULT, UA_POOL
from sickbeard.db import DBConnection
from sickbeard.helpers import download_file, getURL, remove_file_failed
from sickbeard.helpers import download_file, getURL, remove_file_failed, RequestAuth
from sickbeard.name_parser.parser import InvalidNameException, InvalidShowException, NameParser
from sickbeard.show_name_helpers import allPossibleShowNames
from sickbeard.tvcache import TVCache
Expand Down Expand Up @@ -70,6 +70,11 @@ def __init__(self, name):
self.supports_backlog = True
self.url = ''
self.urls = {}
self.auth_hooks = {'login_required': None,
'min_amount_cookies': None,
'check_cookie_expired': None,
'username': None, 'password': None,
'custom_auth_check': None}

shuffle(self.bt_cache_urls)

Expand Down Expand Up @@ -352,10 +357,17 @@ def get_result(self, episodes):

return result

def get_auth_hook(self, **kwargs):
from sickbeard.helpers import RequestAuth
args = {'login_required': True}
return RequestAuth(self, **args)
def _custom_auth_check(self):
""" Use this function if you'd like to have some control over the authentication checks
before searching the provider or snatching a result. The _custom_auth_check is hooked through _get_auth_hook()
to the helpers.getURL() function.
"""
return

def _get_auth_hook(self, **kwargs):
""" This hooks the RequestAuth object to the getURL function """
self.auth_hooks['custom_auth_check'] = self._custom_auth_check
return RequestAuth(self)

@staticmethod
def get_url_hook(response, **kwargs):
Expand All @@ -369,7 +381,7 @@ def get_url(self, url, post_data=None, params=None, timeout=30, json=False, need
if kwargs.pop('echo', True):
kwargs['hooks'] = {'response': self.get_url_hook}

return getURL(url, post_data, params, self.headers, timeout, self.session, json, need_bytes, self.get_auth_hook(), **kwargs)
return getURL(url, post_data, params, self.headers, timeout, self.session, json, need_bytes, self._get_auth_hook(), **kwargs)

def image_name(self):
return self.get_id() + '.png'
Expand Down

1 comment on commit 527d1b7

@p0psicles
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.