Move plex_server logic to separate module
This removes it's dependency from main module.
MODIFY
plex_trakt_sync/commands/sync.py
plex_trakt_sync/commands/sync.py
import click
from plex_trakt_sync.requests_cache import requests_cache
-from plex_trakt_sync.main import get_plex_server
+from plex_trakt_sync.plex_server import get_plex_server
from plex_trakt_sync.config import CONFIG
from plex_trakt_sync.decorators import measure_time
from plex_trakt_sync.main import process_movie_section, process_show_section

MODIFY
plex_trakt_sync/main.py
plex_trakt_sync/main.py
-import plexapi.server
import trakt
from plex_trakt_sync.path import pytrakt_file
from plex_trakt_sync.plex_api import PlexLibrarySection
@@ -301,64 +300,6 @@ def process_show_section(s, watched_set, listutil):
show.title, show.year, e, guid))
-def get_plex_server():
- plex_token = CONFIG["PLEX_TOKEN"]
- plex_baseurl = CONFIG["PLEX_BASEURL"]
- plex_fallbackurl = CONFIG["PLEX_FALLBACKURL"]
- if plex_token == '-':
- plex_token = ""
- server = None
-
- plexapi.X_PLEX_PLATFORM = 'PlexTraktSync'
- plexapi.BASE_HEADERS['X-Plex-Platform'] = plexapi.X_PLEX_PLATFORM
-
- # if connection fails, it will try :
- # 1. url expected by new ssl certificate
- # 2. url without ssl
- # 3. fallback url (localhost)
- try:
- server = plexapi.server.PlexServer(
- token=plex_token, baseurl=plex_baseurl)
- except plexapi.server.requests.exceptions.SSLError as e:
- m = "Plex connection error: {}, fallback url {} didn't respond either.".format(str(e), plex_fallbackurl)
- excep_msg = str(e.__context__)
- if "doesn't match '*." in excep_msg:
- hash_pos = excep_msg.find("*.") + 2
- new_hash = excep_msg[hash_pos:hash_pos + 32]
- end_pos = plex_baseurl.find(".plex.direct")
- new_plex_baseurl = plex_baseurl[:end_pos - 32] + new_hash + plex_baseurl[end_pos:]
- try: # 1
- server = plexapi.server.PlexServer(
- token=plex_token, baseurl=new_plex_baseurl)
- # save new url to .env
- CONFIG["PLEX_TOKEN"] = plex_token
- CONFIG["PLEX_BASEURL"] = new_plex_baseurl
- CONFIG["PLEX_FALLBACKURL"] = plex_fallbackurl
- CONFIG.save()
- logging.info("Plex server url changed to {}".format(new_plex_baseurl))
- except Exception:
- pass
- if server is None and plex_baseurl[:5] == "https":
- new_plex_baseurl = plex_baseurl.replace("https", "http")
- try: # 2
- server = plexapi.server.PlexServer(
- token=plex_token, baseurl=new_plex_baseurl)
- logging.warning("Switched to Plex unsecure connection because of SSLError.")
- except Exception:
- pass
- except Exception as e:
- m = "Plex connection error: {}, fallback url {} didn't respond either.".format(str(e), plex_fallbackurl)
- pass
- if server is None:
- try: # 3
- server = plexapi.server.PlexServer(
- token=plex_token, baseurl=plex_fallbackurl)
- logging.warning("No response from {}, fallback to {}".format(plex_baseurl, plex_fallbackurl))
- except Exception:
- logging.error(m)
- print(m)
- exit(1)
- return server
def respect_trakt_rate(last_time):

ADD
plex_trakt_sync/plex_server.py
+import plexapi.server
+from plex_trakt_sync.config import CONFIG
+from plex_trakt_sync.logging import logging
+
+
+def get_plex_server():
+ plex_token = CONFIG["PLEX_TOKEN"]
+ plex_baseurl = CONFIG["PLEX_BASEURL"]
+ plex_fallbackurl = CONFIG["PLEX_FALLBACKURL"]
+ if plex_token == '-':
+ plex_token = ""
+ server = None
+
+ plexapi.X_PLEX_PLATFORM = 'PlexTraktSync'
+ plexapi.BASE_HEADERS['X-Plex-Platform'] = plexapi.X_PLEX_PLATFORM
+
+ # if connection fails, it will try :
+ # 1. url expected by new ssl certificate
+ # 2. url without ssl
+ # 3. fallback url (localhost)
+ try:
+ server = plexapi.server.PlexServer(
+ token=plex_token, baseurl=plex_baseurl)
+ except plexapi.server.requests.exceptions.SSLError as e:
+ m = "Plex connection error: {}, fallback url {} didn't respond either.".format(str(e), plex_fallbackurl)
+ excep_msg = str(e.__context__)
+ if "doesn't match '*." in excep_msg:
+ hash_pos = excep_msg.find("*.") + 2
+ new_hash = excep_msg[hash_pos:hash_pos + 32]
+ end_pos = plex_baseurl.find(".plex.direct")
+ new_plex_baseurl = plex_baseurl[:end_pos - 32] + new_hash + plex_baseurl[end_pos:]
+ try: # 1
+ server = plexapi.server.PlexServer(
+ token=plex_token, baseurl=new_plex_baseurl)
+ # save new url to .env
+ CONFIG["PLEX_TOKEN"] = plex_token
+ CONFIG["PLEX_BASEURL"] = new_plex_baseurl
+ CONFIG["PLEX_FALLBACKURL"] = plex_fallbackurl
+ CONFIG.save()
+ logging.info("Plex server url changed to {}".format(new_plex_baseurl))
+ except Exception:
+ pass
+ if server is None and plex_baseurl[:5] == "https":
+ new_plex_baseurl = plex_baseurl.replace("https", "http")
+ try: # 2
+ server = plexapi.server.PlexServer(
+ token=plex_token, baseurl=new_plex_baseurl)
+ logging.warning("Switched to Plex unsecure connection because of SSLError.")
+ except Exception:
+ pass
+ except Exception as e:
+ m = "Plex connection error: {}, fallback url {} didn't respond either.".format(str(e), plex_fallbackurl)
+ pass
+ if server is None:
+ try: # 3
+ server = plexapi.server.PlexServer(
+ token=plex_token, baseurl=plex_fallbackurl)
+ logging.warning("No response from {}, fallback to {}".format(plex_baseurl, plex_fallbackurl))
+ except Exception:
+ logging.error(m)
+ print(m)
+ exit(1)
+ return server

Move plex_server logic to separate module

This removes it's dependency from main module.