feat: add job pause and improve HLTB matching
CI / test (push) Successful in 1m17s

This commit is contained in:
strsv committed 2026-11-07 00:13:21 +03:00
1 parent ccc1f625e8
commit 5874ffba9f
13 files changed
+258 -34

No files matched your search

+59 -11
View File
@@ -12,6 +12,7 @@ games.hltb_* and the main story time becomes games.estimated_hours.
"""
import argparse
import difflib
import os
import re
import sys
@@ -41,12 +42,48 @@ def normalize(name):
return " ".join(value.split())
def find_match(entries, name):
key = normalize(name)
ROMAN = {"ii": "2", "iii": "3", "iv": "4", "v": "5", "vi": "6",
"vii": "7", "viii": "8", "ix": "9", "x": "10"}
EDITION_WORDS = re.compile(
r"\b(goty|game of the year|hd|gold pack|gold edition|definitive edition|"
r"remastered|anniversary edition|complete edition|deluxe edition|"
r"ultimate edition|classic edition|classic|edition|rhcp|pcr|cis|row)\b", re.I)
def match_key(name):
"""Normalize titles for matching: editions, years, roman numerals."""
value = normalize(name)
value = re.sub(r"\b(?:19|20)\d{2}\b", " ", value)
value = EDITION_WORDS.sub(" ", value)
words = [ROMAN.get(word, word) for word in value.split()]
return " ".join(words)
def search_variants(name, year=None):
base = normalize(name)
stripped = match_key(name)
variants = [base, name.strip(), stripped]
if year:
variants.extend([f"{base} {year}", f"{stripped} {year}"])
out = []
for value in variants:
if value and value not in out:
out.append(value)
return out
def find_match(entries, name, year=None):
key = match_key(name)
best, best_score = None, 0.0
for entry in entries:
if normalize(entry.game_name) == key:
return entry
return None
candidate = match_key(entry.game_name)
score = 1.0 if candidate == key else difflib.SequenceMatcher(None, key, candidate).ratio()
if year and getattr(entry, "release_world", None) == year:
score += 0.05
if score >= 0.92 and score > best_score:
best, best_score = entry, score
return best
def positive(value):
@@ -82,7 +119,7 @@ def main():
where.append("(hltb_checked_at IS NULL OR hltb_match = 0)")
else:
where.append("hltb_checked_at IS NULL")
sql = (f"SELECT appid, name FROM games WHERE {' AND '.join(where)} "
sql = (f"SELECT appid, name, release_date FROM games WHERE {' AND '.join(where)} "
"ORDER BY COALESCE(review_pct, 0) DESC, name")
rows = con.execute(sql).fetchall()
if not args.all:
@@ -92,14 +129,25 @@ def main():
searcher = HowLongToBeat()
matched = missing = errors = 0
for index, row in enumerate(rows, 1):
try:
entries = searcher.search(row["name"])
except Exception as exc:
release = str(row["release_date"] or "")
year = int(release[:4]) if len(release) >= 4 and release[:4].isdigit() else None
entry = None
last_error = None
for query in search_variants(row["name"], year):
try:
entries = searcher.search(query)
except Exception as exc:
last_error = exc
break
entry = find_match(entries, row["name"], year)
if entry:
break
time.sleep(args.delay)
if last_error is not None:
errors += 1
print(f" {row['name']}: {exc}")
print(f" {row['name']}: {last_error}")
time.sleep(args.delay)
continue
entry = find_match(entries, row["name"])
if not entry:
missing += 1
if not args.dry_run: