Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Poster renamerr alternate match fix #145

Merged
merged 2 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.5
1.0.6
73 changes: 42 additions & 31 deletions modules/poster_renamerr.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ def get_assets_files(source_dirs, logger):

return final_assets

def handle_series_match(asset, media_seasons_numbers, asset_season_numbers):
# Iterate through each file in the asset
files_to_remove = []
seasons_to_remove = []
for file in asset['files']:
# Check for season-related file naming
if re.search(r' - Season| - Specials', file):
if re.search(r"Season (\d+)", file):
season_number = int(re.search(r"Season (\d+)", file).group(1))
elif "Specials" in file:
season_number = 0
if season_number not in media_seasons_numbers:
files_to_remove.append(file)
continue
for file in files_to_remove:
asset['files'].remove(file)
for season in asset_season_numbers:
if season not in media_seasons_numbers:
seasons_to_remove.append(season)
for season in seasons_to_remove:
asset_season_numbers.remove(season)

def match_data(media_dict, asset_files):
"""
Matches media data to asset files
Expand Down Expand Up @@ -124,7 +146,7 @@ def match_data(media_dict, asset_files):
asset_data = asset_files[asset_type]
media_data = media_dict[asset_type]
# Iterate through each media entry of the current asset type
with tqdm(total=len(media_data), desc=f"Matching {asset_type}", unit="media", leave=True, disable=None) as pbar_inner:
with tqdm(total=len(media_data), desc=f"Matching {asset_type}", leave=True, disable=None) as pbar_inner:
for media in media_data:
matched = False
if asset_type == 'series':
Expand All @@ -136,36 +158,25 @@ def match_data(media_dict, asset_files):
matched = True # Set flag to indicate a match
asset_season_numbers = asset.get('season_numbers', None)
if asset_type == "series":
# Iterate through each file in the asset
files_to_remove = []
seasons_to_remove = []
for file in asset['files']:
# Check for season-related file naming
if re.search(r' - Season| - Specials', file):
if re.search(r"Season (\d+)", file):
season_number = int(re.search(r"Season (\d+)", file).group(1))
elif "Specials" in file:
season_number = 0
if season_number not in media_seasons_numbers:
files_to_remove.append(file)
continue
for file in files_to_remove:
asset['files'].remove(file)
for season in asset_season_numbers:
if season not in media_seasons_numbers:
seasons_to_remove.append(season)
for season in seasons_to_remove:
asset_season_numbers.remove(season)

# Store matched data in the matched dictionary
matched_dict.append({
'title': media['title'],
'year': media['year'],
'folder': media['folder'],
'files': asset['files'],
'seasons_numbers': asset_season_numbers,
})
break # Break loop after finding a match
handle_series_match(asset, media_seasons_numbers, asset_season_numbers)
break
if not matched:
for asset in asset_data:
if is_match_alternate(asset, media):
matched = True
asset_season_numbers = asset.get('season_numbers', None)
if asset_type == "series":
handle_series_match(asset, media_seasons_numbers, asset_season_numbers)
break

if matched:
matched_dict.append({
'title': media['title'],
'year': media['year'],
'folder': media['folder'],
'files': asset['files'],
'seasons_numbers': asset_season_numbers,
})

if not matched:
# If no match is found, add to unmatched dictionary
Expand Down
38 changes: 36 additions & 2 deletions util/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,6 @@ def is_match(asset, media):
if (
asset['title'] == media['title'] or
asset['normalized_title'] == media['normalized_title'] or
asset['title'] in alternate_titles or
asset['normalized_title'] in normalized_alternate_titles or
asset['title'] == original_title or
asset['title'] == folder_title or
asset['normalized_title'] == normalized_folder_title or
Expand All @@ -881,3 +879,39 @@ def is_match(asset, media):
return True
else:
return False

def is_match_alternate(asset, media):
"""
Check if the asset matches the media using alternate titles

Args:
asset (dict): The asset to check
media (dict): The media to check

Returns:
bool: True if the asset matches the media, False otherwise
"""
alternate_titles = media.get('alternate_titles', [])
normalized_alternate_titles = media.get('normalized_alternate_titles', [])
secondary_year = media.get('secondary_year', None)
folder = media.get('folder', None)
folder_year = None
if folder:
folder_base_name = os.path.basename(folder)
match = re.search(folder_year_regex, folder_base_name)
if match:
folder_title, folder_year = match.groups()
folder_year = int(folder_year)

# Matching criteria for media and asset
if (
asset['title'] in alternate_titles or
asset['normalized_title'] in normalized_alternate_titles
) and (
asset['year'] == media['year'] or
asset['year'] == secondary_year or
asset['year'] == folder_year
):
return True
else:
return False