From 56e38cf9c382de7c68ea877ccd43050f48f4186d Mon Sep 17 00:00:00 2001 From: blacktwin Date: Wed, 12 Jul 2017 14:19:52 -0400 Subject: [PATCH] Create check_plex_log.py https://gist.github.com/blacktwin/1a8933252ad1a9bc2c97395a020c144a --- check_plex_log.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 check_plex_log.py diff --git a/check_plex_log.py b/check_plex_log.py new file mode 100644 index 0000000..f407c24 --- /dev/null +++ b/check_plex_log.py @@ -0,0 +1,70 @@ +''' +Run script by itself. Will look for WARN code followed by /library/metadata/ str in Plex logs. +This is find files that are corrupt or having playback issues. +I corrupted a file to test. +''' + +import requests +import sys + +## EDIT THESE SETTINGS ## +PLEXPY_APIKEY = 'XXXXXXXX' # Your PlexPy API key +PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL + +lib_met = [] +err_title = [] + +class PlexLOG(object): + def __init__(self, data=None): + self.error_msg = [] + for e, f, g in data[0::1]: + if f == 'WARN' and 'of key /library/metadata' in g: + self.error_msg += [[f] + [g]] + + +class UserHIS(object): + def __init__(self, data=None): + data = data or {} + self.title = [d['full_title'] for d in data] + + +def get_get_plex_log(): + # Get the user IP list from PlexPy + payload = {'apikey': PLEXPY_APIKEY, + 'cmd': 'get_plex_log'} + + try: + r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) + response = r.json() + res_data = response['response']['data']['data'] + + return PlexLOG(data=res_data) + + except Exception as e: + sys.stderr.write("PlexPy API 'get_get_plex_log' request failed: {0}.".format(e)) + +def get_get_history(key): + # Get the user IP list from PlexPy + payload = {'apikey': PLEXPY_APIKEY, + 'cmd': 'get_history', + 'rating_key': key} + + try: + r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) + response = r.json() + + res_data = response['response']['data']['data'] + return UserHIS(data=res_data) + + except Exception as e: + sys.stderr.write("PlexPy API 'get_get_history' request failed: {0}.".format(e)) + +if __name__ == '__main__': + p_log = get_get_plex_log() + for co, msg in p_log.error_msg: + lib_met += [(msg.split('/library/metadata/'))[1].split(r'\n')[0]] + for i in lib_met: + his = get_get_history(int(i)) + err_title += [x.encode('UTF8') for x in his.title] + err_title = ''.join((set(err_title))) + print(err_title + ' is having playback issues')