""" scripts/fix_rome_streetz.py Downloads the verified Bing portrait of Rome Streetz using exact browser-session emulation headers to avoid ConnectionResetError (10054). """ import sqlite3 import requests import os import time from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent DB_PATH = BASE_DIR / "data" / "hiffi_catalog.db" AVATAR_DIR = BASE_DIR / "data" / "artist_avatars" AVATAR_DIR.mkdir(parents=True, exist_ok=True) TARGET_URL = "https://tse1.mm.bing.net/th/id/OIP.tdiubvGo93mmUghZU5eEyQHaJu?r=0&pid=Api&h=220&P=0" # Strict browser negotiation headers to satisfy Bing CDN security filters BROWSER_HEADERS = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36", "Accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.9", "Referer": "https://www.bing.com/", "Sec-Ch-Ua": '"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"', "Sec-Ch-Ua-Mobile": "?0", "Sec-Ch-Ua-Platform": '"Windows"', "Sec-Fetch-Dest": "image", "Sec-Fetch-Mode": "no-cors", "Sec-Fetch-Site": "cross-site", } def main(): if not DB_PATH.exists(): print(f"[-] Database not found at {DB_PATH}") return conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() cursor.execute("SELECT artist_id FROM artists WHERE artist_name = 'Rome Streetz' COLLATE NOCASE") row = cursor.fetchone() artist_id = str(row[0]) if row else "13047" target_path = AVATAR_DIR / f"rome_streetz_{artist_id}.jpg" print("[*] Fetching verified Rome Streetz portrait via browser session...") session = requests.Session() session.headers.update(BROWSER_HEADERS) success = False for attempt in range(1, 4): try: time.sleep(0.5) res = session.get(TARGET_URL, timeout=12) if res.status_code == 200 and len(res.content) > 2000: with open(target_path, "wb") as f: f.write(res.content) size_kb = len(res.content) / 1024 print(f"[+] SAVED -> {target_path.name} ({size_kb:.1f} KB)") cursor.execute( "UPDATE artists SET image_url = ?, image_verified_flag = 1 WHERE artist_id = ?", (TARGET_URL, artist_id) ) conn.commit() success = True break else: print(f"[-] Attempt {attempt} returned HTTP {res.status_code}") except Exception as e: print(f"[-] Attempt {attempt} failed: {e}") time.sleep(1.0) conn.close() if not success: print("[-] Download could not complete. Check connectivity.") if __name__ == "__main__": main()