Spaces:
Sleeping
Sleeping
Download core/database.py from OY-CODES/oy-stream-intelligence: direct link, hf CLI and curl.
- Browser
- Download file 4.09 kB
-
https://huggingface.co/spaces/OY-CODES/oy-stream-intelligence/resolve/main/core/database.py
- Command line
-
hf download hf://spaces/OY-CODES/oy-stream-intelligence/core/database.py
-
curl -L -o database.py https://huggingface.co/spaces/OY-CODES/oy-stream-intelligence/resolve/main/core/database.py
4.09 kB
| """ | |
| OY Stream — Database Engine | |
| SQLite with full API key lifecycle management | |
| """ | |
| import sqlite3 | |
| import json | |
| import secrets | |
| from datetime import datetime, timedelta | |
| from typing import List, Optional, Dict | |
| DB_PATH = "oy_intelligence.db" | |
| def get_db(): | |
| conn = sqlite3.connect(DB_PATH) | |
| conn.row_factory = sqlite3.Row | |
| return conn | |
| def init_db(): | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute('''CREATE TABLE IF NOT EXISTS api_keys | |
| (key TEXT PRIMARY KEY, | |
| owner TEXT, | |
| allowed_nodes TEXT, | |
| expires_at TIMESTAMP, | |
| is_active INTEGER DEFAULT 1, | |
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | |
| last_used TIMESTAMP, | |
| request_count INTEGER DEFAULT 0, | |
| plan TEXT DEFAULT 'basic')''') | |
| conn.commit() | |
| conn.close() | |
| def create_key(owner: str, nodes_list: List[str], days_valid: int, plan: str = 'basic'): | |
| key = f"oy_{secrets.token_hex(16)}" | |
| expiry = datetime.now() + timedelta(days=days_valid) | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute("""INSERT INTO api_keys (key, owner, allowed_nodes, expires_at, plan) | |
| VALUES (?, ?, ?, ?, ?)""", | |
| (key, owner, json.dumps(nodes_list), expiry, plan)) | |
| conn.commit() | |
| conn.close() | |
| return key, expiry | |
| def verify_key(key: str) -> Optional[List[str]]: | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute("SELECT allowed_nodes, expires_at, is_active FROM api_keys WHERE key = ?", (key,)) | |
| result = c.fetchone() | |
| conn.close() | |
| if result: | |
| allowed_nodes, expires_at, is_active = result['allowed_nodes'], result['expires_at'], result['is_active'] | |
| try: | |
| expiry_dt = datetime.fromisoformat(expires_at.split('.')[0]) | |
| except: | |
| expiry_dt = datetime.now() + timedelta(days=1) | |
| if is_active == 1 and expiry_dt > datetime.now(): | |
| return json.loads(allowed_nodes) | |
| return None | |
| def revoke_key(key: str): | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute("UPDATE api_keys SET is_active = 0 WHERE key = ?", (key,)) | |
| conn.commit() | |
| conn.close() | |
| def extend_key(key: str, extra_days: int): | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute("SELECT expires_at FROM api_keys WHERE key = ?", (key,)) | |
| res = c.fetchone() | |
| if res: | |
| current_expiry = datetime.fromisoformat(res['expires_at'].split('.')[0]) | |
| new_expiry = current_expiry + timedelta(days=extra_days) | |
| c.execute("UPDATE api_keys SET expires_at = ? WHERE key = ?", (new_expiry, key)) | |
| conn.commit() | |
| conn.close() | |
| def edit_key_nodes(key: str, new_nodes_list: List[str]): | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute("UPDATE api_keys SET allowed_nodes = ? WHERE key = ?", (json.dumps(new_nodes_list), key)) | |
| conn.commit() | |
| conn.close() | |
| def increment_usage(key: str): | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute("UPDATE api_keys SET request_count = request_count + 1, last_used = ? WHERE key = ?", | |
| (datetime.now(), key)) | |
| conn.commit() | |
| conn.close() | |
| def get_all_keys() -> List[Dict]: | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute("SELECT * FROM api_keys ORDER BY created_at DESC") | |
| rows = c.fetchall() | |
| conn.close() | |
| return [dict(row) for row in rows] | |
| def get_key_stats(key: str) -> Optional[Dict]: | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute("SELECT * FROM api_keys WHERE key = ?", (key,)) | |
| row = c.fetchone() | |
| conn.close() | |
| return dict(row) if row else None | |
| def get_system_stats() -> Dict: | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute("SELECT COUNT(*) as total, SUM(CASE WHEN is_active=1 THEN 1 ELSE 0 END) as active, SUM(request_count) as reqs FROM api_keys") | |
| res = c.fetchone() | |
| conn.close() | |
| return { | |
| "total_keys": res['total'] or 0, | |
| "active_keys": res['active'] or 0, | |
| "revoked_keys": (res['total'] or 0) - (res['active'] or 0), | |
| "total_requests": res['reqs'] or 0 | |
| } | |
| init_db() |