This repository was archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
140 lines (115 loc) · 7.03 KB
/
main.py
File metadata and controls
140 lines (115 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import re
import shutil
import sqlite3
from pathlib import Path
movieboxpro = Path('D:\\batch1')
subtitle_cache = Path(str(movieboxpro) + '\\Subtitle_Cache\\Subtitle_My_Cache')
download_database = str(movieboxpro) + '\\Download.db'
movies, show, subs, suberror = 0, 0, 0, 0
# change subtitle language here, valid codes found here https://registry-page.isdcf.com/languages/
subtitle_language = 'en'
def clean_title(title):
""" Turns Goodfellas_1234_1080p.mp4 into Goodfellas """
media = re.search(r'^(.*?)_[0-9]+_[^_]+$', title)
if media:
return media.group(1).replace('_', '')
def get_subtitles(media_path, media_id):
""" Returns the media files year and copies found subtitles to the main folder. Requires Download.db from MovieBoxPro. """
media_list = read_download_db(download_database)
year, subtitles = get_media_details(media_id, media_list)
for subdir in subtitle_cache.glob('*'):
if subdir.is_dir() and media_id in subdir.name:
try:
shutil.copy2(str(subdir) + '\\' + subtitle_language + '\\' + subtitles, str(media_path).replace(media_path.name, clean_title(media_path.name) + ' (' + year + ').srt'))
except FileNotFoundError:
continue
return ' (' + year + ')', str(media_path).replace(media_path.name, clean_title(media_path.name) + ' (' + year + ').srt')
def get_subtitles_show(media_path, media_id, season, episode):
media_list = read_download_db(download_database)
season_num, episode_num, episode_title, subtitles = get_media_details(media_id, media_list, season, episode, True)
for subdir in subtitle_cache.glob('*'):
if subdir.is_dir() and media_id in subdir.name:
try:
shutil.copy2(str(subdir) + '\\Season ' + str(season_num) + '\\Episode ' + str(episode_num) + '\\' + subtitle_language + '\\' + subtitles, str(media_path).replace(media_path.name, re.sub(r'[?/:\\*<>|]', '', episode_title) + ' S' + str(season_num).zfill(2) + 'E' + str(episode_num).zfill(2) + '.srt'))
except FileNotFoundError:
continue
return season_num, episode_num, episode_title, str(media_path).replace(media_path.name, re.sub(r'[?/:\\*<>|]', '', episode_title) + ' S' + str(season_num).zfill(2) + 'E' + str(episode_num).zfill(2) + '.srt')
def get_media_details(media_id, media_list, season_num = None, episode_num = None, is_tv=False):
""" Returns various information on the media file. """
for m in media_list:
if media_id in str(m[0]):
if is_tv:
# m[10] = season number, m[11] = episode number, m[13] = episode title, m[18] = prioritized subtitle file
if m[10] is None or m[11] is None or m[13] is None or m[18] is None or season_num is None or episode_num is None: continue
if m[10] == int(season_num) and m[11] == int(episode_num):
return m[10], m[11], m[13], m[18]
else:
# m[1] = url containing the year of the media, m[18] = prioritized subtitle file
if m[1] is None or m[18] is None: continue
try:
return str(m[1]).split('/movie.' + media_id + '.')[1][:4], m[18]
except IndexError:
print('Potential error with media id: ' + media_id + '. Recommended to check year and subtitle file.')
continue
def read_download_db(database):
""" Reads MovieBoxPro Download.db file """
db_con = sqlite3.connect(database)
cursor = db_con.cursor()
media_list = [m for m in cursor.execute('SELECT * FROM Download')]
db_con.close()
return media_list
if not movieboxpro.exists(): exit('MovieBoxPro directory does not exist or has not been found!')
if not Path(download_database).exists(): exit('Download.db file does not exist or has not been found!')
# the mistake I just made that warranted these checks to avoid future problems (and tedious work)..
if not subtitle_cache.exists():
if input('Subtitles directory does not exist or has not be found, continue? (y or any): ') != 'y':
exit('Subtitle directory not found!')
for main in movieboxpro.glob('*'):
if main.is_dir() and '_' in main.name:
if 'Subtitle_Cache' in main.name: continue
media_id = main.name.rsplit('_', 1)[-1]
for seasons in main.iterdir():
if seasons.is_dir() and '_' in seasons.name:
for episodes in seasons.glob('*'):
if '.plist' in episodes.name:
os.remove(str(episodes))
continue
elif '.mp4' in episodes.name:
try:
season_num, episode_num, episode_title, tpath = get_subtitles_show(episodes, media_id, re.search(r'\d+', seasons.name).group(), episodes.name.replace('.mp4', ''))
print('Converting: ' + episodes.name, media_id, 'S' + str(season_num).zfill(2) + 'E' + str(episode_num).zfill(2), episode_title)
if Path(tpath).is_file():
subs += 1
else:
suberror += 1
print('Subtitle Error: ', media_id, 'S' + str(season_num).zfill(2) + 'E' + str(episode_num).zfill(2), episode_title)
episodes.rename(str(episodes).replace(episodes.name, re.sub(r'[?/:\\*<>|]', '', episode_title) + ' S' + str(season_num).zfill(2) + 'E' + str(episode_num).zfill(2) + '.mp4'))
show += 1
except FileExistsError:
continue
seasons.rename(str(seasons).replace(seasons.name, 'Season ' + re.search(r'\d+', seasons.name).group().zfill(2)))
# unlike movies there is no actual way to retrieve the shows year from Download.db, so that will have to be manual still...
try:
main.rename(str(main).replace(main.name, main.name.rsplit('_', 1)[0]))
except PermissionError:
continue
elif main.is_file() and '_' in main.name and '.mp4' in main.name:
try:
media_id = re.search(r'_(\d+)_\d+p', main.name).group(1)
year, tpath = get_subtitles(main, media_id)
print('Converting: ' + main.name, media_id, year.replace('(','').replace(')',''))
if Path(tpath).is_file():
subs += 1
else:
suberror += 1
print('Subtitle Error: ', media_id, clean_title(main.name), year.replace('(','').replace(')',''))
main.rename(str(main).replace(main.name, clean_title(main.name) + year + '.mp4'))
movies += 1
except FileExistsError:
continue
print('\nMovies Converted: ' + str(movies))
print('Episodes Converted: ' + str(show))
print('Total Media Converted: ' + str(show + movies))
print('\nSubtitles Found & Converted: ' + str(subs))
print('Subtitle Errors: ' + str(suberror))