From 78fda958534276474bdbc876a248c56475879586 Mon Sep 17 00:00:00 2001
From: Chuddah <chuddah@gmail.com>
Date: Sun, 16 Feb 2020 22:07:56 +0000
Subject: [PATCH 1/3] Replaced debug output of json.dumps with an indirection
 to lazy __str__.

json.dumps is a processing intensive operation. This is being called
every time data is received from the server (most noticeably during
library updates) for debug logging. If the user has debug logging
disabled (the default option) then the user is still paying for
processing which is discarded.

The fix is to add a level of indirection where the dumps function is
only called if a string representation of the json is requested; ie.
when the debug string is evaluated.
---
 jellyfin_kodi/entrypoint/default.py |  4 ++--
 jellyfin_kodi/entrypoint/service.py |  4 ++--
 jellyfin_kodi/helper/debug.py       | 15 +++++++++++++++
 jellyfin_kodi/jellyfin/http.py      |  5 +++--
 jellyfin_kodi/monitor.py            |  4 ++--
 5 files changed, 24 insertions(+), 8 deletions(-)
 create mode 100644 jellyfin_kodi/helper/debug.py

diff --git a/jellyfin_kodi/entrypoint/default.py b/jellyfin_kodi/entrypoint/default.py
index ba8a7573..97f1ac21 100644
--- a/jellyfin_kodi/entrypoint/default.py
+++ b/jellyfin_kodi/entrypoint/default.py
@@ -15,7 +15,7 @@ import client
 from database import reset, get_sync, Database, jellyfin_db, get_credentials
 from objects import Objects, Actions
 from downloader import TheVoid
-from helper import translate, event, settings, window, dialog, api, JSONRPC
+from helper import translate, event, settings, window, dialog, api, JSONRPC, debug
 
 #################################################################################################
 
@@ -45,7 +45,7 @@ class Events(object):
         if server == 'None':
             server = None
 
-        LOG.info("path: %s params: %s", path, json.dumps(params, indent=4))
+        LOG.info("path: %s params: %s", path, debug.JsonDebugPrinter(params))
 
         if '/extrafanart' in base_url:
 
diff --git a/jellyfin_kodi/entrypoint/service.py b/jellyfin_kodi/entrypoint/service.py
index b3b0ed2b..28ebc155 100644
--- a/jellyfin_kodi/entrypoint/service.py
+++ b/jellyfin_kodi/entrypoint/service.py
@@ -20,7 +20,7 @@ import library
 import setup
 import monitor
 from views import Views, verify_kodi_defaults
-from helper import translate, window, settings, event, dialog
+from helper import translate, window, settings, event, dialog, debug
 from jellyfin import Jellyfin
 
 #################################################################################################
@@ -171,7 +171,7 @@ class Service(xbmc.Monitor):
 
             data = json.loads(data)
 
-        LOG.debug("[ %s: %s ] %s", sender, method, json.dumps(data, indent=4))
+        LOG.debug("[ %s: %s ] %s", sender, method, debug.JsonDebugPrinter(data))
 
         if method == 'ServerOnline':
             if data.get('ServerId') is None:
diff --git a/jellyfin_kodi/helper/debug.py b/jellyfin_kodi/helper/debug.py
new file mode 100644
index 00000000..7dc2e4dc
--- /dev/null
+++ b/jellyfin_kodi/helper/debug.py
@@ -0,0 +1,15 @@
+# -*- coding: utf-8 -*-
+
+##################################################################################################
+
+import json
+
+##################################################################################################
+
+class JsonDebugPrinter(object):
+
+    def __init__(self, json):
+        self.json = json
+
+    def __str__(self):
+        return json.dumps(self.json, indent=4)
diff --git a/jellyfin_kodi/jellyfin/http.py b/jellyfin_kodi/jellyfin/http.py
index 63c58f3b..1b7921ed 100644
--- a/jellyfin_kodi/jellyfin/http.py
+++ b/jellyfin_kodi/jellyfin/http.py
@@ -11,6 +11,7 @@ import requests
 from six import string_types
 
 from .exceptions import HTTPException
+from helper import debug
 
 #################################################################################################
 
@@ -80,7 +81,7 @@ class HTTP(object):
             raise AttributeError("Request cannot be empty")
 
         data = self._request(data)
-        LOG.debug("--->[ http ] %s", json.dumps(data, indent=4))
+        LOG.debug("--->[ http ] %s", debug.JsonDebugPrinter(data))
         retry = data.pop('retry', 5)
 
         while True:
@@ -162,7 +163,7 @@ class HTTP(object):
                     elapsed = int(r.elapsed.total_seconds() * 1000)
                     response = r.json()
                     LOG.debug("---<[ http ][%s ms]", elapsed)
-                    LOG.debug(json.dumps(response, indent=4))
+                    LOG.debug(debug.JsonDebugPrinter(response))
 
                     return response
                 except ValueError:
diff --git a/jellyfin_kodi/monitor.py b/jellyfin_kodi/monitor.py
index 00f88d12..804fa506 100644
--- a/jellyfin_kodi/monitor.py
+++ b/jellyfin_kodi/monitor.py
@@ -15,7 +15,7 @@ import downloader
 import player
 from client import get_device_id
 from objects import PlaylistWorker, on_play, on_update, special_listener
-from helper import translate, settings, window, dialog, api, JSONRPC
+from helper import translate, settings, window, dialog, api, JSONRPC, debug
 from jellyfin import Jellyfin
 from webservice import WebService
 
@@ -96,7 +96,7 @@ class Monitor(xbmc.Monitor):
 
             data = json.loads(data)
 
-        LOG.debug("[ %s: %s ] %s", sender, method, json.dumps(data, indent=4))
+        LOG.debug("[ %s: %s ] %s", sender, method, debug.JsonDebugPrinter(data))
 
         if self.sleep:
             LOG.info("System.OnSleep detected, ignore monitor request.")

From b9817a5617aeecfaeccb153256900d6fe8d09509 Mon Sep 17 00:00:00 2001
From: Chuddah <chuddah@gmail.com>
Date: Sun, 16 Feb 2020 22:22:23 +0000
Subject: [PATCH 2/3] Revert "Replaced debug output of json.dumps with an
 indirection to lazy __str__."

This reverts commit 78fda958534276474bdbc876a248c56475879586.

Committed on the wrong branch.
---
 jellyfin_kodi/entrypoint/default.py |  4 ++--
 jellyfin_kodi/entrypoint/service.py |  4 ++--
 jellyfin_kodi/helper/debug.py       | 15 ---------------
 jellyfin_kodi/jellyfin/http.py      |  5 ++---
 jellyfin_kodi/monitor.py            |  4 ++--
 5 files changed, 8 insertions(+), 24 deletions(-)
 delete mode 100644 jellyfin_kodi/helper/debug.py

diff --git a/jellyfin_kodi/entrypoint/default.py b/jellyfin_kodi/entrypoint/default.py
index 97f1ac21..ba8a7573 100644
--- a/jellyfin_kodi/entrypoint/default.py
+++ b/jellyfin_kodi/entrypoint/default.py
@@ -15,7 +15,7 @@ import client
 from database import reset, get_sync, Database, jellyfin_db, get_credentials
 from objects import Objects, Actions
 from downloader import TheVoid
-from helper import translate, event, settings, window, dialog, api, JSONRPC, debug
+from helper import translate, event, settings, window, dialog, api, JSONRPC
 
 #################################################################################################
 
@@ -45,7 +45,7 @@ class Events(object):
         if server == 'None':
             server = None
 
-        LOG.info("path: %s params: %s", path, debug.JsonDebugPrinter(params))
+        LOG.info("path: %s params: %s", path, json.dumps(params, indent=4))
 
         if '/extrafanart' in base_url:
 
diff --git a/jellyfin_kodi/entrypoint/service.py b/jellyfin_kodi/entrypoint/service.py
index 28ebc155..b3b0ed2b 100644
--- a/jellyfin_kodi/entrypoint/service.py
+++ b/jellyfin_kodi/entrypoint/service.py
@@ -20,7 +20,7 @@ import library
 import setup
 import monitor
 from views import Views, verify_kodi_defaults
-from helper import translate, window, settings, event, dialog, debug
+from helper import translate, window, settings, event, dialog
 from jellyfin import Jellyfin
 
 #################################################################################################
