add download log/db.

This commit is contained in:
Hellowlol 2017-02-26 21:01:54 +01:00
parent d41bf0fb89
commit baeedcebbf
3 changed files with 50 additions and 6 deletions

View file

@ -326,6 +326,16 @@ class PlexServer(PlexObject):
return '%s%s%sX-Plex-Token=%s' % (self._baseurl, key, delim, self._token) return '%s%s%sX-Plex-Token=%s' % (self._baseurl, key, delim, self._token)
return '%s%s' % (self._baseurl, key) return '%s%s' % (self._baseurl, key)
def downloadLog(self, savepath=None, unpack=False):
url = self.url('/diagnostics/databases')
fp = utils.download(url, filename=None, savepath=savepath, unpack=unpack)
return fp
def downloadDB(self, savepath=None, unpack=False):
url = self.url('/diagnostics/logs')
fp = utils.download(url, filename=None, savepath=savepath, unpack=unpack)
return fp
class Account(PlexObject): class Account(PlexObject):
""" Contains the locally cached MyPlex account information. The properties provided don't """ Contains the locally cached MyPlex account information. The properties provided don't

View file

@ -1,12 +1,17 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
import os import os
import requests import re
import time import time
import zipfile
from datetime import datetime from datetime import datetime
from threading import Thread
import requests
from plexapi.compat import quote, string_type from plexapi.compat import quote, string_type
from plexapi.exceptions import NotFound from plexapi.exceptions import NotFound
from threading import Thread
# Search Types - Plex uses these to filter specific media types when searching. # Search Types - Plex uses these to filter specific media types when searching.
# Library Types - Populated at runtime # Library Types - Populated at runtime
@ -224,7 +229,7 @@ def downloadSessionImages(server, filename=None, height=150, width=150, opacity=
return info return info
def download(url, filename=None, savepath=None, session=None, chunksize=4024, mocked=False): def download(url, filename=None, savepath=None, session=None, chunksize=4024, mocked=False, unpack=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.
@ -234,6 +239,7 @@ def download(url, filename=None, savepath=None, session=None, chunksize=4024, mo
savepath (str): Defaults to current working dir. savepath (str): Defaults to current working dir.
chunksize (int): What chunksize read/write at the time. chunksize (int): What chunksize read/write at the time.
mocked (bool): Helper to do evertything except write the file. mocked (bool): Helper to do evertything except write the file.
unpack (bool): Unpack the zip file
Example: Example:
>>> download(a_episode.getStreamURL(), a_episode.location) >>> download(a_episode.getStreamURL(), a_episode.location)
@ -251,10 +257,20 @@ def download(url, filename=None, savepath=None, session=None, chunksize=4024, mo
except OSError: except OSError:
if not os.path.isdir(savepath): # pragma: no cover if not os.path.isdir(savepath): # pragma: no cover
raise raise
filename = os.path.basename(filename)
fullpath = os.path.join(savepath, filename)
try: try:
response = session.get(url, stream=True) response = session.get(url, stream=True)
# Lets grab the name if we dont supply one.
# This will be used for downloading logs/db etc.
if filename is None and response.headers.get('Content-Disposition'):
filename = re.findall(ur'filename=\"(.+)\"', response.headers.get('Content-Disposition'))
if filename:
filename = filename[0]
filename = os.path.basename(filename)
fullpath = os.path.join(savepath, filename)
# images dont have a extention so we try # images dont have a extention so we try
# to guess it from content-type # to guess it from content-type
ext = os.path.splitext(fullpath)[-1] ext = os.path.splitext(fullpath)[-1]
@ -266,16 +282,24 @@ def download(url, filename=None, savepath=None, session=None, chunksize=4024, mo
if 'image' in cp: if 'image' in cp:
ext = '.%s' % cp.split('/')[1] ext = '.%s' % cp.split('/')[1]
fullpath = '%s%s' % (fullpath, ext) fullpath = '%s%s' % (fullpath, ext)
if mocked: if mocked:
log.debug('Mocked download %s', fullpath) log.debug('Mocked download %s', fullpath)
return fullpath return fullpath
with open(fullpath, 'wb') as f: with open(fullpath, 'wb') as f:
for chunk in response.iter_content(chunk_size=chunksize): for chunk in response.iter_content(chunk_size=chunksize):
if chunk: if chunk:
f.write(chunk) f.write(chunk)
if fullpath.endswith('zip') and unpack is True:
with zipfile.ZipFile(fullpath, 'r') as zp:
zp.extractall(savepath)
# log.debug('Downloaded %s to %s from %s' % (filename, fullpath, url)) # log.debug('Downloaded %s to %s from %s' % (filename, fullpath, url))
return fullpath return fullpath
except Exception as err: # pragma: no cover except Exception as err: # pragma: no cover
log.error('Error downloading file: %s' % err) log.exception('Error downloading file: %s' % err)
raise raise
# log.exception('Failed to download %s to %s %s' % (url, fullpath, e)) # log.exception('Failed to download %s to %s %s' % (url, fullpath, e))

View file

@ -206,3 +206,13 @@ def test_server_account(pms):
assert acc.subscriptionFeatures == [] assert acc.subscriptionFeatures == []
assert acc.subscriptionState == 'Unknown' assert acc.subscriptionState == 'Unknown'
assert acc.username == 'testplexapi@gmail.com' assert acc.username == 'testplexapi@gmail.com'
def test_server_downloadLogs(tmpdir, pms):
pms.downloadLogs(savepath=tmpdir, unpack=True)
assert len(os.listdir(tmpdir)) > 1
def test_server_downloadDB(tmpdir, pms):
pms.downloadDB(savepath=tmpdir, unpack=True)
assert len(os.listdir(tmpdir)) > 1