Raise Exceptions on bad Download Responses (#1109)

* expose showstatus to downloadDatabases and downloadLogs

* throw errors for bad download responses
This commit is contained in:
meisnate12 2023-05-24 14:37:37 -04:00 committed by GitHub
parent 981f1624ac
commit abc3464de3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View file

@ -549,26 +549,28 @@ class PlexServer(PlexObject):
"""
return PlayQueue.create(self, item, **kwargs)
def downloadDatabases(self, savepath=None, unpack=False):
def downloadDatabases(self, savepath=None, unpack=False, showstatus=False):
""" Download databases.
Parameters:
savepath (str): Defaults to current working dir.
unpack (bool): Unpack the zip file.
showstatus(bool): Display a progressbar.
"""
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
def downloadLogs(self, savepath=None, unpack=False):
def downloadLogs(self, savepath=None, unpack=False, showstatus=False):
""" Download server logs.
Parameters:
savepath (str): Defaults to current working dir.
unpack (bool): Unpack the zip file.
showstatus(bool): Display a progressbar.
"""
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
def butlerTasks(self):

View file

@ -15,9 +15,10 @@ from datetime import datetime
from getpass import getpass
from threading import Event, Thread
from urllib.parse import quote
from requests.status_codes import _codes as codes
import requests
from plexapi.exceptions import BadRequest, NotFound
from plexapi.exceptions import BadRequest, NotFound, Unauthorized
try:
from tqdm import tqdm
@ -391,12 +392,12 @@ def downloadSessionImages(server, filename=None, height=150, width=150,
prettyname = media._prettyfilename()
filename = f'session_transcode_{media.usernames[0]}_{prettyname}_{int(time.time())}'
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}
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):
""" Helper to download a thumb, videofile or other media item. Returns the local
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()
headers = {'X-Plex-Token': token}
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
savepath = savepath or os.getcwd()
os.makedirs(savepath, exist_ok=True)