welcomecenterbot/storage/__init__.py

38 lines
1.2 KiB
Python
Raw Normal View History

2023-09-11 18:40:24 +00:00
from redis import Redis
2023-09-11 20:21:55 +00:00
from storage.profile import Profile as ProfileObj
2024-01-06 11:25:35 +00:00
from bot.config import REDIS_URL
2023-04-28 12:24:14 +00:00
import json
2023-04-23 16:54:58 +00:00
2023-04-24 06:14:35 +00:00
# сохраняет сессии, айди кнопок в чатах для удаления и пересылаемые сообщения между перезагрузками
2023-09-11 18:40:24 +00:00
storage = Redis.from_url(REDIS_URL)
2023-04-23 16:54:58 +00:00
# хранение необходимой информации о пользователях
Profile = ProfileObj(storage)
2023-04-28 12:24:14 +00:00
2023-09-11 20:04:53 +00:00
2023-04-28 12:24:14 +00:00
# достаёт из хранилища jsonы по маске и количеству
2023-09-11 20:04:53 +00:00
def scan(match="usr-*", count=100):
2023-04-28 12:24:14 +00:00
cursor = 0
keys = []
r = storage
while True:
# Scan for keys starting with <match> in batches of <count>
cursor, batch_keys = r.scan(cursor=cursor, match=match, count=count)
keys += batch_keys
# If the cursor is 0, then we've reached the end of the keys
if cursor == 0:
break
# Get the values of all the keys
values = r.mget(keys)
# Parse the JSON data from each value
items = []
for value in values:
2023-09-18 07:50:48 +00:00
if value:
value_str = value.decode("utf-8")
i = json.loads(value_str)
items.append(i)
2023-09-11 20:04:53 +00:00
print(f"scan found {len(items)} items")
2023-04-28 12:24:14 +00:00
2023-09-11 20:04:53 +00:00
return keys, items