Peach: Local-first video metadata scraper and media manager.
fix(metadata): 解析分段活动年份并记录资料链接批次
longmeidao9f0a9a6e29e1
diff --git a/src/peach/minnano_av.py b/src/peach/minnano_av.pyindex 659d5c0fe..cf0b59270 100644--- a/src/peach/minnano_av.py+++ b/src/peach/minnano_av.py@@ -223,9 +223,10 @@ def profile(html: str) -> dict | None:result["blood_type"] = blood.group(1) if blood else Noneresult["birthplace"] = _text(cells.get("出身地", "")) or Noneresult["hobbies"] = _text(cells.get("趣味・特技", "")) or None- period = _PERIOD.search(_text(cells.get("AV出演期間", "")))- result["debut_year"] = int(period.group(1)) if period else None- result["active_until"] = int(period.group(2)) if period and period.group(2) else None+ periods = list(_PERIOD.finditer(_text(cells.get("AV出演期間", ""))))+ result["debut_year"] = min(int(period.group(1)) for period in periods) if periods else None+ latest = max(periods, key=lambda period: int(period.group(1))) if periods else None+ result["active_until"] = int(latest.group(2)) if latest and latest.group(2) else Nonedebut = _text(cells.get("デビュー作品", ""))result["debut_date"] = _iso_date(debut)title = _TRAILING_NOTE.sub("", debut).strip() if result["debut_date"] else debutdiff --git a/src/peach/western_profiles.py b/src/peach/western_profiles.pyindex 9c6e0fffa..551cb7b9b 100644--- a/src/peach/western_profiles.py+++ b/src/peach/western_profiles.py@@ -88,7 +88,8 @@ def land(connection, entity_id: int, expected_name: str, record: dict, *, batch:url = link['url']if connection.execute('SELECT 1 FROM entity_link WHERE entity_id=? AND url=?', (entity_id,url)).fetchone():continue- metadata = json.dumps({'source':batch, 'source_url':record['profile_url']}, ensure_ascii=False)+ metadata = json.dumps({'source':batch.split('@', 1)[0], 'batch':batch,+ 'source_url':record['profile_url']}, ensure_ascii=False)cursor = connection.execute('INSERT INTO entity_link(entity_id,link_kind,label,url,hostname,metadata_json,created_at,updated_at) VALUES(?,?,?,?,?,?,?,?)',(entity_id, link['link_kind'], link['label'], url, urlsplit(url).hostname, metadata,stamp,stamp))added.append(cursor.lastrowid)diff --git a/tests/test_performer_profile_followup.py b/tests/test_performer_profile_followup.pyindex 744892035..4c03e350e 100644--- a/tests/test_performer_profile_followup.py+++ b/tests/test_performer_profile_followup.py@@ -165,6 +165,14 @@ class NoWait:class ParsingTests(unittest.TestCase):+ def test_multiple_career_periods_use_the_first_debut_and_latest_end(self):+ for text, until in (("2017年 - 2018年、2021年 -", None),+ ("2017年 - 2018年、2021年 - 2024年", 2024)):+ with self.subTest(periods=text):+ page = MINNANO_SPARSE.replace("2015年 - 2019年", text)+ found = minnano_av.profile(page)+ self.assertEqual((found['debut_year'], found['active_until']), (2017, until))+def test_a_full_profile_table_is_normalised_column_by_column(self):found = minnano_av.profile(MINNANO_PROFILE)self.assertEqual({column: found[column] for column in minnano_av.PROFILE_COLUMNS}, {diff --git a/tests/test_western_profiles.py b/tests/test_western_profiles.pyindex 57eb1906c..403964e8a 100644--- a/tests/test_western_profiles.py+++ b/tests/test_western_profiles.py@@ -1,5 +1,6 @@"""西方档案身份、单位转换、别名及链接边界。"""import unittest+import jsonimport sqlite3import tempfilefrom pathlib import Path@@ -80,6 +81,10 @@ class WesternProfileTests(unittest.TestCase):first = land(connection,entity_id,'Christy White',record,batch='auto:babepedia-profile@test')self.assertTrue(first['profile_written'])self.assertEqual(len(first['added_links']),1)+ metadata = json.loads(connection.execute('SELECT metadata_json FROM entity_link WHERE id=?',+ (first['added_links'][0],)).fetchone()[0])+ self.assertEqual((metadata['source'],metadata['batch']),+ ('auto:babepedia-profile','auto:babepedia-profile@test'))self.assertEqual(connection.execute('SELECT count(*) FROM entity_alias WHERE entity_id=? AND normalized_alias=?',(entity_id,'christine white')).fetchone()[0],0)self.assertEqual(connection.execute('SELECT alias FROM entity_alias WHERE entity_id=?',(entity_id,)).fetchall(),[('Christina Andreadou',)])record['profile']['height_cm'] = 170