Skip to content

Commit

Permalink
feat: add anime parser. more tweaks to handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dreulavelle committed Nov 9, 2024
1 parent 7580b29 commit 91e6f28
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
10 changes: 9 additions & 1 deletion PTT/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
from .handlers import add_defaults
from .parse import Parser
from .anime import anime_handler

_parser = Parser()
add_defaults(_parser)


def parse_title(raw_title: str, translate_languages: bool = False) -> dict:
def parse_title(raw_title: str, translate_languages: bool = False, parse_anime: bool = False) -> dict:
"""
Parse the given input string using the initialized parser instance.
:param raw_title: The input raw torrent title to parse.
:param translate_languages: Whether to translate language codes to language names or short codes (default: False returns short codes)
:param parse_anime: Whether to parse anime title (default: False)
:return: A dictionary with the parsed results.
Note:
If `parse_anime` is True, the anime handlers will be added to the parser instance.
This can add more time to the parsing process.
"""
if parse_anime:
anime_handler(_parser) # add anime handlers to the parser instance
return _parser.parse(raw_title, translate_languages)


Expand Down
10 changes: 9 additions & 1 deletion PTT/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def add_defaults(parser: Parser):
# Torrent extension
parser.add_handler("torrent", regex.compile(r"\.torrent$"), boolean, {"remove": True})

# Scene
parser.add_handler("scene", regex.compile(r"^(?=.*(\b\d{3,4}p\b).*([_. ]WEB[_. ])(?!DL)\b)|\b(-CAKES|-GGEZ|-GGWP|-GLHF|-GOSSIP|-NAISU|-KOGI|-PECULATE|-SLOT|-EDITH|-ETHEL|-ELEANOR|-B2B|-SPAMnEGGS|-FTP|-DiRT|-SYNCOPY|-BAE|-SuccessfulCrab|-NHTFS|-SURCODE|-B0MBARDIERS)"), boolean, {"remove": False})
parser.add_handler("scene", regex.compile(r"\b(INFLATE|DEFLATE)\b", regex.IGNORECASE), boolean, {"remove": False, "skipIfAlreadyFound": True})

# Extras (This stuff can be trashed)
parser.add_handler("extras", regex.compile(r"\bNCED\b", regex.IGNORECASE), uniq_concat(value("NCED")), {"remove": True})
parser.add_handler("extras", regex.compile(r"\bNCOP\b", regex.IGNORECASE), uniq_concat(value("NCOP")), {"remove": True})
Expand Down Expand Up @@ -125,7 +129,7 @@ def add_defaults(parser: Parser):
parser.add_handler("edition", regex.compile(r"\bRemaster(?:ed)?\b", regex.IGNORECASE), value("Remastered"), {"remove": True, "skipIfAlreadyFound": True})

# Upscaled
parser.add_handler("upscaled", regex.compile(r"\b(?:AI.?)?(Upscaled?|Enhanced?)\b", regex.IGNORECASE), boolean)
parser.add_handler("upscaled", regex.compile(r"\b(?:AI.?)?(Upscal(ed?|ing)|Enhanced?)\b", regex.IGNORECASE), boolean)
parser.add_handler("upscaled", regex.compile(r"\b(?:iris2|regrade|ups(uhd|fhd|hd|4k))\b", regex.IGNORECASE), boolean)
parser.add_handler("upscaled", regex.compile(r"\b\.AI\.\b", regex.IGNORECASE), boolean)

Expand Down Expand Up @@ -543,7 +547,10 @@ def handle_group(context):
parser.add_handler("group", handle_group)

# 3D
parser.add_handler("3d", regex.compile(r"(?<=\b[12]\d{3}\b).*\b(3d|sbs|half[ .-]ou|half[ .-]sbs)\b", regex.IGNORECASE), boolean, {"remove": False, "skipIfFirst": True})
parser.add_handler("3d", regex.compile(r"\b((Half.)?SBS|HSBS)\b", regex.IGNORECASE), boolean, {"remove": False, "skipIfFirst": True})
parser.add_handler("3d", regex.compile(r"\bBluRay3D\b", regex.IGNORECASE), boolean, {"remove": False, "skipIfFirst": True})
parser.add_handler("3d", regex.compile(r"\bBD3D\b", regex.IGNORECASE), boolean, {"remove": False, "skipIfFirst": True})
parser.add_handler("3d", regex.compile(r"\b3D\b", regex.IGNORECASE), boolean, {"remove": False, "skipIfFirst": True})

# Size
Expand Down Expand Up @@ -593,3 +600,4 @@ def handle_group_exclusion(context):

# Title (hardcoded cleanup)
parser.add_handler("title", regex.compile(r"\b100[ .-]*years?[ .-]*quest\b", regex.IGNORECASE), none, {"remove": True}) # episode title
parser.add_handler("title", regex.compile(r"\b(?:INTEGRALE?|INTÉGRALE?)\b", regex.IGNORECASE), none, {"remove": True})
20 changes: 16 additions & 4 deletions cli.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import argparse
import sys

