Skip to content

Commit

Permalink
修正系列名称获取
Browse files Browse the repository at this point in the history
添加强制重命名参数
  • Loading branch information
Nriver committed May 10, 2022
1 parent 1fe6b89 commit 51c42d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
32 changes: 26 additions & 6 deletions EpisodeReName.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def resource_path(relative_path):
rename_delay = int(sys.argv[2])
logger.info(f"{'rename_delay', rename_delay}")
name_format = 'S{season}E{ep}'

force_rename = 0
else:
# 新的argparse解析
# python EpisodeReName.py --path E:\test\极端试验样本\S1 --delay 1 --overwrite 1
Expand All @@ -94,11 +94,14 @@ def resource_path(relative_path):
ap.add_argument('--name_format', required=False,
help='(慎用) 自定义重命名格式, 参数需要加引号 默认为 "S{season}E{ep}" 可以选择性加入 系列名称如 "{series} - S{season}E{ep}" ',
default='S{season}E{ep}')
ap.add_argument('--force_rename', required=False, help='(慎用) 即使已经是标准命名, 也强制重新改名, 默认为0不开启, 1是开启', type=int,
default=0)
args = vars(ap.parse_args())
target_path = args['path']
rename_delay = args['delay']
rename_overwrite = args['overwrite']
name_format = args['name_format']
force_rename = args['force_rename']

if not target_path:
# 没有路径参数直接退出
Expand Down Expand Up @@ -254,6 +257,16 @@ def get_season_cascaded(full_path):
return season


def get_series_from_season_path(season_path):
"""修正系列名称获取 去掉结尾的年份"""
series = os.path.basename(os.path.dirname(season_path))
pat = '\(\d{4}\)$'
res = re.search(pat, series)
if res:
series = series[:-6].strip()
return series


def get_season_and_ep(file_path):
logger.info(f"{'解析文件', file_path}")
season = None
Expand All @@ -273,10 +286,17 @@ def get_season_and_ep(file_path):
return None, None

# 忽略已按规则命名的文件
pat = 'S\d{1,4}E\d{1,4}(\.5)?'
if re.match(pat, file_name):
pat = 'S(\d{1,4})E(\d{1,4}(\.5)?)'
res = re.match(pat, file_name)
if res:
logger.info(f"{'忽略'}")
return None, None
if force_rename:
season, ep = res[1], res[2]
season = str(int(season)).zfill(2)
ep = str(int(ep)).zfill(2)
return season, ep
else:
return None, None

# 如果文件已经有 S01EP01 或者 S01E01 直接读取
pat = '[Ss](\d{1,4})[Ee](\d{1,4}(\.5)?)'
Expand Down Expand Up @@ -611,7 +631,7 @@ def ep_offset_patch(file_path, ep):
ep = ep_offset_patch(file_path, ep)
season_path = get_season_path(file_path)
# 系列名称
series = os.path.basename(os.path.dirname(season_path))
series = get_series_from_season_path(season_path)
# new_name = f'S{season}E{ep}' + '.' + fix_ext(ext)
new_name = name_format.format(**locals()) + '.' + fix_ext(ext)
logger.info(f'{new_name}')
Expand All @@ -637,7 +657,7 @@ def ep_offset_patch(file_path, ep):
ep = ep_offset_patch(file_path, ep)
season_path = get_season_path(file_path)
# 系列名称
series = os.path.basename(os.path.dirname(season_path))
series = get_series_from_season_path(season_path)
# new_name = f'S{season}E{ep}' + '.' + fix_ext(ext)
new_name = name_format.format(**locals()) + '.' + fix_ext(ext)
logger.info(f'{new_name}')
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ D:\Test\EpisodeReName.exe -h
强制重命名, 默认为1开启覆盖模式, 0为不覆盖, 遇到同名文件会跳过, 结果输出到error.txt
--name_format NAME_FORMAT
(慎用) 自定义重命名格式, 参数需要加引号 默认为 "S{season}E{ep}" 可以选择性加入 series系列名称 如 "{series} - S{season}E{ep}"
--force_rename FORCE_RENAME
(慎用) 即使已经是标准命名, 也强制重新改名, 默认为0不开启, 1是开启
```

# 使用场景4 - Linux终端运行
Expand Down

0 comments on commit 51c42d0

Please sign in to comment.