From 61fe63ee09c252e05040ac54a72b3677f0589e96 Mon Sep 17 00:00:00 2001 From: iamkroot Date: Sun, 8 Oct 2023 17:43:28 -0500 Subject: [PATCH] mpc-be: Look in WebServer for Port in Windows reg Fixes #198 --- trakt_scrobbler/player_monitors/mpc.py | 30 +++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/trakt_scrobbler/player_monitors/mpc.py b/trakt_scrobbler/player_monitors/mpc.py index 1fb24a8..0603e0e 100644 --- a/trakt_scrobbler/player_monitors/mpc.py +++ b/trakt_scrobbler/player_monitors/mpc.py @@ -24,14 +24,22 @@ def __init__(self, scrobble_queue): super().__init__(scrobble_queue) @staticmethod - def _read_registry_cfg(path): + def _read_registry_cfg(*paths): import winreg - try: - hkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path) - except FileNotFoundError as e: - e.filename = path - raise - return {"port": lambda: winreg.QueryValueEx(hkey, "WebServerPort")[0]} + error = FileNotFoundError + for path, key in paths: + try: + hkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path) + except FileNotFoundError as e: + error = e + # try next entry in paths + else: + # found! + break + else: + error.filename = ",".join(paths) + raise error + return {"port": lambda: winreg.QueryValueEx(hkey, key)[0]} def get_vars(self): response = self.sess.get(self.URL) @@ -61,7 +69,7 @@ class MPCHCMon(MPCMon): @classmethod def read_player_cfg(cls, auto_keys=None): path = "Software\\MPC-HC\\MPC-HC\\Settings" - return cls._read_registry_cfg(path) + return cls._read_registry_cfg((path, "WebServerPort")) class MPCBEMon(MPCHCMon): @@ -70,5 +78,7 @@ class MPCBEMon(MPCHCMon): @classmethod def read_player_cfg(cls, auto_keys=None): - path = "Software\\MPC-BE\\Settings" - return cls._read_registry_cfg(path) + path1, key1 = "Software\\MPC-BE\\WebServer", "Port" + # old versions can store their port under Settings hkey + path2, key2 = "Software\\MPC-BE\\Settings", "WebServerPort" + return cls._read_registry_cfg((path1, key1), (path2, key2))