· 6 years ago · Jan 12, 2020, 05:28 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
38# DIRECTORIES = (
39# '/media/spooky/WD_Red_02/Media/Movies',
40# '/media/spooky/WD_Red_02/Media/Movies_JP',
41# '/media/spooky/WD_Red_02/Media/Films',
42# '/media/spooky/WD_Red_01/Media/Anime_Movies',
43# '/media/spooky/WD_Red_01/Media/Movies',
44# '/media/spooky/WD_Red_01/Media/Movies_JP',
45# '/media/spooky/WD_Red_01/Media/Movies_Music',
46# '/media/spooky/WD_Red_01/Media/Films'
47# )
48
49locale.setlocale(locale.LC_ALL, LOC)
50
51FORMAT = '%(asctime)-15s %(levelname)-8s %(message)s'
52logging.basicConfig(format=FORMAT)
53logger = logging.getLogger('adhoc-trailer')
54logger.setLevel('DEBUG')
55
56def get_immediate_subdirectories(a_dir):
57 return [name for name in os.listdir(a_dir)
58 if os.path.isdir(os.path.join(a_dir, name))]
59
60for root in DIRECTORIES:
61 for directory in get_immediate_subdirectories(root):
62 title = directory
63 tmdbId = ''
64 full_directory = join(root, directory)
65 for file in [f for f in listdir(full_directory) if isfile(join(full_directory, f))]:
66 if tmdbId:
67 break
68 if file.endswith('.nfo') and not file.endswith('.orig.nfo'):
69 nfo_path = join(full_directory, file)
70 try:
71 mydoc = minidom.parse(nfo_path)
72 except:
73 continue
74 if mydoc:
75 items = mydoc.getElementsByTagName('movie')
76 if items:
77 movie = items[0]
78 tmdbIdNode = movie.getElementsByTagName('tmdbId')
79 if tmdbIdNode:
80 tmdbId = tmdbIdNode[0].firstChild.data
81 else:
82 uniqueidRoot = movie.getElementsByTagName('uniqueid')
83 if uniqueidRoot:
84 for uniqueidNode in uniqueidRoot:
85 if uniqueidNode.attributes['type'].value == 'tmdb':
86 tmdbId = uniqueidNode.firstChild.data
87 if tmdbId:
88 logger.debug('%s --> %s' % (title, tmdbId))
89 for lang in LANG:
90 url = 'https://api.themoviedb.org/3/movie/%s/videos?api_key=%s&language=%s' % (tmdbId, TMDB_API_KEY, lang)
91 videos = requests.get(url).json()
92 if not videos:
93 logger.error('%s --> FAILED getting the %s videos' % (title, lang))
94 continue
95 if 'results' not in videos:
96 if 'status_message' in videos:
97 logger.error('%s --> %s: %s' % (title, lang, videos['status_message']))
98 else:
99 logger.error('%s --> %s: no results' % (title, lang))
100 continue
101 if videos['results']:
102 for video in videos['results']:
103 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':
104 plex_folder = EXTRAS[video['type']]
105 plex_folder_path = join(full_directory, plex_folder)
106 if not os.path.isdir(plex_folder_path):
107 os.mkdir(plex_folder_path)
108 output_filename = video['name'].replace("/", "", 100)
109 #output_filename = video['name'].translate ({ord(c): " " for c in "!@#$%^&*()[]{};:,./<>?\|`~-=_+"})
110 if not any(fname.startswith(output_filename) for fname in os.listdir(plex_folder_path)):
111 logger.info('Downloading %s %s %s %s' % (title, plex_folder, lang, output_filename))
112 output_filepath = '%s.%%(ext)s' % join(plex_folder_path, output_filename)
113 youtube_output = subprocess.run(['youtube-dl', video['key'], '--output', output_filepath, '--all-subs'])
114 if youtube_output.returncode == 0:
115 logger.info('%s --> %s / (%s) %s' % (title, plex_folder, lang, output_filename))
116 else:
117 logger.error('%s --> %s / (%s) %s !!! YOUTUBE-DL ERROR !!!' % (title, plex_folder, lang, output_filename))
118 else:
119 logger.debug('%s --> %s / (%s) %s ALREADY EXISTS' % (title, plex_folder, lang, output_filename))
120 else:
121 logger.warning('%s --> %s: no results' % (title, lang))
122 else:
123 logger.warning('%s --> !!! TMDB ID NOT FOUND !!!' % directory)