@@ -171,7 +171,7 @@ class Service(xbmc.Monitor):
 
             data = json.loads(data)
 
-        LOG.debug("[ %s: %s ] %s", sender, method, debug.JsonDebugPrinter(data))
+        LOG.debug("[ %s: %s ] %s", sender, method, json.dumps(data, indent=4))
 
         if method == 'ServerOnline':
             if data.get('ServerId') is None:
diff --git a/jellyfin_kodi/helper/debug.py b/jellyfin_kodi/helper/debug.py
deleted file mode 100644
index 7dc2e4dc..00000000
--- a/jellyfin_kodi/helper/debug.py
+++ /dev/null
@@ -1,15 +0,0 @@
-# -*- coding: utf-8 -*-
-
-##################################################################################################
-
-import json
-
-##################################################################################################
-
-class JsonDebugPrinter(object):
-
-    def __init__(self, json):
-        self.json = json
-
-    def __str__(self):
-        return json.dumps(self.json, indent=4)
diff --git a/jellyfin_kodi/jellyfin/http.py b/jellyfin_kodi/jellyfin/http.py
index 1b7921ed..63c58f3b 100644
--- a/jellyfin_kodi/jellyfin/http.py
+++ b/jellyfin_kodi/jellyfin/http.py
@@ -11,7 +11,6 @@ import requests
 from six import string_types
 
 from .exceptions import HTTPException
-from helper import debug
 
 #################################################################################################
 
@@ -81,7 +80,7 @@ class HTTP(object):
             raise AttributeError("Request cannot be empty")
 
         data = self._request(data)
-        LOG.debug("--->[ http ] %s", debug.JsonDebugPrinter(data))
+        LOG.debug("--->[ http ] %s", json.dumps(data, indent=4))
         retry = data.pop('retry', 5)
 
         while True:
@@ -163,7 +162,7 @@ class HTTP(object):
                     elapsed = int(r.elapsed.total_seconds() * 1000)
                     response = r.json()
                     LOG.debug("---<[ http ][%s ms]", elapsed)
-                    LOG.debug(debug.JsonDebugPrinter(response))
+                    LOG.debug(json.dumps(response, indent=4))
 
                     return response
                 except ValueError:
diff --git a/jellyfin_kodi/monitor.py b/jellyfin_kodi/monitor.py
index 804fa506..00f88d12 100644
--- a/jellyfin_kodi/monitor.py
+++ b/jellyfin_kodi/monitor.py
@@ -15,7 +15,7 @@ import downloader
 import player
 from client import get_device_id
 from objects import PlaylistWorker, on_play, on_update, special_listener
-from helper import translate, settings, window, dialog, api, JSONRPC, debug
+from helper import translate, settings, window, dialog, api, JSONRPC
 from jellyfin import Jellyfin
 from webservice import WebService
 
@@ -96,7 +96,7 @@ class Monitor(xbmc.Monitor):
 
             data = json.loads(data)
 
-        LOG.debug("[ %s: %s ] %s", sender, method, debug.JsonDebugPrinter(data))
+        LOG.debug("[ %s: %s ] %s", sender, method, json.dumps(data, indent=4))
 
         if self.sleep:
             LOG.info("System.OnSleep detected, ignore monitor request.")

From b878a0e433033395518ba7c00893333865374a0a Mon Sep 17 00:00:00 2001
From: Chuddah <chuddah@gmail.com>
Date: Sun, 16 Feb 2020 22:56:46 +0000
Subject: [PATCH 3/3] Added download thread to the download_threads when
 created.

---
 jellyfin_kodi/library.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/jellyfin_kodi/library.py b/jellyfin_kodi/library.py
index 180ece7d..29d4ea99 100644
--- a/jellyfin_kodi/library.py
+++ b/jellyfin_kodi/library.py
@@ -231,6 +231,7 @@ class Library(threading.Thread):
                 new_thread = GetItemWorker(self.server, queue[0], queue[1])
                 new_thread.start()
                 LOG.info("-->[ q:download/%s ]", id(new_thread))
+                self.download_threads.append(new_thread)
 
     def worker_sort(self):