Last active
January 12, 2020 17:31
-
-
Save ThomasDalla/2fcda5685814f3cff75795e724edb5bf to your computer and use it in GitHub Desktop.
Ad-hoc download trailers for movies in given directories
This file contains hidden or 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
#!/volume1/@appstore/python3/bin/python3 | |
import logging | |
import os | |
import subprocess | |
from os import listdir | |
from os.path import isfile, join | |
from xml.dom import minidom | |
import locale | |
import requests # not standard | |
TMDB_API_KEY = "YOUR_TMDB_API_KEY" | |
LANG = ['en', 'fr', 'jp'] | |
LOC = "en_US.utf8" | |
EXTRAS = { | |
# Map the Type from TMDB API to the sub-folder | |
# You can remove the ones you don't want | |
"Trailer": "Trailers", | |
"Featurette": "Featurettes", | |
"Behind the Scenes": "Behind The Scenes", | |
"Deleted Scene": "Deleted Scenes", | |
"Clip": "Scenes", | |
"Interview": "Interviews" | |
} | |
DIRECTORIES = ( | |
'/volume1/WD_Red_02/Media/Movies', | |
'/volume1/WD_Red_02/Media/Movies_JP', | |
'/volume1/WD_Red_02/Media/Films', | |
'/volume2/WD_Red_01/Media/Anime_Movies', | |
'/volume2/WD_Red_01/Media/Movies', | |
'/volume2/WD_Red_01/Media/Movies_JP', | |
'/volume2/WD_Red_01/Media/Movies_Music', | |
'/volume2/WD_Red_01/Media/Films' | |
) | |
locale.setlocale(locale.LC_ALL, LOC) | |
FORMAT = '%(asctime)-15s %(levelname)-8s %(message)s' | |
logging.basicConfig(format=FORMAT) | |
logger = logging.getLogger('adhoc-trailer') | |
logger.setLevel('DEBUG') | |
def get_immediate_subdirectories(a_dir): | |
return [name for name in os.listdir(a_dir) | |
if os.path.isdir(os.path.join(a_dir, name))] | |
for root in DIRECTORIES: | |
for directory in get_immediate_subdirectories(root): | |
title = directory | |
tmdbId = '' | |
full_directory = join(root, directory) | |
for file in [f for f in listdir(full_directory) if isfile(join(full_directory, f))]: | |
if tmdbId: | |
break | |
if file.endswith('.nfo') and not file.endswith('.orig.nfo'): | |
nfo_path = join(full_directory, file) | |
try: | |
mydoc = minidom.parse(nfo_path) | |
except: | |
continue | |
if mydoc: | |
items = mydoc.getElementsByTagName('movie') | |
if items: | |
movie = items[0] | |
tmdbIdNode = movie.getElementsByTagName('tmdbId') | |
if tmdbIdNode: | |
tmdbId = tmdbIdNode[0].firstChild.data | |
else: | |
uniqueidRoot = movie.getElementsByTagName('uniqueid') | |
if uniqueidRoot: | |
for uniqueidNode in uniqueidRoot: | |
if uniqueidNode.attributes['type'].value == 'tmdb': | |
tmdbId = uniqueidNode.firstChild.data | |
if tmdbId: | |
logger.debug('%s --> %s' % (title, tmdbId)) | |
for lang in LANG: | |
url = 'https://api.themoviedb.org/3/movie/%s/videos?api_key=%s&language=%s' % (tmdbId, TMDB_API_KEY, lang) | |
videos = requests.get(url).json() | |
if not videos: | |
logger.error('%s --> FAILED getting the %s videos' % (title, lang)) | |
continue | |
if 'results' not in videos: | |
if 'status_message' in videos: | |
logger.error('%s --> %s: %s' % (title, lang, videos['status_message'])) | |
else: | |
logger.error('%s --> %s: no results' % (title, lang)) | |
continue | |
if videos['results']: | |
for video in videos['results']: | |
if 'type' in video and video['type'] in EXTRAS and 'name' in video and 'key' in video and 'site' in video and video['site'] == 'YouTube': | |
plex_folder = EXTRAS[video['type']] | |
plex_folder_path = join(full_directory, plex_folder) | |
if not os.path.isdir(plex_folder_path): | |
os.mkdir(plex_folder_path) | |
output_filename = video['name'].replace("/", "", 100) | |
#output_filename = video['name'].translate ({ord(c): " " for c in "!@#$%^&*()[]{};:,./<>?\|`~-=_+"}) | |
if not any(fname.startswith(output_filename) for fname in os.listdir(plex_folder_path)): | |
logger.info('Downloading %s %s %s %s' % (title, plex_folder, lang, output_filename)) | |
output_filepath = '%s.%%(ext)s' % join(plex_folder_path, output_filename) | |
youtube_output = subprocess.run(['youtube-dl', video['key'], '--output', output_filepath, '--all-subs']) | |
if youtube_output.returncode == 0: | |
logger.info('%s --> %s / (%s) %s' % (title, plex_folder, lang, output_filename)) | |
else: | |
logger.error('%s --> %s / (%s) %s !!! YOUTUBE-DL ERROR !!!' % (title, plex_folder, lang, output_filename)) | |
else: | |
logger.debug('%s --> %s / (%s) %s ALREADY EXISTS' % (title, plex_folder, lang, output_filename)) | |
else: | |
logger.warning('%s --> %s: no results' % (title, lang)) | |
else: | |
logger.warning('%s --> !!! TMDB ID NOT FOUND !!!' % directory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment