Skip to content

Commit

Permalink
fix: handle more filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
bugale committed Oct 16, 2024
1 parent 3f31330 commit 54b837d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
23 changes: 12 additions & 11 deletions buganime/buganime.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,34 @@ def _get_subtitle_stream_index() -> int:

def parse_filename(input_path: str) -> TVShow | Movie:
# Remove metadata in brackets/parentheses and extension (e.g. hash, resolution, etc.)
input_path = re.sub(r'[_+]', ' ', input_path)
input_path = os.path.splitext(input_path)[0]
input_path = re.sub(r'[_\+-\. ]+', ' ', input_path)
input_path = re.sub(r'\[[^\]]*\]', '', input_path)
input_path = re.sub(r'\([^\)]*\)', '', input_path)
input_path = re.sub(r'\d{3,4}p[ -][^\\]*', '', input_path)
input_path = re.sub(r'[ -]*\\[ -]*', r'\\', input_path)
input_path = os.path.splitext(input_path)[0].strip(' -')
input_path = re.sub(r'\d{3,4}p [^\\]*', '', input_path)
input_path = re.sub(r' *\\ *', r'\\', input_path)
input_path = input_path.strip(' -')

# Remove extension and directories
input_name = os.path.basename(input_path).strip(' -')
# Remove directories
input_name = os.path.basename(input_path).strip(' ')

# Special/OVAs are season 0
if match := re.match(r'^(?P<name>.+?)[ -]+(?:S(?:eason ?)?\d{1,2}[ -]+)?(?:Special|SP|OVA|OAV|Picture Drama)(?:[ -]+E?(?P<episode>\d{1,3})?)?$',
if match := re.match(r'^(?P<name>.+?) (?:S(?:eason ?)?\d{1,2} )?(?:Special|SP|OVA|OAV|Picture Drama)(?: E?(?P<episode>\d{1,3})?)?$',
input_name):
return TVShow(name=match.group('name'), season=0, episode=int(match.group('episode') or 1))

# Formatted standalone TV Shows
if match := re.match(r'^(?P<name>.+?)[ -]+S(?P<season>\d{1,2})E(?P<episode>\d{1,3})(?:[ -]+.*)?$', input_name):
if match := re.match(r'^(?P<name>.+?) S(?P<season>\d{1,2})E(?P<episode>\d{1,3})(?: .*)?$', input_name):
return TVShow(name=match.group('name'), season=int(match.group('season')), episode=int(match.group('episode')))

# Structured TV Shows
dir_re = r'(?P<name>[^\\]+?)[ -]+S(?:eason ?)?\d{1,2}(?:P\d{1,2})?(?:[ -][^\\]*)?'
file_re = r'[^\\]*S(?P<season>\d{1,2})E(?P<episode>\d{1,3})(?:[ -][^\\]*)?'
dir_re = r'(?P<name>[^\\]+?) S(?:eason ?)?\d{1,2}(?:P\d{1,2})?(?: [^\\]*)?'
file_re = r'[^\\]*S(?P<season>\d{1,2})E(?P<episode>\d{1,3})(?: [^\\]*)?'
if match := re.match(fr'^.*\\{dir_re}(?:\\.*)?\\{file_re}$', input_path):
return TVShow(name=match.group('name'), season=int(match.group('season')), episode=int(match.group('episode')))

# Other standalone TV Shows
if match := re.match(r'^(?P<name>.+?)[ -]* (?:S(?:eason ?)?(?P<season>\d{1,2})[ -]*)?E?(?P<episode>\d{1,3})(?:v\d+)?(?:[ -].*)?$', input_name):
if match := re.match(r'^(?P<name>.+?) (?:S(?:eason ?)?(?P<season>\d{1,2}) ?)?E?(?P<episode>\d{1,3})(?:v\d+)?(?!.* \d{2}(?: |$).*)(?: .*)?$', input_name):
return TVShow(name=match.group('name'), season=int(match.group('season') or '1'), episode=int(match.group('episode')))

return Movie(name=input_name)
Expand Down
25 changes: 16 additions & 9 deletions tests/test_buganime.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

NAME_CONVERSIONS = [
(r'C:\[SHiN-gx] Fight Ippatsu! Juuden-chan!! - Special 1 [720x480 AR h.264 FLAC][v2][FF09021F].mkv',
buganime.TVShow(name='Fight Ippatsu! Juuden-chan!!', season=0, episode=1)),
buganime.TVShow(name='Fight Ippatsu! Juuden chan!!', season=0, episode=1)),

(r'C:\[gleam] Kurenai OVA - 01 [OAD][0e73f000].mkv',
buganime.TVShow(name='Kurenai', season=0, episode=1)),
Expand All @@ -21,19 +21,19 @@
buganime.TVShow(name='Saki', season=0, episode=1)),

