- export_tld_prices.py: Export DB prices to JSON - import_tld_prices.py: Import prices from JSON - seed_tld_prices.py: Initial scrape from Porkbun API - tld_prices_export.json: Current price data (886 TLDs) Usage: python scripts/seed_tld_prices.py # Initial scrape python scripts/export_tld_prices.py # Backup to JSON python scripts/import_tld_prices.py # Restore from JSON
63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Import TLD prices from JSON file to database."""
|
|
import asyncio
|
|
import json
|
|
import sys
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
# Add parent directory to path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from app.database import AsyncSessionLocal, init_db
|
|
from app.models.tld_price import TLDPrice
|
|
|
|
|
|
async def import_prices(json_path: str = None):
|
|
"""Import TLD prices from JSON."""
|
|
await init_db()
|
|
|
|
# Default path
|
|
if json_path is None:
|
|
json_path = Path(__file__).parent / "tld_prices_export.json"
|
|
else:
|
|
json_path = Path(json_path)
|
|
|
|
if not json_path.exists():
|
|
print(f"File not found: {json_path}")
|
|
return
|
|
|
|
with open(json_path) as f:
|
|
data = json.load(f)
|
|
|
|
prices = data.get("prices", [])
|
|
print(f"Importing {len(prices)} TLD prices from {json_path}")
|
|
|
|
async with AsyncSessionLocal() as db:
|
|
imported = 0
|
|
for p in prices:
|
|
try:
|
|
price = TLDPrice(
|
|
tld=p["tld"],
|
|
registrar=p["registrar"],
|
|
registration_price=p["registration_price"],
|
|
renewal_price=p.get("renewal_price"),
|
|
transfer_price=p.get("transfer_price"),
|
|
currency=p.get("currency", "USD"),
|
|
promo_price=p.get("promo_price"),
|
|
recorded_at=datetime.fromisoformat(p["recorded_at"]) if p.get("recorded_at") else datetime.utcnow(),
|
|
)
|
|
db.add(price)
|
|
imported += 1
|
|
except Exception as e:
|
|
print(f"Error importing {p.get('tld')}: {e}")
|
|
|
|
await db.commit()
|
|
print(f"Successfully imported {imported} TLD prices")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
json_file = sys.argv[1] if len(sys.argv) > 1 else None
|
|
asyncio.run(import_prices(json_file))
|
|
|