Skip to content

Instantly share code, notes, and snippets.

@xa-bi
Created November 15, 2023 18:59
Show Gist options
  • Select an option

  • Save xa-bi/5d24558ea122407f3675592da6bc0c77 to your computer and use it in GitHub Desktop.

Select an option

Save xa-bi/5d24558ea122407f3675592da6bc0c77 to your computer and use it in GitHub Desktop.
from plexapi.server import PlexServer
import glob
import re
import codecs
# create a directoy called listas_filmaffinity and put there the txt files with the movies
# the name of the txt file will be the name of the collection
# the content of the txt file will be the movies of the collection
# the format of the txt file will be: title (year)
# example: listas_filmaffinity/terror.txt
# The Shining (1980)
# The Exorcist (1973)
# The Thing (1982)
LISTS_DIRECTORY = 'listas_filmaffinity'
# Plex library section
# This section should match with the name of the library section in Plex
# and will be used to create the collections inside it and to search the movies
SECTION = 'Películas'
# Plex server url
PLEX_SERVER_URL = 'http://192.168.1.30:32400'
# Plex token
# You can get it from here: https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
TOKEN = ''
not_found = []
cached_movies = {}
def create_collection(collection_name, movies):
print('Creating collection: ' + collection_name + ' with (' + str(len(movies)) + ' movies)')
plex.createCollection(
title=collection_name,
section=SECTION,
items=movies
)
def get_collection(collection_name):
for collection in collections:
if collection.title == collection_name:
return collection
def parse_file(file_name):
print('Parsing file: ' + file_name)
movies_in_file = []
with open(file_name, encoding="utf8") as file:
for line in file:
match = re.search(r'(.*)\s\((\d{4})\).*', line)
if not match: continue
title = match.group(1).strip()
year = match.group(2)
key = title + ' - ' + year
title = re.sub(r'\s?\(.*\)', '', title)
title = re.sub(r'[\.\:]', '', title)
# From cache
if cached_movies.get(key):
movies_in_file.append(cached_movies[key])
continue
# From Plex
movie = movieslibrary.search(title=title, year=year)
if movie:
cached_movies[key] = movie[0]
movies_in_file.append(cached_movies[key])
else:
not_found.append(key)
return movies_in_file
plex = PlexServer(PLEX_SERVER_URL, TOKEN)
movieslibrary = plex.library.section(SECTION)
collections = movieslibrary.collections()
files = glob.glob(glob.escape(LISTS_DIRECTORY) + "/*.txt")
for file_name in files:
match = re.search(r'(.*)\\(.*)\.txt', file_name)
if not match: continue
collection_title = match.group(2).capitalize()
collection = get_collection(collection_title)
# Collection already exists -> delete it
if collection:
print('Removing collection: ' + collection.title)
collection.delete()
# Create collection
movies_in_file = parse_file(file_name)
create_collection(collection_title, movies_in_file)
error_file = codecs.open("not_found.txt", "w", "utf-8")
for line in sorted(list(set(not_found))):
error_file.write(line + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment