Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(fc2): 卖家计划按摄取判据找账号,摄取不收的作品记跳过

longmeidao2c2a74304034
diff --git a/src/peach/fc2_sellers.py b/src/peach/fc2_sellers.py
index 089ddf868..fec2f18da 100644
--- a/src/peach/fc2_sellers.py
+++ b/src/peach/fc2_sellers.py
@@ -10,6 +10,7 @@ from pathlib import Path
from urllib.parse import urlsplit
from .catalog_rules import normalise_code_key
+from .classification import creator_collection_base
from .entities import canonicalize_entity_name, normalize_entity_name, upsert_asset_entity
from .entity_classification import write_claim
@@ -42,12 +43,9 @@ def seller_record(payload: dict, provider: str) -> dict | None:
def cached_records(root: Path):
- """读取有限的 FC2 来源快照;失败快照不作为账号证据。"""
+ """逐个读取 FC2 来源快照;失败快照不作为账号证据。"""
for provider in PROVIDERS:
- paths = sorted(root.glob(f'FC2-PPV-*-{provider}.json'))
- if len(paths) > 10000:
- raise ValueError('FC2 来源快照超过单次处理上限')
- for path in paths:
+ for path in sorted(root.glob(f'FC2-PPV-*-{provider}.json')):
try:
row = seller_record(json.loads(path.read_text(encoding='utf-8')), provider)
except (ValueError, TypeError):
@@ -56,6 +54,15 @@ def cached_records(root: Path):
yield {**row, 'cache_file': str(path)}
+def ingest_name(connection, name: str) -> str:
+ """摄取会把 `XXX4K` 这类集合名归到已有账号 `XXX`;计划按同一个名字找账号。"""
+ base = creator_collection_base(name)
+ if base != name and connection.execute("SELECT 1 FROM entity WHERE kind='creator' AND normalized_name=?",
+ (normalize_entity_name(base),)).fetchone():
+ return base
+ return name
+
+
def existing_account(connection, rows: list[dict]) -> tuple[int | None, str]:
"""外部账号优先;同名而不同账号保留为冲突。"""
account = rows[0]['account']
@@ -63,7 +70,7 @@ def existing_account(connection, rows: list[dict]) -> tuple[int | None, str]:
"AND external_kind='creator' AND external_id=?", (account,)).fetchone()
if held:
return int(held[0]), ''
- names = {normalize_entity_name(row['name']) for row in rows}
+ names = {normalize_entity_name(ingest_name(connection, row['name'])) for row in rows}
found = set()
for name in names:
matches = connection.execute("SELECT e.id FROM entity e WHERE e.kind='creator' AND "
@@ -99,7 +106,7 @@ def _account_groups(connection, groups, skipped):
def collect(connection, root: Path) -> dict:
"""来源账号按作品号与馆藏关联;来源冲突和非馆藏作品不落库。"""
assets = defaultdict(list)
- for row in connection.execute("SELECT id,code FROM asset WHERE medium='video' AND COALESCE(disposal,'')<>'vanished' AND code IS NOT NULL"):
+ for row in connection.execute("SELECT id,code FROM asset WHERE medium='video' AND disposal IS NULL AND code IS NOT NULL"):
code = normalise_code_key(row[1])
if code.startswith('FC2-PPV-'):
assets[code].append(int(row[0]))
@@ -143,25 +150,38 @@ def _digest(metadata):
return sha256(json.dumps(payload, sort_keys=True, ensure_ascii=False).encode()).hexdigest()
-def _associate(connection, entity_id, name, record, metadata, batch):
+def _associate(connection, entity_id, name, record, metadata, batch, skipped):
+ """把一部作品挂到卖家账号下,返回挂上的账号;一个文件都没挂上时返回 None。
+
+ 摄取判定不收(已否决、名字是厂牌、目录名是发行标识)的文件记进 `skipped`;摄取认到的
+ 账号与计划不同时整批回滚。"""
payload = {**metadata, **record}
payload['created_payload_digest'] = _digest(payload)
+ linked = None
for asset_id in record['asset_ids']:
- asset = connection.execute("SELECT code FROM asset WHERE id=? AND medium='video' AND COALESCE(disposal,'')<>'vanished'", (asset_id,)).fetchone()
+ asset = connection.execute("SELECT code FROM asset WHERE id=? AND medium='video' AND disposal IS NULL", (asset_id,)).fetchone()
if not asset or normalise_code_key(asset[0]) != record['code']:
raise ValueError('卖家作品计划已过期')
if entity_id and connection.execute("SELECT 1 FROM asset_entity WHERE asset_id=? AND entity_id=? AND role='creator'",
(asset_id, entity_id)).fetchone():
+ linked = entity_id
continue
- entity_id = upsert_asset_entity(connection, kind='creator', name=name, asset_id=asset_id, role='creator',
+ landed = upsert_asset_entity(connection, kind='creator', name=name, asset_id=asset_id, role='creator',
source=batch, metadata=payload, update_entity_metadata=False)
- actual = connection.execute('SELECT kind FROM entity WHERE id=?', (entity_id,)).fetchone()
+ if landed is None:
+ skipped.append({'account': record['account'], 'code': record['code'], 'asset_id': asset_id,
+ 'reason': '摄取判定不收这个卖家名'})
+ continue
+ actual = connection.execute('SELECT kind FROM entity WHERE id=?', (landed,)).fetchone()
if not actual or actual[0] != 'creator':
raise ValueError('卖家账号与已合并出演身份冲突')
- return entity_id
+ if entity_id and landed != entity_id:
+ raise ValueError('卖家身份计划已过期:摄取认到另一个账号')
+ entity_id = linked = landed
+ return linked
-def _install_account(connection, group, batch):
+def _install_account(connection, group, batch, skipped):
first = group['records'][0]
entity_id = group['entity_id']
if existing_account(connection, group['records']) != (entity_id, ''):
@@ -173,14 +193,24 @@ def _install_account(connection, group, batch):
raise ValueError('账号身份已变化')
name = held[1]
metadata = {'source': SOURCE, 'batch': batch, 'account_url': first['account_url']}
+ installed = None
for record in group['records']:
- entity_id = _associate(connection, entity_id, name, record, metadata, batch)
+ linked = _associate(connection, entity_id, name, record, metadata, batch, skipped)
+ if not linked:
+ continue
+ entity_id = installed = linked
_claim(connection, entity_id, record, batch)
alias = normalize_entity_name(record['name'])
if alias != normalize_entity_name(name):
connection.execute('INSERT OR IGNORE INTO entity_alias(entity_id,alias,normalized_alias,source) VALUES(?,?,?,?)',
(entity_id, record['name'], alias, batch))
- if not connection.execute("SELECT 1 FROM entity_external_ref WHERE entity_id=? AND provider='fc2' AND external_kind='creator'", (entity_id,)).fetchone():
+ if not installed:
+ return None
+ other = connection.execute("SELECT external_id FROM entity_external_ref WHERE entity_id=? AND provider='fc2' "
+ "AND external_kind='creator'", (entity_id,)).fetchone()
+ if other and other[0] != first['account']:
+ raise ValueError('卖家身份计划已过期:账号已绑定另一个 FC2 账号')
+ if not other:
connection.execute("INSERT INTO entity_external_ref(entity_id,provider,external_kind,external_id,metadata_json) VALUES(?,'fc2','creator',?,?)",
(entity_id, first['account'], json.dumps(metadata)))
_link(connection, entity_id, first, batch)
@@ -192,8 +222,10 @@ def install(connection, frozen: dict, *, batch: str) -> dict:
if not batch.startswith(SOURCE + '@'):
raise ValueError('卖家批次需要明确来源前缀')
before = connection.total_changes
- ids = [_install_account(connection, group, batch) for group in frozen['accounts']]
- return {'entity_ids': sorted(set(ids)), 'changes': connection.total_changes - before}
+ skipped: list[dict] = []
+ ids = [_install_account(connection, group, batch, skipped) for group in frozen['accounts']]
+ return {'entity_ids': sorted({entity_id for entity_id in ids if entity_id}), 'skipped': skipped,
+ 'changes': connection.total_changes - before}
def planned_revert(connection, source: str, batch: str) -> list[dict]:
diff --git a/tests/test_fc2_sellers.py b/tests/test_fc2_sellers.py
index bc154114c..a035834b9 100644
--- a/tests/test_fc2_sellers.py
+++ b/tests/test_fc2_sellers.py
@@ -140,6 +140,62 @@ class Fc2SellerTests(unittest.TestCase):
self.undo()
self.assertEqual(self.connection.execute('SELECT canonical_name FROM entity WHERE id=?',(entity_id,)).fetchone()[0],'User Seller')
+ def test_rejected_seller_name_is_skipped_while_other_accounts_still_land(self):
+ rejected = self.asset('FC2-PPV-1234567')
+ self.asset('FC2-PPV-7654321')
+ self.connection.execute("INSERT INTO review_decision(category,item_key,status,note,updated_at) "
+ "VALUES('creator-attribution',?,'rejected','','2026-10-08T00:00:00+00:00')",
+ (f'{rejected}:seller',))
+ self.connection.commit()
+ self.snapshot()
+ self.snapshot(code='FC2-PPV-7654321',name='Other Seller',account='other')
+ result = self.install()
+ self.assertEqual(len(result['entity_ids']),1)
+ self.assertEqual([(row['code'],row['reason']) for row in result['skipped']],[('FC2-PPV-1234567','摄取判定不收这个卖家名')])
+ names = [row[0] for row in self.connection.execute("SELECT canonical_name FROM entity WHERE kind='creator'")]
+ self.assertEqual(names,['Other Seller'])
+ self.assertEqual(self.connection.execute("SELECT count(*) FROM entity_external_ref WHERE external_id='seller'").fetchone()[0],0)
+
+ def test_held_account_gets_no_alias_or_claim_from_a_work_that_ingest_rejects(self):
+ one = self.asset('FC2-PPV-1234567')
+ two = self.asset('FC2-PPV-7654321')
+ entity_id = upsert_asset_entity(self.connection,kind='creator',name='User Name',asset_id=one,role='creator',
+ source='user:manual',external_provider='fc2',external_id='seller')
+ self.connection.execute("INSERT INTO review_decision(category,item_key,status,note,updated_at) "
+ "VALUES('creator-attribution',?,'rejected','','2026-10-08T00:00:00+00:00')",
+ (f'{two}:{fc2_sellers.normalize_entity_name("User Name")}',))
+ self.connection.commit()
+ self.snapshot(code='FC2-PPV-7654321',name='Public Name')
+ result = self.install()
+ self.assertEqual(result['entity_ids'],[])
+ self.assertEqual([row['asset_id'] for row in result['skipped']],[two])
+ for table in ('entity_alias','entity_classification','entity_link'):
+ self.assertEqual(self.connection.execute(f'SELECT count(*) FROM {table} WHERE entity_id=?',(entity_id,)).fetchone()[0],0)
+
+ def test_collection_names_are_planned_against_the_account_that_ingest_picks(self):
+ one = self.asset('FC2-PPV-1234567')
+ two = self.asset('FC2-PPV-7654321')
+ held = upsert_asset_entity(self.connection,kind='creator',name='Studio',asset_id=one,role='creator',source='user:manual')
+ self.connection.commit()
+ self.snapshot(code='FC2-PPV-7654321',name='Studio 4K')
+ plan = fc2_sellers.collect(self.connection,self.cache)
+ self.assertEqual(plan['accounts'][0]['entity_id'],held)
+ self.install()
+ self.assertEqual(self.connection.execute("SELECT entity_id FROM asset_entity WHERE asset_id=? AND role='creator'",(two,)).fetchone()[0],held)
+ self.connection.execute("UPDATE entity_external_ref SET external_id='other' WHERE entity_id=?",(held,))
+ self.connection.commit()
+ (self.cache / 'FC2-PPV-7654321-fc2ppvdb.json').unlink()
+ self.snapshot(code='FC2-PPV-1234567',name='Studio 4K',account='third')
+ plan = fc2_sellers.collect(self.connection,self.cache)
+ self.assertEqual([row['reason'] for row in plan['skipped']],['同名对应不同 FC2 账号'])
+
+ def test_works_in_the_recycle_bin_get_no_seller(self):
+ asset_id = self.asset('FC2-PPV-1234567')
+ self.connection.execute("UPDATE asset SET disposal='trash' WHERE id=?",(asset_id,))
+ self.connection.commit()
+ self.snapshot()
+ self.assertEqual(fc2_sellers.collect(self.connection,self.cache)['accounts'],[])
+
def test_stale_work_plan_rolls_back_and_revert_keeps_later_user_metadata(self):
asset_id = self.asset('FC2-PPV-1234567')
self.snapshot()