Karan Adhikari commited on
Update metadata.py
Browse files- Backend/helper/metadata.py +32 -11
Backend/helper/metadata.py
CHANGED
|
@@ -130,17 +130,31 @@ def _year_from_str(year_val) -> int:
|
|
| 130 |
m = re.search(r"(\d{4})", str(year_val))
|
| 131 |
return int(m.group(1)) if m else 0
|
| 132 |
|
| 133 |
-
def _score_candidate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
score = _title_similarity(query_title, result_title)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
if query_year and result_year:
|
| 136 |
diff = abs(int(query_year) - result_year)
|
| 137 |
-
if
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
return score
|
| 145 |
|
| 146 |
def _build_query_variants(title: str, year: Optional[int] = None) -> List[str]:
|
|
@@ -209,12 +223,17 @@ async def safe_imdb_search(title: str, type_: str, year: Optional[int] = None) -
|
|
| 209 |
best_score = 0.0
|
| 210 |
best_title = ""
|
| 211 |
|
|
|
|
|
|
|
| 212 |
for query in query_variants:
|
| 213 |
try:
|
| 214 |
async with API_SEMAPHORE:
|
| 215 |
results = await search_title_multi(query=query, type=type_, limit=8)
|
| 216 |
for r in results:
|
| 217 |
-
score = _score_candidate(
|
|
|
|
|
|
|
|
|
|
| 218 |
if score > best_score:
|
| 219 |
best_score, best_id, best_title = score, r.get("id"), r.get("title", "")
|
| 220 |
if best_score >= _STRONG_MATCH:
|
|
@@ -280,11 +299,13 @@ async def _pick_best_tmdb_result(results, query_title: str, query_year: Optional
|
|
| 280 |
if not results:
|
| 281 |
return None
|
| 282 |
|
|
|
|
|
|
|
| 283 |
scored = []
|
| 284 |
best_item, best_score = None, 0.0
|
| 285 |
for item in results:
|
| 286 |
r_title, r_year = _tmdb_title_year(item, media_type)
|
| 287 |
-
score = _score_candidate(query_title, query_year, r_title, r_year)
|
| 288 |
scored.append((score, item, r_year))
|
| 289 |
if score > best_score:
|
| 290 |
best_score, best_item = score, item
|
|
@@ -296,7 +317,7 @@ async def _pick_best_tmdb_result(results, query_title: str, query_year: Optional
|
|
| 296 |
for _, item, r_year in scored[:_ALT_TITLE_LOOKUPS]:
|
| 297 |
alt_titles = await _tmdb_alternative_titles(media_type, getattr(item, "id", None))
|
| 298 |
for alt in alt_titles:
|
| 299 |
-
alt_score = _score_candidate(query_title, query_year, alt, r_year)
|
| 300 |
if alt_score > best_score:
|
| 301 |
best_score, best_item = alt_score, item
|
| 302 |
if best_score >= _STRONG_MATCH:
|
|
|
|
| 130 |
m = re.search(r"(\d{4})", str(year_val))
|
| 131 |
return int(m.group(1)) if m else 0
|
| 132 |
|
| 133 |
+
def _score_candidate(
|
| 134 |
+
query_title: str,
|
| 135 |
+
query_year: Optional[int],
|
| 136 |
+
result_title: str,
|
| 137 |
+
result_year: int,
|
| 138 |
+
year_reliable: bool = True,
|
| 139 |
+
) -> float:
|
| 140 |
score = _title_similarity(query_title, result_title)
|
| 141 |
+
|
| 142 |
+
if score < 0.5:
|
| 143 |
+
return score
|
| 144 |
+
|
| 145 |
if query_year and result_year:
|
| 146 |
diff = abs(int(query_year) - result_year)
|
| 147 |
+
if year_reliable:
|
| 148 |
+
if diff > 2:
|
| 149 |
+
score = max(0.0, score - 0.10 * (diff - 2))
|
| 150 |
+
elif score >= 0.80:
|
| 151 |
+
if diff == 0:
|
| 152 |
+
score = min(1.0, score + 0.20)
|
| 153 |
+
elif diff == 1:
|
| 154 |
+
score = min(1.0, score + 0.07)
|
| 155 |
+
else:
|
| 156 |
+
if diff == 0 and score >= 0.80:
|
| 157 |
+
score = min(1.0, score + 0.05)
|
| 158 |
return score
|
| 159 |
|
| 160 |
def _build_query_variants(title: str, year: Optional[int] = None) -> List[str]:
|
|
|
|
| 223 |
best_score = 0.0
|
| 224 |
best_title = ""
|
| 225 |
|
| 226 |
+
year_reliable = type_ == "movie"
|
| 227 |
+
|
| 228 |
for query in query_variants:
|
| 229 |
try:
|
| 230 |
async with API_SEMAPHORE:
|
| 231 |
results = await search_title_multi(query=query, type=type_, limit=8)
|
| 232 |
for r in results:
|
| 233 |
+
score = _score_candidate(
|
| 234 |
+
title, year, r.get("title", ""), _year_from_str(r.get("year", "")),
|
| 235 |
+
year_reliable=year_reliable,
|
| 236 |
+
)
|
| 237 |
if score > best_score:
|
| 238 |
best_score, best_id, best_title = score, r.get("id"), r.get("title", "")
|
| 239 |
if best_score >= _STRONG_MATCH:
|
|
|
|
| 299 |
if not results:
|
| 300 |
return None
|
| 301 |
|
| 302 |
+
year_reliable = media_type == "movie"
|
| 303 |
+
|
| 304 |
scored = []
|
| 305 |
best_item, best_score = None, 0.0
|
| 306 |
for item in results:
|
| 307 |
r_title, r_year = _tmdb_title_year(item, media_type)
|
| 308 |
+
score = _score_candidate(query_title, query_year, r_title, r_year, year_reliable=year_reliable)
|
| 309 |
scored.append((score, item, r_year))
|
| 310 |
if score > best_score:
|
| 311 |
best_score, best_item = score, item
|
|
|
|
| 317 |
for _, item, r_year in scored[:_ALT_TITLE_LOOKUPS]:
|
| 318 |
alt_titles = await _tmdb_alternative_titles(media_type, getattr(item, "id", None))
|
| 319 |
for alt in alt_titles:
|
| 320 |
+
alt_score = _score_candidate(query_title, query_year, alt, r_year, year_reliable=year_reliable)
|
| 321 |
if alt_score > best_score:
|
| 322 |
best_score, best_item = alt_score, item
|
| 323 |
if best_score >= _STRONG_MATCH:
|