Skip to content

Instantly share code, notes, and snippets.

@Rycochet
Last active March 7, 2025 21:01
Show Gist options
  • Save Rycochet/da47919a1bbdc68e16d7d8c742dce348 to your computer and use it in GitHub Desktop.
Save Rycochet/da47919a1bbdc68e16d7d8c742dce348 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#
# Description: List all Unmatched and Incorrectly matched files.
# Author: https://github.com/Rycochet
# Requires: plexapi, python_dotenv
#
# Config via a .env file, PLEX_TOKEN, PLEX_URL (optional), PLEX_PATH<n> + PLEX_PATH<n>_REPLACE
import glob
import os
import re
from dotenv import load_dotenv
from plexapi.server import PlexServer
load_dotenv()
PLEX_TOKEN = os.getenv("PLEX_TOKEN")
if not PLEX_TOKEN:
print("Error: You must supply a PLEX_TOKEN env variable")
sys.exit(-1)
PLEX_URL = os.getenv("PLEX_URL", "http://localhost:32400")
FIX_PATHS = {}
for i in range(9):
if os.getenv(f'PLEX_PATH{i}') and os.getenv(f'PLEX_PATH{i}_REPLACE'):
FIX_PATHS[os.getenv(f'PLEX_PATH{i}')] = os.getenv(f'PLEX_PATH{i}_REPLACE')
if not len(FIX_PATHS):
print("Error: You must supply PLEX_PATH<n> and PLEX_PATH<n>_REPLACE env vars. Only libraries containing these paths will get processed")
sys.exit(-1)
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
count = 0
for section in plex.library.sections():
folders = []
for path in section.locations:
if not path.endswith("/"):
path = path + "/"
for fix in FIX_PATHS:
path = path.replace(fix, FIX_PATHS[fix])
folders = folders + glob.glob(path + "*")
if not len(folders):
continue
print(f'Checking {section.title} ({len(folders)} folders{", please be patient" if len(folders) > 5000 else ""})')
for item in section.all():
found = False
correct = False
for guid in item.guids:
if "tvdb://" in guid.id:
found = True
tvid = f'{{tvdb-{guid.id[7:]}}}'
for folder in folders:
if tvid in folder:
correct = True
if "tmdb://" in guid.id:
found = True
tmid = f'{{tmdb-{guid.id[7:]}}}'
for folder in folders:
if tmid in folder:
correct = True
if not found:
count += 1
item.addLabel("FixMatch")
print(f' Unmatched: {item.title} ({item.year})')
elif not correct:
count += 1
item.addLabel("FixMatch")
print(f' Incorrect: {item.title} ({item.year})')
if count:
print(f'Done, {count} found, filter label="FixMatch" in Plex')
else:
print("Done")
@Rycochet
Copy link
Author

Rycochet commented Mar 7, 2025

Important

This has been replaced by https://github.com/Rycochet/plex-find-mismatch/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment