· 5 years ago · Jun 13, 2020, 07:20 PM
1#!/volume1/@appstore/python3/bin/python3
2
3import logging
4import os
5import subprocess
6from os import listdir
7from os.path import isfile, join
8from xml.dom import minidom
9import locale
10
11import requests # not standard
12
13TMDB_API_KEY = "YOUR_TMDB_API_KEY"
14LANG = ['en', 'fr', 'jp']
15LOC = "en_US.utf8"
16EXTRAS = {
17 # Map the Type from TMDB API to the sub-folder
18 # You can remove the ones you don't want
19 "Trailer": "Trailers",
20 "Featurette": "Featurettes",
21 "Behind the Scenes": "Behind The Scenes",
22 "Deleted Scene": "Deleted Scenes",
23 "Clip": "Scenes",
24 "Interview": "Interviews"
25}
26
27DIRECTORIES = (
28 '/volume1/WD_Red_02/Media/Movies',
29 '/volume1/WD_Red_02/Media/Movies_JP',
30 '/volume1/WD_Red_02/Media/Films',
31 '/volume2/WD_Red_01/Media/Anime_Movies',
32 '/volume2/WD_Red_01/Media/Movies',
33 '/volume2/WD_Red_01/Media/Movies_JP',
34 '/volume2/WD_Red_01/Media/Movies_Music',
35 '/volume2/WD_Red_01/Media/Films'
36)
37
38locale.setlocale(locale.LC_ALL, LOC)
39
40FORMAT = '%(asctime)-15s %(levelname)-8s %(message)s'
41logging.basicConfig(format=FORMAT)
42logger = logging.getLogger('adhoc-trailer')
43logger.setLevel('DEBUG')
44
45def get_immediate_subdirectories(a_dir):
46 return [name for name in os.listdir(a_dir)
47 if os.path.isdir(os.path.join(a_dir, name))]
48
49for root in DIRECTORIES:
50 for directory in get_immediate_subdirectories(root):
51 title = directory
52 tmdbId = ''
53 full_directory = join(root, directory)
54 for file in [f for f in listdir(full_directory) if isfile(join(full_directory, f))]:
55 if tmdbId:
56 break
57 if file.endswith('.nfo') and not file.endswith('.orig.nfo'):
58 nfo_path = join(full_directory, file)
59 try:
60 mydoc = minidom.parse(nfo_path)
61 except:
62 continue
63 if mydoc:
64 items = mydoc.getElementsByTagName('movie')
65 if items:
66 movie = items[0]
67 tmdbIdNode = movie.getElementsByTagName('tmdbId')
68 if tmdbIdNode:
69 tmdbId = tmdbIdNode[0].firstChild.data
70 else:
71 uniqueidRoot = movie.getElementsByTagName('uniqueid')
72 if uniqueidRoot:
73 for uniqueidNode in uniqueidRoot:
74 if uniqueidNode.attributes['type'].value == 'tmdb':
75 tmdbId = uniqueidNode.firstChild.data
76 if tmdbId:
77 logger.debug('%s --> %s' % (title, tmdbId))
78 for lang in LANG:
79 url = 'https://api.themoviedb.org/3/movie/%s/videos?api_key=%s&language=%s' % (tmdbId, TMDB_API_KEY, lang)
80 videos = requests.get(url).json()
81 if not videos:
82 logger.error('%s --> FAILED getting the %s videos' % (title, lang))
83 continue
84 if 'results' not in videos:
85 if 'status_message' in videos:
86 logger.error('%s --> %s: %s' % (title, lang, videos['status_message']))
87 else:
88 logger.error('%s --> %s: no results' % (title, lang))
89 continue
90 if videos['results']:
91 for video in videos['results']:
92 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':
93 plex_folder = EXTRAS[video['type']]
94 plex_folder_path = join(full_directory, plex_folder)
95 if not os.path.isdir(plex_folder_path):
96 os.mkdir(plex_folder_path)
97 output_filename = video['name'].replace("/", "", 100)
98 #output_filename = video['name'].translate ({ord(c): " " for c in "!@#$%^&*()[]{};:,./<>?\|`~-=_+"})
99 if not any(fname.startswith(output_filename) for fname in os.listdir(plex_folder_path)):
100 logger.info('Downloading %s %s %s %s' % (title, plex_folder, lang, output_filename))
101 output_filepath = '%s.%%(ext)s' % join(plex_folder_path, output_filename)
102 youtube_output = subprocess.run(['youtube-dl', video['key'], '--output', output_filepath, '--all-subs'])
103 if youtube_output.returncode == 0:
104 logger.info('%s --> %s / (%s) %s' % (title, plex_folder, lang, output_filename))
105 else:
106 logger.error('%s --> %s / (%s) %s !!! YOUTUBE-DL ERROR !!!' % (title, plex_folder, lang, output_filename))
107 else:
108 logger.debug('%s --> %s / (%s) %s ALREADY EXISTS' % (title, plex_folder, lang, output_filename))
109 else:
110 logger.warning('%s --> %s: no results' % (title, lang))
111 else:
112 logger.warning('%s --> !!! TMDB ID NOT FOUND !!!' % directory)