Skip to content

Commit

Permalink
Enable synonyms
Browse files Browse the repository at this point in the history
Fixes #82
  • Loading branch information
reconman committed Nov 6, 2021
1 parent 7ff4fab commit 0cbb22f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 36 deletions.
2 changes: 1 addition & 1 deletion PlexAniSync.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import plexmodule
import graphql

__version__ = "1.3.14"
__version__ = "1.3.15"

# Logger settings
LOG_FILENAME = "PlexAniSync.log"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ https://anilist.co/anime/99263/Tate-no-Yuusha-no-Nariagari
- You can remove any existing entries from the example file as they are purely instructional
- Upon startup it will check if the file is a valid YAML file. The most likely reason it's not is because you didn't put quotes around an anime title with special characters (e.g. ":") in it.

#### Community mappings

There are some mappings provided by the Github community at https://github.com/RickDB/PlexAniSync-Custom-Mappings/. For now you can use the mapping files by copying parts into your own mapping file.

The feature of synonyms was introduced for the community mappings where you can specify that a show can have one of multiple titles but should be mapped the same way. See Shaman King (2021) in the example mapping file.

### Custom settings file location

If you want to load a different settings.in file you can do so by supplying it in the first argument like so:
Expand Down
2 changes: 1 addition & 1 deletion TautulliSyncHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import plexmodule
import graphql

__version__ = "1.3.14"
__version__ = "1.3.15"

# Logger settings
logger = logging.getLogger("PlexAniSync")
Expand Down
8 changes: 4 additions & 4 deletions anilist.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ def match_to_plex(anilist_series: List[AnilistSeries], plex_series_watched: List
if season_mappings:
watchcounts = map_watchcount_to_seasons(plex_title, season_mappings, plex_season.watched_episodes)

for anime_id in watchcounts:
for anime_id, watchcount in watchcounts.items():
logger.info(
f"[ANILIST] Used custom mapping | title: {plex_title} | season: {season_number} | anilist id: {anime_id}"
)

add_or_update_show_by_id(anilist_series, plex_title, plex_year, True, watchcounts[anime_id], anime_id)
add_or_update_show_by_id(anilist_series, plex_title, plex_year, True, watchcount, anime_id)

# If custom match found continue to next
continue
Expand Down Expand Up @@ -376,11 +376,11 @@ def match_to_plex(anilist_series: List[AnilistSeries], plex_series_watched: List
if season_mappings:
watchcounts = map_watchcount_to_seasons(plex_title, season_mappings, plex_season.watched_episodes)

for anime_id in watchcounts:
for anime_id, watchcount in watchcounts.items():
logger.info(
f"[ANILIST] Used custom mapping | title: {plex_title} | season: {season_number} | anilist id: {anime_id}"
)
add_or_update_show_by_id(anilist_series, plex_title, plex_year, True, watchcounts[anime_id], anime_id)
add_or_update_show_by_id(anilist_series, plex_title, plex_year, True, watchcount, anime_id)

# If custom match found continue to next
continue
Expand Down
32 changes: 23 additions & 9 deletions custom_mappings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# coding=utf-8
import os
import logging
import sys
from typing import List
from dataclasses import dataclass

from ruyaml import YAML
import yamale
from yamale.yamale_error import YamaleError


logger = logging.getLogger("PlexAniSync")
Expand All @@ -24,24 +26,36 @@ def read_custom_mappings():
logger.info(f"[MAPPING] Custom map file not found: {MAPPING_FILE}")
else:
logger.info(f"[MAPPING] Custom map file found: {MAPPING_FILE}")
file = open(MAPPING_FILE, "r", encoding="utf-8")
yaml = YAML(typ='safe')
file_mappings = yaml.load(file)

for file_entry in file_mappings['entries']:
schema = yamale.make_schema('./custom_mappings_schema.yaml', parser='ruamel')

# Create a Data object
file_mappings = yamale.make_data(MAPPING_FILE, parser='ruamel')

try:
# Validate data against the schema same as before.
yamale.validate(schema, file_mappings)
except YamaleError as e:
logger.error('Custom Mappings validation failed!\n')
for result in e.results:
for error in result.errors:
logger.error(f"{error}\n")
sys.exit(1)

for file_entry in file_mappings[0][0]['entries']:
series_title = str(file_entry['title']).lower()
synonyms = file_entry.get('synonyms', [])
series_mappings: List[AnilistCustomMapping] = []
for file_season in file_entry['seasons']:
season = file_season['season']
anilist_id = file_season['anilist-id']
start = 1
if 'start' in file_season:
start = file_season['start']
start = file_season.get('start', 1)

logger.info(
f"[MAPPING] Adding custom mapping | title: {file_entry['title']} | season: {season} | anilist id: {anilist_id} | start: {start}"
)
series_mappings.append(AnilistCustomMapping(season, anilist_id, start))

custom_mappings[series_title] = series_mappings
for synonym in synonyms:
custom_mappings[synonym] = series_mappings
return custom_mappings
8 changes: 7 additions & 1 deletion custom_mappings.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ entries:
start: 1
- season: 10
anilist-id: 101925
start: 13
start: 13
- title: 'Shaman King (2021)'
synonyms:
- 'Shaman King'
seasons:
- season: 1
anilist-id: 119675
11 changes: 11 additions & 0 deletions custom_mappings_schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
entries: list(include('entry'), min=1)
---
entry:
title: str()
synonyms: list(str(), required=False)
seasons: list(include('season'), min=1)

season:
season: int(min=1)
anilist-id: int(min=1)
start: int(required=False)
41 changes: 21 additions & 20 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
babelfish>=0.5.5
beautifulsoup4>=4.9.3
certifi>=2020.12.5
chardet>=4.0.0
colorama>=0.4.4
coloredlogs>=15.0
guessit>=3.3.0
humanfriendly>=9.1
idna>=2.10
inflect>=5.0.2
PlexAPI>=4.5.2
pyreadline>=2.1
python-dateutil>=2.8.1
rebulk>=3.0.1
requests>=2.25.1
ruyaml>=0.20.0
six>=1.15.0
tqdm>=4.56.0
urllib3>=1.26.3
websocket-client>=0.57.0
-babelfish>=0.5.5
-beautifulsoup4>=4.9.3
-certifi>=2020.12.5
-chardet>=4.0.0
-colorama>=0.4.4
-coloredlogs>=15.0
-guessit>=3.3.0
-humanfriendly>=9.1
-idna>=2.10
-inflect>=5.0.2
-PlexAPI>=4.5.2
-pyreadline>=2.1
-python-dateutil>=2.8.1
-rebulk>=3.0.1
-requests>=2.25.1
-ruamel.yaml>=0.17.17
-six>=1.15.0
-tqdm>=4.56.0
-urllib3>=1.26.3
-websocket-client>=0.57.0
-yamale>=4.0.2

0 comments on commit 0cbb22f

Please sign in to comment.