(r'C:\[CoalGuys] K-ON!! S2 - 05 [4B19B10F].mkv',
buganime.TVShow(name='K-ON!!', season=2, episode=5)),
buganime.TVShow(name='K ON!!', season=2, episode=5)),

(r'C:\[SubsPlease] RWBY - Hyousetsu Teikoku - 01 (1080p) [FA9C5B87].mkv',
buganime.TVShow(name='RWBY - Hyousetsu Teikoku', season=1, episode=1)),
buganime.TVShow(name='RWBY Hyousetsu Teikoku', season=1, episode=1)),

(r'C:\[SubsPlease] Tokyo Mew Mew New - 01 (1080p) [440C0CD7].mkv',
buganime.TVShow(name='Tokyo Mew Mew New', season=1, episode=1)),

(r'C:\[Erai-raws] Shin Tennis no Ouji-sama - U-17 World Cup - 01 [1080p][Multiple Subtitle][0341CBE1].mkv',
buganime.TVShow(name='Shin Tennis no Ouji-sama - U-17 World Cup', season=1, episode=1)),
buganime.TVShow(name='Shin Tennis no Ouji sama U 17 World Cup', season=1, episode=1)),

(r'C:\[Judas] Kaguya-Sama Wa Kokurasetai - S03E07.mkv',
buganime.TVShow(name='Kaguya-Sama Wa Kokurasetai', season=3, episode=7)),
buganime.TVShow(name='Kaguya Sama Wa Kokurasetai', season=3, episode=7)),

(r'C:\[SubsPlease] Rikei ga Koi ni Ochita no de Shoumei shitemita - 08v2 (1080p) [77514EF3].mkv',
buganime.TVShow(name='Rikei ga Koi ni Ochita no de Shoumei shitemita', season=1, episode=8)),
Expand All @@ -42,17 +42,17 @@
buganime.TVShow(name='Rikei ga Koi ni Ochita no de Shoumei shitemita', season=2, episode=8)),

(r'C:\Kaguya-sama - Love is War - S00E01 - (S2O1 OVA).mkv',
buganime.TVShow(name='Kaguya-sama - Love is War', season=0, episode=1)),
buganime.TVShow(name='Kaguya sama Love is War', season=0, episode=1)),

(r'C:\Kaguya-sama - Love is War - S01E06.mkv',
buganime.TVShow(name='Kaguya-sama - Love is War', season=1, episode=6)),
buganime.TVShow(name='Kaguya sama Love is War', season=1, episode=6)),

(r'C:\Kaguya-sama wa Kokurasetai S03 1080p Dual Audio WEBRip AAC x265-EMBER\S03E01-Miko Iino Wants to Be Soothed Kaguya Doesn’t Realize Chika Fujiwara '
r'Wants to Battle [8933E8C9].mkv',
buganime.TVShow(name='Kaguya-sama wa Kokurasetai', season=3, episode=1)),
buganime.TVShow(name='Kaguya sama wa Kokurasetai', season=3, episode=1)),

(r'C:\Kaguya-sama wa Kokurasetai S2 - OVA - 1080p WEB H.264 -NanDesuKa (B-Global).mkv',
buganime.TVShow(name='Kaguya-sama wa Kokurasetai', season=0, episode=1)),
buganime.TVShow(name='Kaguya sama wa Kokurasetai', season=0, episode=1)),

(r'C:\Tensei shitara Ken Deshita - 01 - 2160p WEB H.264 -NanDesKa.mkv',
buganime.TVShow(name='Tensei shitara Ken Deshita', season=1, episode=1)),
Expand All @@ -77,6 +77,13 @@

(r'C:\Mushoku Tensei S02P01+SP 1080p Dual Audio BDRip 10 bits DD+ x265-EMBER\S02E01-The Brokenhearted Mage [AFBB9792].mkv',
buganime.TVShow(name='Mushoku Tensei', season=2, episode=1)),

(r'C:\A.Terrified.Teacher.at.Ghoul.School!.S01E01.Welcome.to.Hyakki.Academy!.1080p.CR.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub.mkv',
buganime.TVShow(name='A Terrified Teacher at Ghoul School!', season=1, episode=1)),

(r'C:\Tohai.-.Ura.Rate.Mahjong.Tohai.Roku.S01E01.1080p.AMZN.WEB-DL.DDP2.0.H.264-Emmid'
r'\Tohai.-.Ura.Rate.Mahjong.Tohai.Roku.S01E01.1080p.AMZN.WEB-DL.DDP2.0.H.264-Emmid.mkv',
buganime.TVShow(name='Tohai Ura Rate Mahjong Tohai Roku', season=1, episode=1)),
]


Expand Down

0 comments on commit 54b837d

Please sign in to comment.