-
Notifications
You must be signed in to change notification settings - Fork 9
/
qq_context_requests.py
44 lines (37 loc) · 1.24 KB
/
qq_context_requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from enum import IntEnum, unique
from typing import Dict, Optional
from datetime import datetime
@unique
class RegionEnum(IntEnum):
Unknown = 1
Bilibili = 1 << 1
Taiwan = 1 << 2
Japan = 1 << 3
All = 1 << 4
class QueryRequestContext:
def __init__(self, region: RegionEnum = RegionEnum.Unknown):
self.time = datetime.now()
self.region = region
gs_qqid2request: Dict[int, QueryRequestContext] = {}
gs_seconds_to_wait = 60
_gs_last_clean_time = datetime.now()
def _ClearOldRequests():
current_time = datetime.now()
if (current_time - _gs_last_clean_time).seconds < 599:
return
to_delete = [qqid for qqid, context in gs_qqid2request.items()
if (current_time - context.time).seconds >= gs_seconds_to_wait]
for qqid in to_delete:
gs_qqid2request.pop(qqid, None)
def GetRequest(qqid: int) -> Optional[QueryRequestContext]:
_ClearOldRequests()
if qqid not in gs_qqid2request:
return None
req = gs_qqid2request[qqid]
if (datetime.now() - req.time).seconds >= gs_seconds_to_wait:
return None
return req
def PopRequest(qqid: int) -> Optional[QueryRequestContext]:
req = GetRequest(qqid)
gs_qqid2request.pop(qqid, None)
return req