working version of sonarr_translation
translate all episodes in root folder only qurey present seasons optimize code
This commit is contained in:
1 parent
24b12ec6d6
commit
ab80645034
1 file changed
+159
-77
+159
-77
@@ -28,8 +28,8 @@ HEADERS = {
|
|||||||
|
|
||||||
def tmdb_get(endpoint, params=None):
|
def tmdb_get(endpoint, params=None):
|
||||||
"""Perform a GET request against TMDb."""
|
"""Perform a GET request against TMDb."""
|
||||||
|
|
||||||
url = BASE_URL + endpoint
|
url = BASE_URL + endpoint
|
||||||
|
print(f"Set of Tmdb query: {url}")
|
||||||
|
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
url,
|
url,
|
||||||
@@ -124,16 +124,21 @@ def get_season(tmdb_id, season_number):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_episode_list(show):
|
def get_episode_list(show,series_path: Path):
|
||||||
#_, show = find_by_tmdb_id(tmdb_id)
|
#_, show = find_by_tmdb_id(tmdb_id)
|
||||||
|
file_seasons = sorted({
|
||||||
|
int(m.group(1))
|
||||||
|
for d in series_path.iterdir()
|
||||||
|
if d.is_dir()
|
||||||
|
and (m := re.match(r"^Season\s+0*(\d+)$", d.name, re.I))
|
||||||
|
})
|
||||||
episodes = []
|
episodes = []
|
||||||
|
|
||||||
for season in show.get("seasons", []):
|
for season in show.get("seasons", []):
|
||||||
season_number = season["season_number"]
|
season_number = season["season_number"]
|
||||||
|
|
||||||
# skip specials if desired
|
# skip specials if desired
|
||||||
if season_number == 0:
|
if season_number == 0 or season_number not in file_seasons:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
season_data = get_season(show.get("id"), season_number)
|
season_data = get_season(show.get("id"), season_number)
|
||||||
@@ -217,111 +222,188 @@ def best_episode_match(filename, episodes,title,year):
|
|||||||
|
|
||||||
return best, best_score
|
return best, best_score
|
||||||
|
|
||||||
def main():
|
def rename_media(media_data,episodes, series_path,file_title):
|
||||||
|
series=series_path.name
|
||||||
|
episode_map = {
|
||||||
|
(ep["season"], ep["episode"]): ep
|
||||||
|
for ep in episodes
|
||||||
|
}
|
||||||
|
file_list = []
|
||||||
|
new_file_list = []
|
||||||
|
for file in series_path.rglob("*"):
|
||||||
|
match_SE_name = re.search(r"S(\d{2})E(\d{2})", file.name, re.I)
|
||||||
|
if not match_SE_name:
|
||||||
|
match_season_folder = re.search(r"Season (\d{2})", file.name, re.I)
|
||||||
|
if match_season_folder:
|
||||||
|
continue # witchout bothering anyone
|
||||||
|
print(f"No season/episode number found: {file.name}")
|
||||||
|
continue
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
season = int(match_SE_name.group(1))
|
||||||
parser.add_argument("series", help='Series name, e.g. "Maya the Bee (1975) [10620]"')
|
episode= int(match_SE_name.group(2))
|
||||||
|
|
||||||
args = parser.parse_args()
|
episode_number = episode_map.get((season, episode))
|
||||||
|
|
||||||
if len(sys.argv) != 2 or not Path(args.series).is_dir():
|
if not episode_number:
|
||||||
print("Usage:")
|
print(f"Episode not found: S{season:02d}E{episode:02d}")
|
||||||
print()
|
continue
|
||||||
print(" python tmdb_lookup.py <Series Folder>")
|
episode_season_number_match = re.match(
|
||||||
print(" Seires Folder needs to be this Style: Maya the Bee (1975) [10620]")
|
r"^(.*?S\d{2}E\d{2})\s*-\s*(.*?)\s*(\[[^\]]+\].*)?$",
|
||||||
sys.exit(1)
|
file.stem,
|
||||||
|
re.I,
|
||||||
|
)
|
||||||
|
if not episode_season_number_match:
|
||||||
|
print(f"S{season:02d}E{episode:02d} not found")
|
||||||
|
continue
|
||||||
|
prefix = episode_season_number_match.group(1).replace(file_title,(media_data.get('name')
|
||||||
|
or media_data.get('title'))) # Andor (2022) - S01E07
|
||||||
|
quality = episode_season_number_match.group(3) or "" # [WEBRip-1080p][AAC 5.1][h265]
|
||||||
|
|
||||||
series_path = Path(args.series)
|
new_name = (
|
||||||
|
f"{prefix} - {episode_number['title']}"
|
||||||
|
f"{(' ' + quality) if quality else ''}"
|
||||||
|
f"{file.suffix}"
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f"{file.name} -> {new_name}")
|
||||||
|
|
||||||
|
file_list.append(file)
|
||||||
|
new_file_list.append(new_name)
|
||||||
|
|
||||||
|
title=(f"{(media_data.get('name') or media_data.get('title'))} "
|
||||||
|
f"({(media_data.get('first_air_date') or media_data.get('release_date') or '')[:4]}) "
|
||||||
|
f"[{media_data.get('id')}]")
|
||||||
|
print(f"{series} -> {title}")
|
||||||
|
|
||||||
|
file_list.append(series_path)
|
||||||
|
new_file_list.append(title)
|
||||||
|
has_changes = False
|
||||||
|
for file, new_file in zip(file_list, new_file_list):
|
||||||
|
if file.name != new_file:
|
||||||
|
has_changes = True
|
||||||
|
break
|
||||||
|
if not has_changes:
|
||||||
|
print("No changes necessary.")
|
||||||
|
return
|
||||||
|
answer = input("Rename files? [y/N]: ").strip().lower()
|
||||||
|
if answer in ("y", "yes"):
|
||||||
|
print("Proceeding...")
|
||||||
|
for file, new_file in zip(file_list,new_file_list):
|
||||||
|
file.rename(file.with_name(new_file))
|
||||||
|
print("All files renamed")
|
||||||
|
return
|
||||||
|
print("Cancelled.")
|
||||||
|
return
|
||||||
|
|
||||||
|
def process_series(series_path: Path):
|
||||||
series = series_path.name
|
series = series_path.name
|
||||||
|
|
||||||
m = re.match(r"^(.*?)\s*\((\d{4})\)\s*\[(\d+)\]$", series)
|
m = re.match(r"^(.*?)\s*\((\d{4})\)\s*\[(\d+)\]$", series)
|
||||||
|
|
||||||
if not m:
|
if not m:
|
||||||
parser.error(f"Invalid series format: {args.series}")
|
print(f"Skipping invalid folder: {series}")
|
||||||
|
return
|
||||||
|
|
||||||
file_title = m.group(1).strip()
|
file_title = m.group(1).strip()
|
||||||
file_year = int(m.group(2))
|
file_year = int(m.group(2))
|
||||||
file_tmdb_id = int(m.group(3))
|
file_tmdb_id = int(m.group(3))
|
||||||
|
|
||||||
|
print()
|
||||||
|
print("=" * 80)
|
||||||
print(f"TITLE={file_title}")
|
print(f"TITLE={file_title}")
|
||||||
print(f"YEAR={file_year}")
|
print(f"YEAR={file_year}")
|
||||||
print(f"TMDB_ID={file_tmdb_id}")
|
print(f"TMDB_ID={file_tmdb_id}")
|
||||||
|
|
||||||
if file_tmdb_id == 0:
|
if file_tmdb_id == 0:
|
||||||
print("Tmdb ID required")
|
print("TMDB ID required")
|
||||||
exit(1)
|
return
|
||||||
|
|
||||||
print(f"Looking up IMDb ID: {file_tmdb_id}")
|
try:
|
||||||
|
data = find_by_tmdb_id(file_tmdb_id)
|
||||||
|
|
||||||
data = find_by_tmdb_id(file_tmdb_id)
|
if not data:
|
||||||
media_type, media_data = data
|
print("TMDB lookup failed")
|
||||||
|
return
|
||||||
|
|
||||||
print_results(data)
|
media_type, media_data = data
|
||||||
|
|
||||||
episodes = get_episode_list(media_data)
|
print_results(data)
|
||||||
episode_map = {
|
|
||||||
(ep["season"], ep["episode"]): ep
|
|
||||||
for ep in episodes
|
|
||||||
}
|
|
||||||
|
|
||||||
for file in series_path.rglob("*"):
|
episodes = get_episode_list(media_data)
|
||||||
m = re.search(r"S(\d{2})E(\d{2})", file.name, re.I)
|
|
||||||
if not m:
|
|
||||||
m_b = re.search(r"Season (\d{2})", file.name, re.I)
|
|
||||||
if m_b:
|
|
||||||
continue
|
|
||||||
print(f"No season/episode number found: {file.name}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
season = int(m.group(1))
|
rename_media(
|
||||||
episode= int(m.group(2))
|
media_data,
|
||||||
|
episodes,
|
||||||
|
series_path,
|
||||||
|
file_title
|
||||||
|
)
|
||||||
|
|
||||||
ep = episode_map.get((season, episode))
|
except Exception as e:
|
||||||
file_list = []
|
print(f"Error processing {series}: {e}")
|
||||||
new_file_list = []
|
|
||||||
if ep:
|
|
||||||
m = re.match(
|
|
||||||
r"^(.*?S\d{2}E\d{2})\s*-\s*(.*?)\s*(\[[^\]]+\].*)?$",
|
|
||||||
file.stem,
|
|
||||||
re.I,
|
|
||||||
)
|
|
||||||
|
|
||||||
if m:
|
def process_series(series_path: Path):
|
||||||
prefix = m.group(1) # Andor (2022) - S01E07
|
series = series_path.name
|
||||||
quality = m.group(3) or "" # [WEBRip-1080p][AAC 5.1][h265]
|
|
||||||
|
|
||||||
new_name = (
|
m = re.match(r"^(.*?)\s*\((\d{4})\)\s*\[(\d+)\]$", series)
|
||||||
f"{prefix} - {ep['title']}"
|
|
||||||
f"{(' ' + quality) if quality else ''}"
|
|
||||||
f"{file.suffix}"
|
|
||||||
)
|
|
||||||
|
|
||||||
print(
|
if not m:
|
||||||
f"{file.name} -> "
|
print(f"Skipping invalid folder: {series}")
|
||||||
f"{new_name}"
|
return
|
||||||
)
|
|
||||||
|
|
||||||
file_list.append(file)
|
file_title = m.group(1).strip()
|
||||||
new_file_list.append(new_name)
|
#file_year = int(m.group(2))
|
||||||
else:
|
file_tmdb_id = int(m.group(3))
|
||||||
print(
|
|
||||||
f"Episod* not found: "
|
|
||||||
f"S{season:02d}E{episode:02d}"
|
|
||||||
)
|
|
||||||
title=(f"{(data.get('name') or data.get('title'))} "
|
|
||||||
f"({(data.get('first_air_date') or data.get('release_date') or '')[:4]})")
|
|
||||||
print(f"{series} -> {title}")
|
|
||||||
|
|
||||||
answer = input("Rename files? [y/N]: ").strip().lower()
|
if file_tmdb_id == 0:
|
||||||
if answer in ("y", "yes"):
|
print("TMDB ID required")
|
||||||
print("Proceeding...")
|
return
|
||||||
|
|
||||||
file_list.append(series_path)
|
try:
|
||||||
new_file_list.append(title)
|
data = find_by_tmdb_id(file_tmdb_id)
|
||||||
for file, new_file in zip(file_list,new_file_list):
|
|
||||||
file.rename(file.with_name(new_file))
|
|
||||||
|
|
||||||
else:
|
if not data:
|
||||||
print("Cancelled.")
|
print("TMDB lookup failed")
|
||||||
|
return
|
||||||
|
|
||||||
|
media_type, media_data = data
|
||||||
|
|
||||||
|
#print_results(data)
|
||||||
|
|
||||||
|
episodes = get_episode_list(media_data,series_path)
|
||||||
|
|
||||||
|
rename_media(
|
||||||
|
media_data,
|
||||||
|
episodes,
|
||||||
|
series_path,
|
||||||
|
file_title
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error processing {series}: {e}")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument(
|
||||||
|
"path",
|
||||||
|
help='Either a series folder "Maya the Bee (1975) [10620]" or a root folder containing such folders'
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
input_path = Path(args.path)
|
||||||
|
|
||||||
|
if not input_path.is_dir():
|
||||||
|
parser.error(f"Directory not found: {input_path}")
|
||||||
|
|
||||||
|
# Input is already a series folder
|
||||||
|
if re.match(r"^(.*?)\s*\((\d{4})\)\s*\[(\d+)\]$", input_path.name):
|
||||||
|
process_series(input_path)
|
||||||
|
|
||||||
|
# Input is a root folder containing series folders
|
||||||
|
else:
|
||||||
|
for series_path in sorted(input_path.iterdir()):
|
||||||
|
if series_path.is_dir():
|
||||||
|
process_series(series_path)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in new issue
Block a user