diff --git a/plexpy/api2.py b/plexpy/api2.py index 3613888a..5f0fdec3 100644 --- a/plexpy/api2.py +++ b/plexpy/api2.py @@ -32,6 +32,7 @@ import xmltodict import plexpy import config import database +import helpers import libraries import logger import mobile_app @@ -173,47 +174,51 @@ class API2: end = int(end) if regex: - logger.api_debug(u'Tautulli APIv2 :: Filtering log using regex %s' % regex) - reg = re.compile('u' + regex, flags=re.I) + logger.api_debug(u"Tautulli APIv2 :: Filtering log using regex '%s'" % regex) + reg = re.compile(regex, flags=re.I) - for line in open(logfile, 'r').readlines(): - temp_loglevel_and_time = None + with open(logfile, 'r') as f: + for line in f.readlines(): + temp_loglevel_and_time = None - try: - temp_loglevel_and_time = line.split('- ') - loglvl = temp_loglevel_and_time[1].split(' :')[0].strip() - tl_tread = line.split(' :: ') - if loglvl is None: - msg = line.replace('\n', '') - else: - msg = line.split(' : ')[1].replace('\n', '') - thread = tl_tread[1].split(' : ')[0] - except IndexError: - # We assume this is a traceback - tl = (len(templog) - 1) - templog[tl]['msg'] += line.replace('\n', '') - continue + try: + temp_loglevel_and_time = line.split('- ') + loglvl = temp_loglevel_and_time[1].split(' :')[0].strip() + tl_tread = line.split(' :: ') + if loglvl is None: + msg = line.replace('\n', '') + else: + msg = line.split(' : ')[1].replace('\n', '') + thread = tl_tread[1].split(' : ')[0] + except IndexError: + # We assume this is a traceback + tl = (len(templog) - 1) + templog[tl]['msg'] += helpers.sanitize(unicode(line.replace('\n', ''), 'utf-8')) + continue - if len(line) > 1 and temp_loglevel_and_time is not None and loglvl in line: + if len(line) > 1 and temp_loglevel_and_time is not None and loglvl in line: - d = { - 'time': temp_loglevel_and_time[0], - 'loglevel': loglvl, - 'msg': msg.replace('\n', ''), - 'thread': thread - } - templog.append(d) + d = { + 'time': temp_loglevel_and_time[0], + 'loglevel': loglvl, + 'msg': helpers.sanitize(unicode(msg.replace('\n', ''), 'utf-8')), + 'thread': thread + } + templog.append(d) + + if order == 'desc': + templog = templog[::-1] if end > 0 or start > 0: - logger.api_debug(u'Tautulli APIv2 :: Slicing the log from %s to %s' % (start, end)) - templog = templog[start:end] + logger.api_debug(u"Tautulli APIv2 :: Slicing the log from %s to %s" % (start, end)) + templog = templog[start:end] if sort: - logger.api_debug(u'Tautulli APIv2 :: Sorting log based on %s' % sort) + logger.api_debug(u"Tautulli APIv2 :: Sorting log based on '%s'" % sort) templog = sorted(templog, key=lambda k: k[sort]) if search: - logger.api_debug(u'Tautulli APIv2 :: Searching log values for %s' % search) + logger.api_debug(u"Tautulli APIv2 :: Searching log values for '%s'" % search) tt = [d for d in templog for k, v in d.items() if search.lower() in v.lower()] if len(tt): @@ -222,16 +227,13 @@ class API2: if regex: tt = [] for l in templog: - stringdict = ' '.join('{}{}'.format(k, v) for k, v in l.items()) + stringdict = ' '.join(u'{}{}'.format(k, v) for k, v in l.items()) if reg.search(stringdict): tt.append(l) if len(tt): templog = tt - if order == 'desc': - templog = templog[::-1] - return templog def get_settings(self, key=''):