#!/usr/bin/env python # -*- coding: utf-8 -*- import os import pathlib import subprocess import requests from plexapi.server import PlexServer import urllib3 urllib3.disable_warnings() SCRIPT_DIR = pathlib.Path(os.path.dirname(os.path.abspath(__file__))) class PLEX_INFO(): def __init__(self): pass @property def address(self): return "127.0.0.1" @property def port(self): return "32400" @property def proto(self): return "http://" @property def token(self): token_script = pathlib.Path(SCRIPT_DIR).joinpath('plex-token.sh') token_proc = subprocess.Popen(['/bin/bash', token_script, "silent"], stdout=subprocess.PIPE) token = token_proc.stdout.read().decode().strip("\n") token_proc.terminate() token_proc.communicate() token_proc.kill() return token @property def url(self): return '%s%s:%s' % (self.proto, self.address, str(self.port)) print("Gathering local Plex information.") plex_info = PLEX_INFO() print("Plex server is running at %s and using token %s" % (plex_info.url, plex_info.token)) sess = requests.Session() sess.verify = False print("Connecting to Plex server API.") plex = PlexServer(plex_info.url, plex_info.token, session=sess) all_libraries = plex.library.sections() print("Checking %s libraries" % len(all_libraries)) for library in all_libraries: print("Checking library: %s" % library) collection_delete_count = 0 for collection in library.search(libtype="collection"): if collection.childCount == 0: collection.delete() collection_delete_count += 1 print("Deleted %s collections from %s library" % (collection_delete_count, library))