Created
January 12, 2020 17:27
Auto download trailers after Radarr download
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
#!/volume1/@appstore/python3/bin/python3 | |
import logging | |
import os | |
import sys | |
from os.path import join | |
import subprocess | |
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" | |
} | |
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') | |
# 1. Find the movie directory/filename and tmdbid from env vars | |
# https://github.com/Radarr/Radarr/wiki/Custom-Post-Processing-Scripts | |
# Directory: radarr_movie_path | |
# Filename: radarr_moviefile_relativepath | |
# TMDB ID: radarr_movie_tmdbid | |
# IMDB ID: radarr_movie_imdbid | |
# If IMDB ID only | |
# https://api.themoviedb.org/3/find/tt8106534?api_key=TMDB_API_KEY&external_source=imdb_id | |
# 2. If no trailer found, find the trailers using the tmdb api | |
# https://api.themoviedb.org/3/movie/181812/videos?api_key=TMDB_API_KEY&language=fr | |
# 3. Download the trailer using youtube-dl | |
# https://support.plex.tv/articles/local-files-for-trailers-and-extras/ | |
# https://github.com/ytdl-org/youtube-dl | |
# sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl | |
# sudo chmod a+rx /usr/local/bin/youtube-dl | |
# youtube-dl YLE85olJjp8 --output "/path/to/movie/%(title)s-trailer.%(ext)s" --all-subs | |
# youtube-dl YLE85olJjp8 --output "/path/to/movie/Trailers/%(title)s.%(ext)s" --all-subs | |
# youtube-dl YLE85olJjp8 --output "/path/to/movie/Trailers/$name.%(ext)s" --all-subs | |
full_directory = os.getenv('radarr_movie_path', '') | |
if not full_directory: | |
logger.error('!!ERROR!! Directory of the movie not found, exiting') | |
sys.exit(1) | |
logger.info('Movie directory: %s' % full_directory) | |
title = os.getenv('radarr_movie_title', full_directory) | |
tmdbId = os.getenv('radarr_movie_tmdbid', '') | |
if not tmdbId: | |
movie_imdbid = os.getenv('radarr_movie_imdbid', '') | |
if not movie_imdbid: | |
logger.error('!!ERROR!! IMDB ID not found, exiting') | |
sys.exit(1) | |
logger.info('No TMDB ID, but we got the IMDB ID (%s)' % movie_imdbid) | |
url = "https://api.themoviedb.org/3/find/%s?api_key=%s&external_source=imdb_id" % (movie_imdbid, TMDB_API_KEY) | |
contents = requests.get(url).json() | |
if not contents: | |
logger.error('!!ERROR!! Failed getting the TMDB ID from the IMDB ID') | |
sys.exit(1) | |
if 'movie_results' not in contents: | |
if 'status_message' in contents: | |
logger.error('!!ERROR!! %s' % contents.status_message) | |
else: | |
logger.error('!!ERROR!! Could not find the TMDB ID from the IMDB ID') | |
sys.exit(1) | |
if not contents['movie_results']: | |
logger.error('!!ERROR!! No TMDB ID found for IMDB ID %s' % movie_imdbid) | |
sys.exit(1) | |
tmdb_info = contents['movie_results'][0] | |
if not 'id' in tmdb_info: | |
logger.error('!!ERROR!! No ID for the TMDB match of the IMDB ID %s...' % movie_imdbid) | |
sys.exit(1) | |
tmdbId = tmdb_info['id'] # example: 509967 | |
logger.info('Found the TMDB ID: %d' % tmdbId) | |
else: | |
logger.info('Already have the TMDB ID: %d', tmdbId) | |
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', '--restrict-filenames']) | |
youtube_output = subprocess.run(['youtube-dl', video['key'], '--output', output_filepath, '--all-subs']) | |
if youtube_output.returncode == 0: | |
logger.info('%s --> %s / %s' % (title, plex_folder, output_filename)) | |
else: | |
logger.error('%s --> %s / %s !!! ERROR !!!' % (title, plex_folder, output_filename)) | |
else: | |
logger.debug('%s --> %s / %s ALREADY EXISTS' % (title, plex_folder, output_filename)) | |
else: | |
logger.warning('%s --> %s: no results' % (title, lang)) | |
logger.info('RADARR-TRAILER DONE FOR %s' % title) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assumptions:
Edit accordingly to match your setup, then add as a custom script notification in Radarr (connection).