Last active
October 2, 2022 14:39
-
-
Save mrgfisher/c419fa9e0705865ff4f23a21c90f07f8 to your computer and use it in GitHub Desktop.
Plex movie theme copy script in python - copy movie theme mp3s from a reference directory into the Plex movie folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lets say that you have a folder full of theme.mp3 files, organised as {movie name}/theme.mp3 | |
# and you want to copy this a Plex movies folder matching on the movie name and a semi-inteligent | |
# match on similar name. The similar name may differ in odd characters - extra ' ', '!', '\'','-' etc | |
# This simple and thrown together script does this simple-ish job. | |
# This is known to 'just work' on a DSM7.1 Synology when run via ssh | |
# Note - use is 100% at your risk. It shouldn't overwrite existing files and shouldn't corrupt anything, but | |
# please review the code, have a laugh at this simplicity and lack of anything good (comments, error handling, logging) | |
# and use if you find it helpful. If you want a source of lots of movie theme music, have a search of that | |
# discussion site with a name similar to red and dit. There are some dedicated and helpful people in the /r/PlexTheme | |
# Don't forget to change the folders you are using at the top | |
# If JetBrains are reading this - love you products :D | |
#!/bin/python3 | |
import os | |
import shutil | |
src_path = "/volume1/temp/Plex Themes" | |
desc_path = "/volume1/plex-movies" | |
folder_keys = {} # this holds hash/key of the target folders | |
def gen_key(folder_name): | |
bad_chars = [' ', '-', '_', '!', '(', ')'] | |
lower_name = folder_name.lower() | |
for i in bad_chars: | |
lower_name = lower_name.replace(i, '') | |
return lower_name | |
def check_and_copy(src_dir): | |
global folder_keys | |
#if you just want to test a subset of them: | |
#if not src_dir.startswith("Z"): | |
# return | |
target_dir = src_dir | |
full_path = src_path + "/" + src_dir + "/theme.mp3" | |
if not os.path.exists(full_path): | |
print("Scenario 3 - source directory with no theme " + full_path) | |
return 3 # theme file missing | |
if not os.path.exists(desc_path + "/" + src_dir): | |
dir_as_key = gen_key(target_dir) | |
if dir_as_key in folder_keys: | |
print("Scenario 4 - found a fuzzy match of '" + src_dir + "' with target of '" + folder_keys[dir_as_key] + "'") | |
target_dir = folder_keys[dir_as_key] | |
else: | |
print("Scenario 1 - target directory not found as exact match - movie not in my collection '" + src_dir + "'") | |
return 1 | |
target_file = desc_path + "/" + target_dir + "/theme.mp3" | |
if os.path.exists(target_file): | |
print("Scenario 2 - target file exists, nothing to do '" + src_dir + "'") | |
return 2 | |
print("Copying from '" + full_path + "' to '" + target_file + "'") | |
shutil.copy(full_path, target_file) | |
return 0 | |
def scan_and_copy(): | |
global folder_keys | |
dest_level = os.scandir(desc_path) | |
for entry in dest_level: | |
if entry.is_dir(): | |
this_key = gen_key(entry.name) | |
if this_key in folder_keys: | |
print("ERROR - gen key has a duplicate" + entry.name) | |
return | |
folder_keys[this_key] = entry.name | |
top_level = os.scandir(src_path) | |
for entry in top_level: | |
if entry.is_dir(): | |
check_and_copy(entry.name) | |
# Press the green button in the gutter to run the script. | |
if __name__ == '__main__': | |
scan_and_copy() | |
# See PyCharm help at https://www.jetbrains.com/help/pycharm/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If anyone either reads or uses this please comment - it will encourage me to share other gists in the future :-)