· 6 years ago · Jan 12, 2020, 05:26 PM
1#!/volume1/@appstore/python3/bin/python3
2
3import logging
4import os
5import sys
6from os.path import join
7import subprocess
8import locale
9
10import requests # not standard
11
12TMDB_API_KEY = "YOUR_TMDB_API_KEY"
13LANG = ["en", "fr", "jp"]
14LOC = "en_US.utf8"
15EXTRAS = {
16 # Map the Type from TMDB API to the sub-folder
17 # You can remove the ones you don't want
18 "Trailer": "Trailers",
19 "Featurette": "Featurettes",
20 "Behind the Scenes": "Behind The Scenes",
21 "Deleted Scene": "Deleted Scenes",
22 "Clip": "Scenes",
23 "Interview": "Interviews"
24}
25
26locale.setlocale(locale.LC_ALL, LOC)
27
28FORMAT = '%(asctime)-15s %(levelname)-8s %(message)s'
29logging.basicConfig(format=FORMAT)
30logger = logging.getLogger('adhoc-trailer')
31logger.setLevel('DEBUG')
32
33# 1. Find the movie directory/filename and tmdbid from env vars
34# https://github.com/Radarr/Radarr/wiki/Custom-Post-Processing-Scripts
35# Directory: radarr_movie_path
36# Filename: radarr_moviefile_relativepath
37# TMDB ID: radarr_movie_tmdbid
38# IMDB ID: radarr_movie_imdbid
39# If IMDB ID only
40# https://api.themoviedb.org/3/find/tt8106534?api_key=TMDB_API_KEY&external_source=imdb_id
41
42# 2. If no trailer found, find the trailers using the tmdb api
43# https://api.themoviedb.org/3/movie/181812/videos?api_key=TMDB_API_KEY&language=fr
44
45# 3. Download the trailer using youtube-dl
46# https://support.plex.tv/articles/local-files-for-trailers-and-extras/
47# https://github.com/ytdl-org/youtube-dl
48# sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
49# sudo chmod a+rx /usr/local/bin/youtube-dl
50# youtube-dl YLE85olJjp8 --output "/path/to/movie/%(title)s-trailer.%(ext)s" --all-subs
51# youtube-dl YLE85olJjp8 --output "/path/to/movie/Trailers/%(title)s.%(ext)s" --all-subs
52# youtube-dl YLE85olJjp8 --output "/path/to/movie/Trailers/$name.%(ext)s" --all-subs
53
54full_directory = os.getenv('radarr_movie_path', '')
55if not full_directory:
56 logger.error('!!ERROR!! Directory of the movie not found, exiting')
57 sys.exit(1)
58logger.info('Movie directory: %s' % full_directory)
59
60title = os.getenv('radarr_movie_title', full_directory)
61
62tmdbId = os.getenv('radarr_movie_tmdbid', '')
63if not tmdbId:
64 movie_imdbid = os.getenv('radarr_movie_imdbid', '')
65 if not movie_imdbid:
66 logger.error('!!ERROR!! IMDB ID not found, exiting')
67 sys.exit(1)
68 logger.info('No TMDB ID, but we got the IMDB ID (%s)' % movie_imdbid)
69
70 url = "https://api.themoviedb.org/3/find/%s?api_key=%s&external_source=imdb_id" % (movie_imdbid, TMDB_API_KEY)
71 contents = requests.get(url).json()
72 if not contents:
73 logger.error('!!ERROR!! Failed getting the TMDB ID from the IMDB ID')
74 sys.exit(1)
75 if 'movie_results' not in contents:
76 if 'status_message' in contents:
77 logger.error('!!ERROR!! %s' % contents.status_message)
78 else:
79 logger.error('!!ERROR!! Could not find the TMDB ID from the IMDB ID')
80 sys.exit(1)
81 if not contents['movie_results']:
82 logger.error('!!ERROR!! No TMDB ID found for IMDB ID %s' % movie_imdbid)
83 sys.exit(1)
84
85 tmdb_info = contents['movie_results'][0]
86 if not 'id' in tmdb_info:
87 logger.error('!!ERROR!! No ID for the TMDB match of the IMDB ID %s...' % movie_imdbid)
88 sys.exit(1)
89 tmdbId = tmdb_info['id'] # example: 509967
90 logger.info('Found the TMDB ID: %d' % tmdbId)
91else:
92 logger.info('Already have the TMDB ID: %d', tmdbId)
93
94if tmdbId:
95 logger.debug('%s --> %s' % (title, tmdbId))
96 for lang in LANG:
97 url = 'https://api.themoviedb.org/3/movie/%s/videos?api_key=%s&language=%s' % (tmdbId, TMDB_API_KEY, lang)
98 videos = requests.get(url).json()
99 if not videos:
100 logger.error('%s --> FAILED getting the %s videos' % (title, lang))
101 continue
102 if 'results' not in videos:
103 if 'status_message' in videos:
104 logger.error('%s --> %s: %s' % (title, lang, videos['status_message']))
105 else:
106 logger.error('%s --> %s: no results' % (title, lang))
107 continue
108 if videos['results']:
109 for video in videos['results']:
110 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':
111 plex_folder = EXTRAS[video['type']]
112 plex_folder_path = join(full_directory, plex_folder)
113 if not os.path.isdir(plex_folder_path):
114 os.mkdir(plex_folder_path)
115 output_filename = video['name'].replace("/", "", 100)
116 #output_filename = video['name'].translate ({ord(c): " " for c in "!@#$%^&*()[]{};:,./<>?\|`~-=_+"})
117 if not any(fname.startswith(output_filename) for fname in os.listdir(plex_folder_path)):
118 logger.info('Downloading %s %s %s %s' % (title, plex_folder, lang, output_filename))
119 output_filepath = '%s.%%(ext)s' % join(plex_folder_path, output_filename)
120 #youtube_output = subprocess.run(['youtube-dl', video['key'], '--output', output_filepath, '--all-subs', '--restrict-filenames'])
121 youtube_output = subprocess.run(['youtube-dl', video['key'], '--output', output_filepath, '--all-subs'])
122 if youtube_output.returncode == 0:
123 logger.info('%s --> %s / %s' % (title, plex_folder, output_filename))
124 else:
125 logger.error('%s --> %s / %s !!! ERROR !!!' % (title, plex_folder, output_filename))
126 else:
127 logger.debug('%s --> %s / %s ALREADY EXISTS' % (title, plex_folder, output_filename))
128 else:
129 logger.warning('%s --> %s: no results' % (title, lang))
130
131logger.info('RADARR-TRAILER DONE FOR %s' % title)