You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.0 KiB
Python
80 lines
2.0 KiB
Python
#!/usr/bin/python3
|
|
|
|
import os
|
|
import re
|
|
|
|
ONE_PIECE_SEASON_COUNT = [
|
|
8, # Season 1
|
|
22, # Season 2
|
|
17, # Season 3
|
|
13, # Season 4
|
|
9, # Season 5
|
|
22, # Season 6
|
|
39, # Season 7
|
|
13, # Season 8
|
|
52, # Season 9
|
|
31, # Season 10
|
|
99, # Season 11
|
|
56, # Season 12
|
|
100, # Season 13
|
|
35, # Season 14
|
|
62, # Season 15
|
|
49, # Season 16
|
|
118, # Season 17
|
|
33, # Season 18
|
|
98, # Season 19
|
|
14, # Season 20
|
|
179, # Season 21
|
|
]
|
|
|
|
ONE_PIECE_DIR = "/mnt/mergerfs/media/tv/One Piece"
|
|
|
|
|
|
def _extract_episode_number(filename):
|
|
# [HorribleSubs] One Piece - 260 [1080p].mkv -> 260
|
|
return int(re.findall(r"\s(\d+)\s", filename)[0])
|
|
|
|
|
|
def _get_episode_season(absolute_number):
|
|
season = 1
|
|
episodes_in_season = ONE_PIECE_SEASON_COUNT[season - 1]
|
|
while absolute_number > episodes_in_season:
|
|
season += 1
|
|
episodes_in_season += ONE_PIECE_SEASON_COUNT[season - 1]
|
|
return season
|
|
|
|
|
|
def _get_episode_number(season, absolute_number):
|
|
relative_number = absolute_number - sum(ONE_PIECE_SEASON_COUNT[:season - 1])
|
|
missing_episode = 590
|
|
if relative_number > missing_episode:
|
|
relative_number += 1
|
|
return relative_number
|
|
|
|
|
|
def _get_new_name(relative_episode_number, actual_season):
|
|
name = "[HorribleSubs] One Piece - S"
|
|
if actual_season < 10:
|
|
name += "0"
|
|
name += f"{actual_season}"
|
|
return name + f"E{relative_episode_number} [1080p].mkv"
|
|
|
|
|
|
def main():
|
|
# 589 -> 591
|
|
all_files = os.listdir(ONE_PIECE_DIR)
|
|
for f in all_files:
|
|
# skip files that have a season associated with them
|
|
if "S0" in f:
|
|
print(f'Skipping {f}')
|
|
continue
|
|
current_episode = _extract_episode_number(f)
|
|
episode_season = _get_episode_season(current_episode)
|
|
relative_episode_number = _get_episode_number(episode_season, current_episode)
|
|
new_name = _get_new_name(relative_episode_number, episode_season)
|
|
print(f"Renaming {f} to {new_name}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|