Files
namecheap-api/db.py
kaffa 896699535d Initial commit: Namecheap API library with REST/MCP servers
Features:
- Domain management (check, register, renew, contacts)
- DNS management (nameservers, records)
- Glue records (child nameserver) support
- TLD price tracking with KRW conversion
- FastAPI REST server with OpenAI schema
- MCP server for Claude integration

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-15 10:21:46 +09:00

63 lines
1.5 KiB
Python

"""
Database module for TLD prices
"""
import sqlite3
from pathlib import Path
DB_PATH = Path(__file__).parent / "prices.db"
def get_connection():
return sqlite3.connect(DB_PATH)
def init_db():
"""Initialize database and create tables"""
conn = get_connection()
conn.execute("""
CREATE TABLE IF NOT EXISTS tld_prices (
tld TEXT PRIMARY KEY,
usd REAL NOT NULL,
krw INTEGER NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS exchange_rates (
currency TEXT PRIMARY KEY,
rate REAL NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
conn.close()
def get_prices() -> list[dict]:
"""Get all TLD prices"""
conn = get_connection()
conn.row_factory = sqlite3.Row
cursor = conn.execute(
"SELECT tld, usd, krw, updated_at FROM tld_prices ORDER BY krw"
)
results = [dict(row) for row in cursor.fetchall()]
conn.close()
return results
def get_exchange_rate(currency: str = "KRW") -> float | None:
"""Get exchange rate for currency"""
conn = get_connection()
cursor = conn.execute(
"SELECT rate FROM exchange_rates WHERE currency = ?", (currency,)
)
row = cursor.fetchone()
conn.close()
return row[0] if row else None
if __name__ == "__main__":
init_db()
print(f"Database initialized: {DB_PATH}")