get_plex_server() with 3 try
if connection fails, it will try :
* 1. url expected by new ssl certificate
* 2. url without ssl
* 3. fallback url from .env (localhost by default)
MODIFY
main.py
main.py
import requests_cache
requests_cache.install_cache('trakt_cache')
-
+env_file = path.join(path.dirname(path.abspath(__file__)), ".env")
def process_movie_section(s, watched_set, ratings_dict, listutil, collection):
# args: a section of plex movies, a set comprised of the trakt ids of all watched movies and a dict with key=slug and value=rating (1-10)
@@ -253,6 +253,64 @@ def process_show_section(s, watched_set, listutil):
show.title, show.year, guid))
+def get_plex_server():
+ plex_token = getenv("PLEX_TOKEN")
+ plex_baseurl = getenv("PLEX_BASEURL")
+ plex_fallbackurl = getenv("PLEX_FALLBACKURL")
+ if plex_token == '-':
+ plex_token = ""
+ server = None
+ # 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
+ with open(env_file, 'w') as txt:
+ txt.write("PLEX_USERNAME=" + getenv('PLEX_USERNAME') + "\n")
+ txt.write("PLEX_TOKEN=" + plex_token + "\n")
+ txt.write("PLEX_BASEURL=" + new_plex_baseurl + "\n")
+ txt.write("PLEX_FALLBACKURL=" + plex_fallbackurl + "\n")
+ txt.write("TRAKT_USERNAME=" + getenv('TRAKT_USERNAME') + "\n")
+ 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 main():
start_time = time()
@@ -302,24 +360,12 @@ def main():
ratings[r['movie']['ids']['slug']] = r['rating']
logging.debug("Movie ratings from trakt: {}".format(ratings))
logging.info('Loaded Trakt lists.')
- plex_token = getenv("PLEX_TOKEN")
- plex_baseurl = getenv("PLEX_BASEURL")
- if plex_token == '-':
- plex_token = ""
with requests_cache.disabled():
- try:
- plex = plexapi.server.PlexServer(
- token=plex_token, baseurl=plex_baseurl)
+ plex = get_plex_server()
logging.info("Server version {} updated at: {}".format(
plex.version, plex.updatedAt))
logging.info("Recently added: {}".format(
plex.library.recentlyAdded()[:5]))
- except Exception as e:
- m = "Plex connection error: {}".format(str(e))
- logging.info(m)
- print(m)
- exit(1)
-
with requests_cache.disabled():
sections = plex.library.sections()
for section in sections:

get_plex_server() with 3 try

if connection fails, it will try :
 * 1. url expected by new ssl certificate
* 2. url without ssl
* 3. fallback url from .env (localhost by default)