2023-09-14 13:24:36 +00:00
|
|
|
from typing import List, Dict
|
|
|
|
|
|
|
|
from cashews import cache
|
|
|
|
from telethon import TelegramClient
|
2023-09-14 13:43:30 +00:00
|
|
|
from telethon.errors import StatsMigrateError
|
2023-09-14 13:24:36 +00:00
|
|
|
from telethon.tl.functions.stats import GetMegagroupStatsRequest
|
|
|
|
from telethon.utils import get_input_channel
|
|
|
|
from telethon.tl.types.stats import MegagroupStats
|
|
|
|
|
|
|
|
cache.setup("mem://")
|
|
|
|
|
|
|
|
|
|
|
|
@cache(ttl="1h", key="{cid}")
|
|
|
|
async def get_group_data(cid: int, client: TelegramClient) -> List[Dict]:
|
|
|
|
group = get_input_channel(await client.get_input_entity(cid))
|
2023-09-14 13:43:30 +00:00
|
|
|
try:
|
|
|
|
result: MegagroupStats = await client(GetMegagroupStatsRequest(
|
|
|
|
channel=group,
|
|
|
|
dark=True
|
|
|
|
))
|
|
|
|
except StatsMigrateError as e:
|
|
|
|
sender = await client._borrow_exported_sender(e.dc)
|
|
|
|
result: MegagroupStats = await client._call(sender, GetMegagroupStatsRequest(
|
|
|
|
channel=group,
|
|
|
|
dark=True
|
|
|
|
))
|
2023-09-14 13:24:36 +00:00
|
|
|
return [i.to_dict() for i in result.top_posters]
|