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.
67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
import os, re, click
|
|
|
|
class FileProperties():
|
|
|
|
FILE_EXTENSIONS = ['mkv', 'mp4', 'avi', 'flv', 'webm']
|
|
|
|
SEASON_EPISODE_INDICATOR_MATCH = '[sS]([0-9]+)[eE]([0-9]+)'
|
|
EPISODE_INDICATOR_MATCH = '[eE]([0-9]+)'
|
|
|
|
def ___init__(self, sourcePath, ):
|
|
|
|
|
|
# Separate basedir, basename and extension for current source file
|
|
self.__sourceDirectory = os.path.dirname(sourcePath)
|
|
self.__sourceFilename = os.path.basename(sourcePath)
|
|
sourcePathTokens = self.__sourceFilename.split('.')
|
|
|
|
if sourcePathTokens[-1] in FilenameController.FILE_EXTENSIONS:
|
|
self.__sourceFileBasename = '.'.join(sourcePathTokens[:-1])
|
|
self.__sourceFilenameExtension = sourcePathTokens[-1]
|
|
else:
|
|
self.__sourceFileBasename = self.__sourceFilename
|
|
self.__sourceFilenameExtension = ''
|
|
|
|
|
|
se_match = re.compile(FilenameController.SEASON_EPISODE_INDICATOR_MATCH)
|
|
e_match = re.compile(FilenameController.EPISODE_INDICATOR_MATCH)
|
|
|
|
se_result = se_match.search(self.__sourceFilename)
|
|
e_result = e_match.search(self.__sourceFilename)
|
|
|
|
self.__season = -1
|
|
self.__episode = -1
|
|
file_index = 0
|
|
|
|
if se_result is not None:
|
|
self.__season = int(se_result.group(1))
|
|
self.__episode = int(se_result.group(2))
|
|
elif e_result is not None:
|
|
self.__episode = int(e_result.group(1))
|
|
else:
|
|
file_index += 1
|
|
|
|
|
|
|
|
matchingFileSubtitleDescriptors = sorted([d for d in availableFileSubtitleDescriptors if d['season'] == season and d['episode'] == episode], key=lambda d: d['stream']) if availableFileSubtitleDescriptors else []
|
|
|
|
print(f"season={season} episode={episode} file={file_index}")
|
|
|
|
|
|
# Assemble target filename tokens
|
|
targetFilenameTokens = []
|
|
targetFilenameExtension = DEFAULT_FILE_EXTENSION
|
|
|
|
if label:
|
|
targetFilenameTokens = [label]
|
|
|
|
if season > -1 and episode > -1:
|
|
targetFilenameTokens += [f"S{season:0{season_digits}d}E{episode:0{episode_digits}d}"]
|
|
elif episode > -1:
|
|
targetFilenameTokens += [f"E{episode:0{episode_digits}d}"]
|
|
else:
|
|
targetFilenameTokens += [f"{file_index:0{index_digits}d}"]
|
|
|
|
else:
|
|
targetFilenameTokens = [sourceFileBasename]
|