#!/usr/bin/env python3 """Test MTGJSON API endpoints and download JSON files.""" import asyncio import aiohttp from pathlib import Path DATA_DIR = Path("/app/data/mtgjson") DATA_DIR.mkdir(parents=True, exist_ok=True) # MTGJSON API endpoints for .json files MTGJSON_BASE_URL = "https://mtgjson.com/api/v5" JSON_FILES = { "AllPrintings.json": MTGJSON_BASE_URL + "/AllPrintings.json", "AllSetFiles.json": MTGJSON_BASE_URL + "/AllSetFiles.json", "AllIdentifiers.json": MTGJSON_BASE_URL + "/AllIdentifiers.json", "CardTypes.json": MTGJSON_BASE_URL + "/CardTypes.json", "DeckList.json": MTGJSON_BASE_URL + "/DeckList.json", "Keywords.json": MTGJSON_BASE_URL + "/Keywords.json", "SetList.json": MTGJSON_BASE_URL + "/SetList.json", } async def test_download_file(session, url, filename): """Test downloading a single JSON file.""" dest_path = DATA_DIR / filename print(f"Testing {filename}...") try: async with session.get(url) as response: if response.status == 200: content = await response.read() print(f" ✓ Status: {response.status}") print(f" ✓ Size: {len(content) / (1024*1024):.2f} MB") print(f" ✓ Content-Type: {response.content_type}") # Write to file with open(dest_path, 'wb') as f: f.write(content) print(f" ✓ Saved to {dest_path}") # Test JSON parsing import json with open(dest_path, 'r', encoding='utf-8') as f: data = json.load(f) print(f" ✓ Valid JSON: {type(data).__name__}") return True else: print(f" ✗ Status: {response.status}") return False except Exception as e: print(f" ✗ Error: {e}") return False async def main(): """Test all MTGJSON JSON endpoints.""" print("Testing MTGJSON JSON API endpoints...\n") async with aiohttp.ClientSession() as session: results = [] for filename, url in JSON_FILES.items(): success = await test_download_file(session, url, filename) results.append((filename, success)) print() print("\n=== Results ===") for filename, success in results: status = "✓" if success else "✗" print(f"{status} {filename}") if __name__ == "__main__": asyncio.run(main())