add verbose and maxCache to tdlib

This commit is contained in:
2021-12-12 11:18:21 +08:00
parent bf89adef7f
commit a40a715f14
2 changed files with 22 additions and 2 deletions

View File

@@ -149,10 +149,16 @@ class TdLibJSONEncoder(JSONEncoder):
class TdLib:
def __init__(self) -> None:
def __init__(self, verbose: bool = False, maxCache: int = None) -> None:
self._initalized = False
self._destoried = False
self._db_initalized = False
self._v = verbose
self._maxCache = 1000
if maxCache is not None:
if maxCache < 100:
raise ValueError('At least cache 100 messages.')
self._maxCache = maxCache
self._e = 0
self._re = {}
self._rel = []
@@ -181,6 +187,10 @@ class TdLib:
await self._thread.killed()
_td_json_client_destroy(self._client_id)
self._destoried = True
if self._v:
le1 = len(self._re)
le2 = len(self._rel)
print(f"There are {le1 + le2} ({le1} + {le2}) messages in cache.")
del self._re
del self._rel
@@ -201,9 +211,17 @@ class TdLib:
if result:
de = TdLibJSONDecoder()
re = de.decode(result.decode())
if self._v:
if '@type' in re:
print(f'Get message type: {re["@type"]}')
if '@extra' in re:
self._re[re['@extra']] = re
else:
while len(self._rel) >= self._maxCache:
m = self._rel.pop(0)
if self._v:
t = m['@type'] if '@type' in m else 'Unknown'
print(f'Discard a message in cache. Message type: {t}')
self._rel.append(re)
async def addProxy(self, server, port, enable, type):

View File

@@ -91,11 +91,13 @@ p.add_argument('chat_id', nargs='?', type=int, help="Specify the chat's ID.")
p.add_argument('-n', '--chat-name', help="Specify chat's name. Will used if chat_id is not sepcified.", metavar='NAME', dest='chat_name') # noqa: E501
p.add_argument('-s', '--start-time', type=tparse, metavar='TIME', help="The messages which are sended before this time will not be deleted.", dest='start_time') # noqa: E501
p.add_argument('-e', '--end-time', type=tparse, metavar='TIME', help="The messages which are sended after this time will not be deleted.", dest="end_time") # noqa: E501
p.add_argument('-v', '--verbose', action='store_true', help="Enable verbose logging.", dest="verbose") # noqa: E501
p.add_argument('--max-cache', metavar='COUNT', type=int, help='Specify the max count of cache messages. At least 100.', dest="max_cache") # noqa: E501
arg = p.parse_intermixed_args()
if arg.chat_id is None and arg.chat_name is None:
raise ValueError('chat_id or chat_name is needed.')
try:
with TdLib() as lib:
with TdLib(arg.verbose, arg.max_cache) as lib:
re = asyncio.run(main(lib, arg))
sys.exit(re)
except Exception: