File size: 6,570 Bytes
337aa19 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | from __future__ import annotations
import re
from typing import Any
from .utils import compact_title, normalize_text
QUALITY_RE = re.compile(r"\b(2160p|1440p|1080p|720p|480p|360p|4k|uhd|hd)\b", re.I)
YEAR_RE = re.compile(r"\b(19\d{2}|20\d{2}|2100)\b")
PART_RE = re.compile(r"\b(?:parte|part|pt)\s*([0-9]{1,3})\b", re.I)
SEASON_EPISODE_PATTERNS = [
re.compile(r"\bS(\d{1,2})\s*E(\d{1,3})\b", re.I),
re.compile(r"\b(\d{1,2})x(\d{1,3})\b", re.I),
re.compile(r"\b(?:temporada|season|t)\s*(\d{1,2})\D{0,6}(?:capitulo|capítulo|episodio|episode|ep|e|c)\s*(\d{1,3})\b", re.I),
]
EPISODE_ONLY_PATTERNS = [
re.compile(r"\b(?:capitulo|capítulo|episodio|episode|ep|e|c)\s*(\d{1,3})\b", re.I),
]
SEASON_ONLY_PATTERNS = [
re.compile(r"\b(?:temporada|season|t)\s*(\d{1,2})\b", re.I),
]
LANG_PATTERNS = [
(re.compile(r"espa(?:n|ñ)ol\s+latino", re.I), ("Español Latino", "🇲🇽")),
(re.compile(r"latino", re.I), ("Español Latino", "🇲🇽")),
(re.compile(r"castellano", re.I), ("Español Castellano", "🇪🇸")),
(re.compile(r"sub(?:titulado)?\s+espa(?:n|ñ)ol", re.I), ("Sub Español", "🇪🇸")),
(re.compile(r"dual\s+audio", re.I), ("Dual Audio", "🌐")),
(re.compile(r"english|ingl[eé]s", re.I), ("Inglés", "🇺🇸")),
(re.compile(r"japon[eé]s|japanese", re.I), ("Japonés", "🇯🇵")),
]
EXPLICIT_MOVIE = re.compile(r"#movie|#pelicula|\bpel[ií]cula\b|\bfilm\b", re.I)
EXPLICIT_SERIES = re.compile(r"#serie|#series|#anime|\banime\b|\bova\b|\btemporada\b|\bseason\b|\bcap[ií]tulo\b|\bepisodio\b", re.I)
FINAL_SEASON_RE = re.compile(r"final\s+de\s+temporada|season\s+finale", re.I)
TITLE_CLEAN_RE = re.compile(
r"\b(?:2160p|1440p|1080p|720p|480p|360p|4k|uhd|hd|espa(?:n|ñ)ol|latino|castellano|sub|dual|audio|cap[ií]tulo|episodio|episode|season|temporada|parte|part|final|de|la|pel[ií]cula|movie|mkv|mp4)\b",
re.I,
)
TRAILING_NUMBER_RE = re.compile(r"(.+?)\s+([0-9]{1,3})$")
def _first_text(*values: str | None) -> str:
for value in values:
if value and value.strip():
return value.strip()
return ""
def _detect_language(text: str) -> tuple[str | None, str | None]:
for pattern, payload in LANG_PATTERNS:
if pattern.search(text):
return payload
return None, None
def _clean_title(text: str) -> str:
lines = [line.strip() for line in text.splitlines() if line.strip()]
if not lines:
return "Sin título"
base = lines[0]
base = re.sub(r"\[[^\]]+\]", " ", base)
base = re.sub(r"\([^)]*\)", " ", base)
base = QUALITY_RE.sub(" ", base)
base = YEAR_RE.sub(" ", base)
base = PART_RE.sub(" ", base)
for pattern in SEASON_EPISODE_PATTERNS + EPISODE_ONLY_PATTERNS + SEASON_ONLY_PATTERNS:
base = pattern.sub(" ", base)
base = TITLE_CLEAN_RE.sub(" ", base)
base = re.sub(r"\.(mkv|mp4|avi|mov|m4v|webm)$", " ", base, flags=re.I)
base = re.sub(r"[_\-.]+", " ", base)
base = re.sub(r"\s+", " ", base).strip(" -")
return compact_title(base or lines[0])
def _detect_episode(text: str) -> tuple[int | None, int | None]:
for pattern in SEASON_EPISODE_PATTERNS:
match = pattern.search(text)
if match:
return int(match.group(1)), int(match.group(2))
season = None
episode = None
for pattern in SEASON_ONLY_PATTERNS:
match = pattern.search(text)
if match:
season = int(match.group(1))
break
for pattern in EPISODE_ONLY_PATTERNS:
match = pattern.search(text)
if match:
episode = int(match.group(1))
break
return season, episode
def parse_media_message(message) -> dict[str, Any] | None:
media = message.video or message.document
if not media:
return None
file_name = getattr(media, "file_name", None) or ""
raw_caption = message.caption or ""
raw_text = _first_text(raw_caption, file_name, f"media-{message.id}")
source_text = f"{raw_caption}\n{file_name}".strip()
quality_match = QUALITY_RE.search(source_text)
quality = quality_match.group(1).upper() if quality_match else None
year_match = YEAR_RE.search(source_text)
year = int(year_match.group(1)) if year_match else None
season, episode = _detect_episode(source_text)
part_match = PART_RE.search(source_text)
part = int(part_match.group(1)) if part_match else None
duration = getattr(media, "duration", None) or None
language, language_flag = _detect_language(source_text)
is_finale = bool(FINAL_SEASON_RE.search(source_text))
title = _clean_title(raw_text)
lowered = source_text.lower()
content_type = "movie"
if EXPLICIT_SERIES.search(source_text) or season or episode:
content_type = "series"
elif EXPLICIT_MOVIE.search(source_text):
content_type = "movie"
elif duration and duration < 55 * 60:
content_type = "series"
elif duration and duration >= 55 * 60:
content_type = "movie"
if content_type == "movie" and not part:
trailing = TRAILING_NUMBER_RE.match(_clean_title(raw_text))
if trailing:
maybe_number = int(trailing.group(2))
if maybe_number < 100 and maybe_number != year:
title = compact_title(trailing.group(1))
part = maybe_number
if content_type == "series" and season is None:
season = 1
category = "anime" if "anime" in lowered or "ova" in lowered else content_type
search_blob = " ".join(
part for part in [title, file_name, raw_caption, language or "", quality or "", str(year or "")] if part
)
return {
"item_id": None,
"chat_id": message.chat.id,
"message_id": message.id,
"file_id": getattr(media, "file_id", None),
"file_unique_id": getattr(media, "file_unique_id", None),
"file_name": file_name,
"file_size": getattr(media, "file_size", None),
"mime_type": getattr(media, "mime_type", None),
"duration": duration,
"media_kind": "video" if message.video else "document",
"content_type": content_type,
"category": category,
"title": title,
"normalized_title": normalize_text(title),
"year": year,
"season": season,
"episode": episode,
"part": part,
"quality": quality,
"language": language,
"language_flag": language_flag,
"is_finale": is_finale,
"source_caption": raw_caption,
"search_blob": search_blob,
"notes": "",
}
|