mirror of
https://github.com/pkkid/python-plexapi
synced 2024-11-10 06:04:15 +00:00
Raise Exceptions on bad Download Responses (#1109)
* expose showstatus to downloadDatabases and downloadLogs * throw errors for bad download responses
This commit is contained in:
parent
981f1624ac
commit
abc3464de3
2 changed files with 21 additions and 7 deletions
|
@ -549,26 +549,28 @@ class PlexServer(PlexObject):
|
||||||
"""
|
"""
|
||||||
return PlayQueue.create(self, item, **kwargs)
|
return PlayQueue.create(self, item, **kwargs)
|
||||||
|
|
||||||
def downloadDatabases(self, savepath=None, unpack=False):
|
def downloadDatabases(self, savepath=None, unpack=False, showstatus=False):
|
||||||
""" Download databases.
|
""" Download databases.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
savepath (str): Defaults to current working dir.
|
savepath (str): Defaults to current working dir.
|
||||||
unpack (bool): Unpack the zip file.
|
unpack (bool): Unpack the zip file.
|
||||||
|
showstatus(bool): Display a progressbar.
|
||||||
"""
|
"""
|
||||||
url = self.url('/diagnostics/databases')
|
url = self.url('/diagnostics/databases')
|
||||||
filepath = utils.download(url, self._token, None, savepath, self._session, unpack=unpack)
|
filepath = utils.download(url, self._token, None, savepath, self._session, unpack=unpack, showstatus=showstatus)
|
||||||
return filepath
|
return filepath
|
||||||
|
|
||||||
def downloadLogs(self, savepath=None, unpack=False):
|
def downloadLogs(self, savepath=None, unpack=False, showstatus=False):
|
||||||
""" Download server logs.
|
""" Download server logs.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
savepath (str): Defaults to current working dir.
|
savepath (str): Defaults to current working dir.
|
||||||
unpack (bool): Unpack the zip file.
|
unpack (bool): Unpack the zip file.
|
||||||
|
showstatus(bool): Display a progressbar.
|
||||||
"""
|
"""
|
||||||
url = self.url('/diagnostics/logs')
|
url = self.url('/diagnostics/logs')
|
||||||
filepath = utils.download(url, self._token, None, savepath, self._session, unpack=unpack)
|
filepath = utils.download(url, self._token, None, savepath, self._session, unpack=unpack, showstatus=showstatus)
|
||||||
return filepath
|
return filepath
|
||||||
|
|
||||||
def butlerTasks(self):
|
def butlerTasks(self):
|
||||||
|
|
|
@ -15,9 +15,10 @@ from datetime import datetime
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
from threading import Event, Thread
|
from threading import Event, Thread
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
from requests.status_codes import _codes as codes
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from plexapi.exceptions import BadRequest, NotFound
|
from plexapi.exceptions import BadRequest, NotFound, Unauthorized
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
@ -391,12 +392,12 @@ def downloadSessionImages(server, filename=None, height=150, width=150,
|
||||||
prettyname = media._prettyfilename()
|
prettyname = media._prettyfilename()
|
||||||
filename = f'session_transcode_{media.usernames[0]}_{prettyname}_{int(time.time())}'
|
filename = f'session_transcode_{media.usernames[0]}_{prettyname}_{int(time.time())}'
|
||||||
url = server.transcodeImage(url, height, width, opacity, saturation)
|
url = server.transcodeImage(url, height, width, opacity, saturation)
|
||||||
filepath = download(url, filename=filename)
|
filepath = download(url, server._token, filename=filename)
|
||||||
info['username'] = {'filepath': filepath, 'url': url}
|
info['username'] = {'filepath': filepath, 'url': url}
|
||||||
return info
|
return info
|
||||||
|
|
||||||
|
|
||||||
def download(url, token, filename=None, savepath=None, session=None, chunksize=4024,
|
def download(url, token, filename=None, savepath=None, session=None, chunksize=4024, # noqa: C901
|
||||||
unpack=False, mocked=False, showstatus=False):
|
unpack=False, mocked=False, showstatus=False):
|
||||||
""" Helper to download a thumb, videofile or other media item. Returns the local
|
""" Helper to download a thumb, videofile or other media item. Returns the local
|
||||||
path to the downloaded file.
|
path to the downloaded file.
|
||||||
|
@ -419,6 +420,17 @@ def download(url, token, filename=None, savepath=None, session=None, chunksize=4
|
||||||
session = session or requests.Session()
|
session = session or requests.Session()
|
||||||
headers = {'X-Plex-Token': token}
|
headers = {'X-Plex-Token': token}
|
||||||
response = session.get(url, headers=headers, stream=True)
|
response = session.get(url, headers=headers, stream=True)
|
||||||
|
if response.status_code not in (200, 201, 204):
|
||||||
|
codename = codes.get(response.status_code)[0]
|
||||||
|
errtext = response.text.replace('\n', ' ')
|
||||||
|
message = f'({response.status_code}) {codename}; {response.url} {errtext}'
|
||||||
|
if response.status_code == 401:
|
||||||
|
raise Unauthorized(message)
|
||||||
|
elif response.status_code == 404:
|
||||||
|
raise NotFound(message)
|
||||||
|
else:
|
||||||
|
raise BadRequest(message)
|
||||||
|
|
||||||
# make sure the savepath directory exists
|
# make sure the savepath directory exists
|
||||||
savepath = savepath or os.getcwd()
|
savepath = savepath or os.getcwd()
|
||||||
os.makedirs(savepath, exist_ok=True)
|
os.makedirs(savepath, exist_ok=True)
|
||||||
|
|
Loading…
Reference in a new issue