def main():
parser = argparse.ArgumentParser(description="Parse filename or torrent name using Parsett")
parser.add_argument('filename', type=str, help="Parse filename")
parser.add_argument('filename', type=str, help="The name of the file or torrent to be parsed")
parser.add_argument('-a', '--anime', action='store_true', help="Enable parsing of anime titles")
parser.add_argument('-tl', '--translate-languages', action='store_true', help="Translate language codes (e.g., 'en', 'jp') to their full language names (e.g., 'English', 'Japanese')")

args = parser.parse_args()
if not args.filename:
parser.print_help()
sys.exit(1)

if args.filename:
from PTT import parse_title
print(parse_title(args.filename))
from PTT import parse_title

print(
parse_title(
args.filename,
translate_languages=args.translate_languages,
parse_anime=args.anime
)
)

if __name__ == "__main__":
main()
13 changes: 10 additions & 3 deletions tests/test_anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"codec": "hevc",
"resolution": "1080p",
"group": "iAHD",
"anime": True
}),
("[SubsPlease] Tearmoon Teikoku Monogatari - 01 (1080p) [15ADAE00].mkv", {
"title": "Tearmoon Teikoku Monogatari",
Expand All @@ -26,6 +27,7 @@
"group": "SubsPlease",
"container": "mkv",
"extension": "mkv",
"anime": True
}),
("[Erai-raws] Tearmoon Teikoku Monogatari - 01 [1080p][Multiple Subtitle] [ENG][POR-BR][SPA-LA][SPA][ARA][FRE][GER][ITA][RUS]", {
"title": "Tearmoon Teikoku Monogatari",
Expand All @@ -35,6 +37,7 @@
"resolution": "1080p",
"group": "Erai-raws",
"subbed": True,
"anime": True
}),
("Hunter x Hunter (2011) - 01 [1080p][Multiple Subtitle] [ENG][POR-BR][SPA-LA][SPA][ARA][FRE][GER][ITA][RUS]", {
"title": "Hunter x Hunter",
Expand All @@ -43,7 +46,7 @@
"languages": ["en", "fr", "es", "pt", "it", "de", "ru", "ar"],
"resolution": "1080p",
"year": 2011,
"subbed": True,
"subbed": True
}),
("[SubsPlease] Fairy Tail - 100 Years Quest - 05 (1080p) [1107F3A9].mkv", {
"title": "Fairy Tail",
Expand All @@ -55,6 +58,7 @@
"container": "mkv",
"resolution": "1080p",
"group": "SubsPlease",
"anime": True
}),
("Naruto Shippuden (001-500) [Complete Series + Movies] (Dual Audio)", {
"title": "Naruto Shippuden",
Expand All @@ -74,6 +78,7 @@
"container": "mkv",
"extension": "mkv",
"subbed": True,
"anime": True
}),
("One.Piece.S01E1116.Lets.Go.Get.It!.Buggys.Big.Declaration.2160p.B-Global.WEB-DL.JPN.AAC2.0.H.264.MSubs-ToonsHub.mkv", {
"title": "One Piece",
Expand All @@ -87,7 +92,7 @@
"group": "ToonsHub",
"container": "mkv",
"extension": "mkv",
"subbed": True,
"subbed": True
}),
("[Erai-raws] 2-5 Jigen no Ririsa - 08 [480p][Multiple Subtitle][972D0669].mkv", {
"title": "2-5 Jigen no Ririsa",
Expand All @@ -100,6 +105,7 @@
"container": "mkv",
"extension": "mkv",
"subbed": True,
"anime": True
}),
("[Exiled-Destiny]_Tokyo_Underground_Ep02v2_(41858470).mkv", {
"title": "Tokyo Underground",
Expand All @@ -110,8 +116,9 @@
"episode_code": "41858470",
"container": "mkv",
"extension": "mkv",
"anime": True
}),
])
def test_random_anime_parse(release_name, expected):
result = parse_title(release_name)
result = parse_title(release_name, parse_anime=True)
assert result == expected
3 changes: 3 additions & 0 deletions tests/test_title.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def parser():
("О мышах и людях (Of Mice and Men) 1992 BDRip 1080p.mkv", "Of Mice and Men"),
("Wonder Woman 1984 (2020) [UHDRemux 2160p DoVi P8 Es-DTSHD AC3 En-AC3].mkv", "Wonder Woman 1984"),
("www.TamilBlasters.cam - Titanic (1997)[1080p BDRip - Org Auds - [Tamil + Telugu + Hindi + Eng] - x264 - DD5.1 (448 Kbps) - 4.7GB - ESubs].mkv", "Titanic"),
("S.W.A.T.2017.S08E01.720p.HDTV.x264-SYNCOPY[TGx]", "S W A T"),
("Grimm.INTEGRAL.MULTI.COMPLETE.BLURAY-BMTH", "Grimm"),
("Friends.1994.INTEGRALE.MULTI.1080p.WEB-DL.H265-FTMVHD", "Friends"),
],
)
def test_title_detection(parser, release_name, expected_title):
Expand Down

0 comments on commit 91e6f28

Please sign in to comment.