mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-22 06:13:19 -07:00
Merge pull request #1929 from clinton-hall/unprocessed
Convert `ProcessResult` to `NamedTuple` and add `url` property.
This commit is contained in:
commit
10e6576ac2
3 changed files with 31 additions and 34 deletions
|
@ -5,28 +5,27 @@ import requests
|
|||
from core import logger
|
||||
|
||||
|
||||
class ProcessResult:
|
||||
def __init__(self, message: str, status_code: int):
|
||||
self.message = message
|
||||
self.status_code = status_code
|
||||
|
||||
def __iter__(self) -> typing.Tuple[int, str]:
|
||||
return self.status_code, self.message
|
||||
class ProcessResult(typing.NamedTuple):
|
||||
status_code: int
|
||||
message: str
|
||||
|
||||
def __bool__(self) -> bool:
|
||||
return not bool(self.status_code)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return 'Processing {0}: {1}'.format(
|
||||
'succeeded' if bool(self) else 'failed',
|
||||
self.message,
|
||||
)
|
||||
status = 'succeeded' if bool(self) else 'failed'
|
||||
return f'Processing {self.message}: {status}'
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return '<ProcessResult {0}: {1}>'.format(
|
||||
self.status_code,
|
||||
self.message,
|
||||
)
|
||||
return f'<ProcessResult {self.status_code}: {self.message}>'
|
||||
|
||||
@classmethod
|
||||
def failure(cls, message: str = 'Failed'):
|
||||
return cls(1, message)
|
||||
|
||||
@classmethod
|
||||
def success(cls, message: str = 'Success'):
|
||||
return cls(0, message)
|
||||
|
||||
|
||||
def command_complete(url, params, headers, section):
|
||||
|
|
|
@ -11,7 +11,8 @@ import requests
|
|||
class PyMedusa(SickBeard):
|
||||
"""PyMedusa class."""
|
||||
|
||||
def _create_url(self):
|
||||
@property
|
||||
def url(self):
|
||||
route = f'{self.sb_init.web_root}/home/postprocess/processEpisode'
|
||||
return core.utils.common.create_url(
|
||||
self.sb_init.protocol,
|
||||
|
@ -24,7 +25,8 @@ class PyMedusa(SickBeard):
|
|||
class PyMedusaApiV1(SickBeard):
|
||||
"""PyMedusa apiv1 class."""
|
||||
|
||||
def _create_url(self):
|
||||
@property
|
||||
def url(self) -> str:
|
||||
route = f'{self.sb_init.web_root}/api/{self.sb_init.apikey}/'
|
||||
return core.utils.common.create_url(
|
||||
self.sb_init.protocol,
|
||||
|
@ -35,13 +37,11 @@ class PyMedusaApiV1(SickBeard):
|
|||
|
||||
def api_call(self):
|
||||
self._process_fork_prarams()
|
||||
url = self._create_url()
|
||||
|
||||
logger.debug('Opening URL: {0} with params: {1}'.format(url, self.sb_init.fork_params), self.sb_init.section)
|
||||
logger.debug(f'Opening URL: {self.url} with params: {self.sb_init.fork_params}', self.sb_init.section)
|
||||
try:
|
||||
response = self.session.get(url, auth=(self.sb_init.username, self.sb_init.password), params=self.sb_init.fork_params, stream=True, verify=False, timeout=(30, 1800))
|
||||
response = self.session.get(self.url, auth=(self.sb_init.username, self.sb_init.password), params=self.sb_init.fork_params, stream=True, verify=False, timeout=(30, 1800))
|
||||
except requests.ConnectionError:
|
||||
logger.error('Unable to open URL: {0}'.format(url), self.sb_init.section)
|
||||
logger.error(f'Unable to open URL: {self.url}', self.sb_init.section)
|
||||
return ProcessResult(
|
||||
message='{0}: Failed to post-process - Unable to connect to {0}'.format(self.sb_init.section),
|
||||
status_code=1,
|
||||
|
@ -75,7 +75,8 @@ class PyMedusaApiV2(SickBeard):
|
|||
if not sb_init.apikey:
|
||||
raise Exception('For the section SickBeard `fork = medusa-apiv2` you also need to configure an `apikey`')
|
||||
|
||||
def _create_url(self):
|
||||
@property
|
||||
def url(self):
|
||||
route = f'{self.sb_init.web_root}/api/v2/postprocess'
|
||||
return core.utils.common.create_url(
|
||||
self.sb_init.protocol,
|
||||
|
@ -101,9 +102,7 @@ class PyMedusaApiV2(SickBeard):
|
|||
|
||||
def api_call(self):
|
||||
self._process_fork_prarams()
|
||||
url = self._create_url()
|
||||
|
||||
logger.debug('Opening URL: {0}'.format(url), self.sb_init.section)
|
||||
logger.debug(f'Opening URL: {self.url}', self.sb_init.section)
|
||||
payload = self.sb_init.fork_params
|
||||
payload['resource'] = self.sb_init.fork_params['nzbName']
|
||||
del payload['nzbName']
|
||||
|
@ -116,7 +115,7 @@ class PyMedusaApiV2(SickBeard):
|
|||
|
||||
# Send postprocess request
|
||||
try:
|
||||
response = self.session.post(url, json=payload, verify=False, timeout=(30, 1800))
|
||||
response = self.session.post(self.url, json=payload, verify=False, timeout=(30, 1800))
|
||||
except requests.ConnectionError:
|
||||
logger.error('Unable to send postprocess request', self.sb_init.section)
|
||||
return ProcessResult(
|
||||
|
@ -140,7 +139,7 @@ class PyMedusaApiV2(SickBeard):
|
|||
wait_for = int(self.sb_init.config.get('wait_for', 2))
|
||||
n = 0
|
||||
response = {}
|
||||
url = '{0}/{1}'.format(url, queueitem_identifier)
|
||||
url = f'{self.url}/{queueitem_identifier}'
|
||||
while n < 12: # set up wait_for minutes to see if command completes..
|
||||
time.sleep(5 * wait_for)
|
||||
response = self._get_identifier_status(url)
|
||||
|
|
|
@ -346,7 +346,8 @@ class SickBeard:
|
|||
if client_agent == core.TORRENT_CLIENT_AGENT and core.USE_LINK == 'move-sym':
|
||||
self.process_method = 'symlink'
|
||||
|
||||
def _create_url(self) -> str:
|
||||
@property
|
||||
def url(self) -> str:
|
||||
if self.sb_init.apikey:
|
||||
route = f'{self.sb_init.web_root}/api/{self.sb_init.apikey}/'
|
||||
else:
|
||||
|
@ -434,9 +435,7 @@ class SickBeard:
|
|||
def api_call(self):
|
||||
"""Perform a base sickbeard api call."""
|
||||
self._process_fork_prarams()
|
||||
url = self._create_url()
|
||||
|
||||
logger.debug('Opening URL: {0} with params: {1}'.format(url, self.sb_init.fork_params), self.sb_init.section)
|
||||
logger.debug(f'Opening URL: {self.url} with params: {self.sb_init.fork_params}', self.sb_init.section)
|
||||
try:
|
||||
if not self.sb_init.apikey and self.sb_init.username and self.sb_init.password:
|
||||
# If not using the api, we need to login using user/pass first.
|
||||
|
@ -452,9 +451,9 @@ class SickBeard:
|
|||
if r.status_code in [401, 403] and r.cookies.get('_xsrf'):
|
||||
login_params['_xsrf'] = r.cookies.get('_xsrf')
|
||||
self.session.post(login, data=login_params, stream=True, verify=False, timeout=(30, 60))
|
||||
response = self.session.get(url, auth=(self.sb_init.username, self.sb_init.password), params=self.sb_init.fork_params, stream=True, verify=False, timeout=(30, 1800))
|
||||
response = self.session.get(self.url, auth=(self.sb_init.username, self.sb_init.password), params=self.sb_init.fork_params, stream=True, verify=False, timeout=(30, 1800))
|
||||
except requests.ConnectionError:
|
||||
logger.error('Unable to open URL: {0}'.format(url), self.sb_init.section)
|
||||
logger.error(f'Unable to open URL: {self.url}', self.sb_init.section)
|
||||
return ProcessResult(
|
||||
message='{0}: Failed to post-process - Unable to connect to {0}'.format(self.sb_init.section),
|
||||
status_code=1,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue