Files

60 lines
1.5 KiB
Python
Raw Permalink Normal View History

"""搜图 —— 运行态(Service 层:每日限流 + 批量搜索会话)。"""
from __future__ import annotations
from datetime import datetime, timedelta
from ..config import DAILY_LIMIT, SEARCH_TIMEOUT
class DailyNumberLimiter:
"""每日次数限制器(按年积日计数)"""
def __init__(self, max_num):
self.max = max_num
self.today = -1
self.count = {}
def check(self, key) -> bool:
day = datetime.now().timetuple().tm_yday
if day != self.today:
self.today = day
self.count.clear()
return self.count.get(key, 0) < self.max
def get_num(self, key):
return self.count.get(key, 0)
def increase(self, key, num=1):
self.count[key] = self.count.get(key, 0) + num
class PicListener:
def __init__(self):
self.on = {}
self.count = {}
self.limit = {}
self.timeout = {}
def get_on_off_status(self, gid):
return self.on[gid] if self.on.get(gid) is not None else False
def turn_on(self, gid, uid):
self.on[gid] = uid
self.timeout[gid] = datetime.now() + timedelta(seconds=SEARCH_TIMEOUT)
self.count[gid] = 0
self.limit[gid] = DAILY_LIMIT - lmtd.get_num(uid)
def turn_off(self, gid):
self.on.pop(gid)
self.count.pop(gid)
self.timeout.pop(gid)
self.limit.pop(gid)
def count_plus(self, gid):
self.count[gid] += 1
lmtd = DailyNumberLimiter(DAILY_LIMIT)
pls = PicListener()