forked from f0rthsp4ce/botka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresidents-admin-table.py
executable file
·210 lines (168 loc) · 6.17 KB
/
residents-admin-table.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python3
from __future__ import annotations
import asyncio
import sqlite3
import sys
import traceback
import typing
import telethon
import telethon.hints
import telethon.tl.functions.channels
import telethon.tl.types
import yaml
API_ID = 27161938
API_HASH = "25540bdf9a27dc0da066770a1d5b12c5"
DB_FILENAME = "db.sqlite3"
SESSION_NAME = "session"
TypeParticipant: typing.TypeAlias = (
telethon.tl.types.TypeChannelParticipant
| telethon.tl.types.TypeChatParticipant
)
class WatchingChat(typing.TypedDict):
id: int
internal: bool
async def main() -> None:
with open(sys.argv[1]) as f: # noqa: ASYNC101
config = yaml.safe_load(f)
db = sqlite3.connect(f"file:{DB_FILENAME}?mode=ro", uri=True)
client = await telethon.TelegramClient(
SESSION_NAME, API_ID, API_HASH
).start(bot_token=config["telegram"]["token"])
watching_chats = config["telegram"]["chats"]["resident_owned"]
async with client:
res = await fetch_residents_chats_table(client, watching_chats)
print_results(res, db_load_residents(db))
class ResidentsChatsTable(typing.NamedTuple):
chats: list[FetchChatResult]
users: dict[int, telethon.tl.types.User]
errors: list[int]
class FetchChatResult(typing.NamedTuple):
chat: telethon.tl.types.Chat | telethon.tl.types.Channel
is_internal: bool
participants: dict[int, tuple[telethon.tl.types.User, TypeParticipant]]
async def fetch_residents_chats_table(
client: telethon.TelegramClient,
watching_chats: list[WatchingChat],
) -> ResidentsChatsTable:
result = ResidentsChatsTable([], {}, [])
for wc, ch in zip(
watching_chats,
await asyncio.gather(
*(fetch_chat(client, ch) for ch in watching_chats),
return_exceptions=True,
),
):
if isinstance(ch, FetchChatResult):
result.chats.append(ch)
for u, _ in ch.participants.values():
result.users[u.id] = u
else:
traceback.print_exception(type(ch), ch, ch.__traceback__)
result.errors.append(wc["id"])
return result
async def fetch_chat(
client: telethon.TelegramClient, ch: WatchingChat
) -> FetchChatResult:
chat = await client.get_entity(ch["id"])
if isinstance(chat, telethon.tl.types.User):
msg = "User is not supported"
raise TypeError(msg)
result = FetchChatResult(chat, ch["internal"], {})
async for user in client.iter_participants(
chat,
filter=None
if ch["internal"]
else telethon.tl.types.ChannelParticipantsAdmins(),
):
result.participants[user.id] = (user, user.participant)
return result
def print_results( # noqa: C901 PLR0912
result: ResidentsChatsTable, resident_ids: list[int]
) -> None:
def key(user_id: int) -> tuple[int, str, int]:
if user_id in resident_ids:
return (0, "Residents", resident_ids.index(user_id))
user = result.users.get(user_id)
if user is not None and user.bot is True:
return (1, "Bots", user_id)
return (2, "Non-residents", user_id)
first = True
prev_table = None
for user_id in sorted(result.users.keys() | set(resident_ids), key=key):
# Print table header
curr_table = key(user_id)[1]
if curr_table != prev_table:
if not first:
print()
first = False
row = [f"{n}\ufe0f\u20e3" for n in range(len(result.chats))]
print(f"{format_row(row)} <b>{curr_table}</b>")
prev_table = curr_table
row = [format_participant(user_id, ch) for ch in result.chats]
print(end=format_row(row) + " ")
if (user := result.users.get(user_id)) is None:
print(end=f"id={user_id}")
else:
if user.username:
print(end=f'<a href="https://t.me/{user.username}">')
print(end=escape_html(user.first_name or ""))
if user.last_name:
print(end=" " + escape_html(user.last_name))
if user.username:
print(end="</a>")
print()
print()
print("<b>Legend</b>")
for n, ch in enumerate(result.chats):
print(
end=format_row(
[
"〰️" if ni < n else f"{n}\ufe0f\u20e3" if ni == n else ""
for ni in range(len(result.chats))
]
).rstrip()
)
print(end=' — <a href="https://t.me/')
if isinstance(ch, telethon.tl.types.Channel) and ch.username:
print(end=ch.username)
else:
print(end=f"c/{ch.chat.id}")
print(end=f'">{escape_html(ch.chat.title)}</a>')
if not ch.is_internal:
print(end=" (public)")
print()
print("👑 — owner, ⭐ — admin, 👤 — participant/subscriber")
print("➖ — not present (or not admin for public chats)")
if result.errors:
print(f"\n⚠️ got errors while fetching chats with ids {result.errors}")
def format_row(items: list[str]) -> str:
middle = len(items) // 2
return "".join(items[0:middle]) + " " + "".join(items[middle:])
t = telethon.tl.types
PARTICIPANT_TYPES = [
(None | t.ChannelParticipantBanned | t.ChannelParticipantLeft, "➖"),
(t.ChannelParticipant | t.ChatParticipant | t.ChatParticipant, "👤"),
(t.ChannelParticipantCreator | t.ChatParticipantCreator, "👑"),
(t.ChannelParticipantAdmin | t.ChatParticipantAdmin, "⭐"),
(t.ChannelParticipantSelf, "❓"),
]
def format_participant(user_id: int, ch: FetchChatResult) -> str:
p = ch.participants.get(user_id)
p = p[1] if p else None
return next((s for t, s in PARTICIPANT_TYPES if isinstance(p, t)), "❓")
def escape_html(s: str) -> str:
return s.replace("&", "&").replace("<", "<").replace(">", ">")
def db_load_residents(db: sqlite3.Connection) -> list[int]:
return [
row[0]
for row in db.execute(
r"""
SELECT tg_id
FROM residents
WHERE end_date IS NULL
ORDER BY begin_date DESC
"""
).fetchall()
]
if __name__ == "__main__":
asyncio.run(main())