-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknowledge_sync.py
More file actions
143 lines (128 loc) · 4.77 KB
/
Copy pathknowledge_sync.py
File metadata and controls
143 lines (128 loc) · 4.77 KB
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
"""High-level knowledge import and enrichment helpers."""
from __future__ import annotations
import logging
from pathlib import Path
from typing import Any
from consolemods_adapters import ConsoleModsMultiIdAdapter, ConsoleModsTitleIdAdapter
from dat_adapters import LocalDatAdapter
from database import DatabaseManager
from knowledge_base import KnowledgeRepository
from knowledge_sources import CachedHttpClient, KnowledgeImportService
from wiki_adapters import (
ConsoleModsWikiAdapter,
Free60WikiAdapter,
XenonLibraryWikiAdapter,
)
logger = logging.getLogger(__name__)
def sync_consolemods_knowledge(
db: DatabaseManager | None = None,
cache_dir: Path | str | None = None,
) -> dict[str, Any]:
"""Import ConsoleMods TitleID and Multi-ID data, then enrich the library."""
db = db or DatabaseManager()
summaries: list[dict[str, int | str]] = []
with db.get_connection() as conn:
repository = KnowledgeRepository(conn)
repository.ensure_schema()
service = KnowledgeImportService(repository)
client = CachedHttpClient(cache_dir=cache_dir)
for adapter in (
ConsoleModsTitleIdAdapter(client),
ConsoleModsMultiIdAdapter(client),
):
summary = service.run_adapter(adapter)
summaries.append(summary)
logger.info(
"Imported %s records from %s",
summary["records_imported"],
summary["adapter"],
)
enriched = db.enrich_existing_titleids_from_knowledge()
return {
"adapters": summaries,
"titleids_enriched": enriched,
}
def sync_reference_wikis(
db: DatabaseManager | None = None,
cache_dir: Path | str | None = None,
max_documents_per_source: int | None = None,
) -> dict[str, Any]:
"""Import searchable Xbox 360 wiki articles with per-source isolation."""
db = db or DatabaseManager()
client = CachedHttpClient(cache_dir=cache_dir)
with db.get_connection() as connection:
known_by_source = {
slug: tuple(
row[0]
for row in connection.execute(
"""
SELECT d.url FROM source_documents d
JOIN knowledge_sources s ON s.id=d.source_id
WHERE s.slug=? AND d.document_type='wiki_article'
""",
(slug,),
).fetchall()
)
for slug in ("consolemods-wiki", "xenonlibrary", "free60")
}
adapters = (
ConsoleModsWikiAdapter(
client,
max_documents=max_documents_per_source,
known_urls=known_by_source["consolemods-wiki"],
),
XenonLibraryWikiAdapter(
client,
max_documents=max_documents_per_source,
known_urls=known_by_source["xenonlibrary"],
),
Free60WikiAdapter(
client,
max_documents=max_documents_per_source,
known_urls=known_by_source["free60"],
),
)
summaries: list[dict[str, Any]] = []
for adapter in adapters:
try:
with db.get_connection() as conn:
repository = KnowledgeRepository(conn)
repository.ensure_schema()
summary = KnowledgeImportService(repository).run_adapter(adapter)
summaries.append(summary)
except Exception as exc:
logger.exception("Knowledge source sync failed: %s", adapter.source.slug)
summaries.append(
{
"source": adapter.source.slug,
"adapter": adapter.adapter_name,
"status": "failed",
"error": str(exc),
"records_imported": 0,
}
)
try:
from offline_knowledge import OfflineKnowledgeArchive
archive = OfflineKnowledgeArchive(
database_path=db.db_path,
cache_dir=cache_dir,
).rebuild()
except Exception as exc:
logger.exception("Offline knowledge archive rebuild failed")
archive = {"status": "failed", "error": str(exc)}
return {"adapters": summaries, "offline_archive": archive}
def import_dat_knowledge(
path: Path | str,
source_kind: str,
db: DatabaseManager | None = None,
) -> dict[str, Any]:
"""Import a user-selected Redump or No-Intro XML DAT."""
db = db or DatabaseManager()
adapter = LocalDatAdapter(path, source_kind)
with db.get_connection() as conn:
repository = KnowledgeRepository(conn)
repository.ensure_schema()
summary = KnowledgeImportService(repository).run_adapter(adapter)
if summary.get("status") == "failed":
raise ValueError(str(summary.get("error") or "DAT import failed"))
return summary