Malits/Av1api
0
1import sqlite32import asyncio3import aiohttp4import sys5import os6 7# Set working directory to project root in case it's run from subdirs8sys.path.append(os.path.dirname(os.path.abspath(__file__)))9 10from api_modules.db import DB_NAME, _db11from api_modules.enrich import enrich_anime_by_slug12 13CONCURRENCY = 114 15async def enrich_item(slug: str, title: str):16 print(f"→ Enriching '{title}' (slug: {slug})...")17 try:18 success, result = await enrich_anime_by_slug(slug, title)19 if success:20 romaji = result.get('title', {}).get('romaji') if isinstance(result, dict) else "Unknown"21 print(f" [✓] Success: '{title}' (slug: {slug}) → {romaji}")22 else:23 print(f" [✗] Failed: {result}")24 except Exception as e:25 print(f" [✗] Error enriching {slug}: {e}")26 27async def main():28 conn = _db()29 cursor = conn.cursor()30 try:31 cursor.execute("SELECT slug, title FROM anime_index WHERE native_title IS NULL")32 except sqlite3.OperationalError:33 from Av1Indexer import init_db34 init_db()35 cursor.execute("SELECT slug, title FROM anime_index WHERE native_title IS NULL")36 37 rows = cursor.fetchall()38 conn.close()39 40 if not rows:41 print("✓ Database is already enriched.")42 return43 44 print(f"→ Phase 3: Enriching {len(rows)} entries using modularized scraper logic...")45 46 sem = asyncio.Semaphore(3)47 48 async def worker(slug, title):49 async with sem:50 await enrich_item(slug, title)51 await asyncio.sleep(0.75)52 53 tasks = [worker(slug, title) for slug, title in rows]54 await asyncio.gather(*tasks)55 56 # Close the global scrapers HTTP session to prevent "Unclosed client session" warnings57 import api_modules.scrapers as scrapers58 if scrapers._session and not scrapers._session.closed:59 await scrapers._session.close()60 61 print(f"\n✓ Enrichment complete.")62 63if __name__ == "__main__":64 asyncio.run(main())65 66 