|
| 1 | +""" |
| 2 | +下载配额管理模块 |
| 3 | +
|
| 4 | +基于 SQLite 实现每用户每日下载次数限制。 |
| 5 | +用户标识使用 QQ 号(或其他平台的 user_id)。 |
| 6 | +""" |
| 7 | + |
| 8 | +import sqlite3 |
| 9 | +from datetime import date |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from astrbot.api import logger |
| 13 | + |
| 14 | + |
| 15 | +class DownloadQuotaManager: |
| 16 | + """下载配额管理器 - 基于 SQLite""" |
| 17 | + |
| 18 | + def __init__(self, db_path: Path): |
| 19 | + """ |
| 20 | + 初始化配额管理器 |
| 21 | +
|
| 22 | + Args: |
| 23 | + db_path: SQLite 数据库文件路径 |
| 24 | + """ |
| 25 | + self.db_path = db_path |
| 26 | + self._init_db() |
| 27 | + |
| 28 | + def _init_db(self): |
| 29 | + """初始化数据库表""" |
| 30 | + try: |
| 31 | + with self._get_connection() as conn: |
| 32 | + conn.execute(""" |
| 33 | + CREATE TABLE IF NOT EXISTS download_quota ( |
| 34 | + user_id TEXT NOT NULL, |
| 35 | + date TEXT NOT NULL, |
| 36 | + count INTEGER DEFAULT 0, |
| 37 | + PRIMARY KEY (user_id, date) |
| 38 | + ) |
| 39 | + """) |
| 40 | + conn.commit() |
| 41 | + except Exception as e: |
| 42 | + logger.error(f"初始化配额数据库失败: {e}") |
| 43 | + |
| 44 | + def _get_connection(self) -> sqlite3.Connection: |
| 45 | + """获取数据库连接""" |
| 46 | + return sqlite3.connect(self.db_path) |
| 47 | + |
| 48 | + def _get_today(self) -> str: |
| 49 | + """获取今天的日期字符串""" |
| 50 | + return date.today().isoformat() |
| 51 | + |
| 52 | + def get_used_count(self, user_id: str) -> int: |
| 53 | + """ |
| 54 | + 获取用户今日已使用次数 |
| 55 | +
|
| 56 | + Args: |
| 57 | + user_id: 用户 QQ 号 |
| 58 | +
|
| 59 | + Returns: |
| 60 | + 今日已使用次数 |
| 61 | + """ |
| 62 | + try: |
| 63 | + with self._get_connection() as conn: |
| 64 | + cursor = conn.execute( |
| 65 | + "SELECT count FROM download_quota WHERE user_id = ? AND date = ?", |
| 66 | + (str(user_id), self._get_today()), |
| 67 | + ) |
| 68 | + row = cursor.fetchone() |
| 69 | + return row[0] if row else 0 |
| 70 | + except Exception as e: |
| 71 | + logger.error(f"查询配额失败: {e}") |
| 72 | + return 0 |
| 73 | + |
| 74 | + def check_quota(self, user_id: str, limit: int) -> tuple[bool, int, int]: |
| 75 | + """ |
| 76 | + 检查用户是否可以下载 |
| 77 | +
|
| 78 | + Args: |
| 79 | + user_id: 用户 QQ 号 |
| 80 | + limit: 每日下载限制次数 |
| 81 | +
|
| 82 | + Returns: |
| 83 | + (是否可下载, 已用次数, 限制次数) |
| 84 | + """ |
| 85 | + if limit <= 0: |
| 86 | + return True, 0, 0 # 限制为 0 表示不限制 |
| 87 | + |
| 88 | + used = self.get_used_count(user_id) |
| 89 | + can_download = used < limit |
| 90 | + return can_download, used, limit |
| 91 | + |
| 92 | + def consume_quota(self, user_id: str) -> int: |
| 93 | + """ |
| 94 | + 消耗一次配额 |
| 95 | +
|
| 96 | + Args: |
| 97 | + user_id: 用户 QQ 号 |
| 98 | +
|
| 99 | + Returns: |
| 100 | + 消耗后的已用次数 |
| 101 | + """ |
| 102 | + try: |
| 103 | + today = self._get_today() |
| 104 | + with self._get_connection() as conn: |
| 105 | + # 使用 UPSERT 语法,原子操作 |
| 106 | + conn.execute( |
| 107 | + """ |
| 108 | + INSERT INTO download_quota (user_id, date, count) |
| 109 | + VALUES (?, ?, 1) |
| 110 | + ON CONFLICT(user_id, date) DO UPDATE SET count = count + 1 |
| 111 | + """, |
| 112 | + (str(user_id), today), |
| 113 | + ) |
| 114 | + conn.commit() |
| 115 | + return self.get_used_count(user_id) |
| 116 | + except Exception as e: |
| 117 | + logger.error(f"消耗配额失败: {e}") |
| 118 | + return 0 |
| 119 | + |
| 120 | + def get_remaining(self, user_id: str, limit: int) -> int | None: |
| 121 | + """ |
| 122 | + 获取剩余次数 |
| 123 | +
|
| 124 | + Args: |
| 125 | + user_id: 用户 QQ 号 |
| 126 | + limit: 每日下载限制次数 |
| 127 | +
|
| 128 | + Returns: |
| 129 | + 剩余次数,如果不限制则返回 None |
| 130 | + """ |
| 131 | + if limit <= 0: |
| 132 | + return None |
| 133 | + used = self.get_used_count(user_id) |
| 134 | + return max(0, limit - used) |
| 135 | + |
| 136 | + def cleanup_old_data(self, days: int = 7): |
| 137 | + """ |
| 138 | + 清理过期数据 |
| 139 | +
|
| 140 | + Args: |
| 141 | + days: 保留最近多少天的数据 |
| 142 | + """ |
| 143 | + try: |
| 144 | + with self._get_connection() as conn: |
| 145 | + conn.execute( |
| 146 | + "DELETE FROM download_quota WHERE date < date('now', ?)", |
| 147 | + (f"-{days} days",), |
| 148 | + ) |
| 149 | + conn.commit() |
| 150 | + logger.debug(f"已清理 {days} 天前的配额数据") |
| 151 | + except Exception as e: |
| 152 | + logger.error(f"清理配额数据失败: {e}") |
0 commit comments