-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserparser.py
More file actions
173 lines (166 loc) · 6.53 KB
/
Copy pathuserparser.py
File metadata and controls
173 lines (166 loc) · 6.53 KB
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
# На модуль распространяется лицензия "GNU General Public License v3.0"
# https://github.com/all-licenses/GNU-General-Public-License-v3.0
# -*- coding: utf-8 -*-
# Name: UserParser
# Description: Данный модуль позволяет копировать ID, Username и Name участников чата при помощи команды .userpars
# meta developer: @PyModule
# meta fhsdesc: tool, tools, id, parser, userparser
from .. import loader, utils
import json
import os
class UserIDParserMod(loader.Module):
"""Парсер ID, имени, фамилии и юзернейма пользователей с выбором формата файла"""
strings = {
"name": "UserParser",
"format_set": "<emoji document_id=5206607081334906820>✔️</emoji> <b>Формат файла успешно установлен на: {}</b>",
"invalid_format": "<emoji document_id=5274099962655816924>❗️</emoji> <b>Неверный формат! Используйте: json, txt или html.</b>",
}
def __init__(self):
self.file_format = "json"
async def client_ready(self, client, db):
self.client = client
self.db = db
saved_format = self.db.get("UserParser", "file_format", None)
if saved_format:
self.file_format = saved_format
async def formatparscmd(self, message):
"""Устанавливает формат файла: json, txt или html"""
args = utils.get_args_raw(message)
if args and args.lower() in ["json", "txt", "html"]:
self.file_format = args.lower()
self.db.set("UserParser", "file_format", self.file_format)
await message.edit(self.strings["format_set"].format(self.file_format))
else:
await message.edit(self.strings["invalid_format"])
async def userparscmd(self, message):
"""Собирает информацию о пользователях из чата и сохраняет в файл"""
chat = message.chat
if not chat:
await message.edit("<emoji document_id=5210952531676504517>❌</emoji> <b>Это не чат!</b>")
return
user_data = []
async for user in self.client.iter_participants(chat.id):
user_info = {
"id": user.id,
"username": user.username or "None",
"first_name": user.first_name or "None",
"last_name": user.last_name or "None"
}
user_data.append(user_info)
chat_title = chat.title or "Без названия"
chat_id = chat.id
chat_info = f"Чат: {chat_title}\nID чата: {chat_id}"
file_format = self.file_format
if file_format == "json":
file_path = "user_data.json"
with open(file_path, "w", encoding="utf-8") as f:
json.dump(user_data, f, indent=4, ensure_ascii=False)
caption = f"Список пользователей из чата (JSON):\n{chat_info}"
elif file_format == "txt":
file_path = "user_data.txt"
with open(file_path, "w", encoding="utf-8") as f:
f.write(f"{chat_info}\n\n")
for user in user_data:
f.write(
f"ID: {user['id']}, "
f"Username: {user['username']}, "
f"Имя: {user['first_name']}, "
f"Фамилия: {user['last_name']}\n"
)
caption = f"Список пользователей из чата (TXT):\n{chat_info}"
elif file_format == "html":
file_path = "user_data.html"
with open(file_path, "w", encoding="utf-8") as f:
f.write(f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Список пользователей</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f9f9f9;
}}
h1 {{
text-align: center;
color: #333;
}}
p {{
font-size: 16px;
color: #555;
}}
table {{
width: 100%;
border-collapse: collapse;
margin: 20px 0;
font-size: 16px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}}
th, td {{
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}}
th {{
background-color: #4CAF50;
color: white;
font-weight: bold;
}}
tr:nth-child(even) {{
background-color: #f2f2f2;
}}
tr:hover {{
background-color: #ddd;
}}
.bold {{
font-weight: bold;
}}
.italic {{
font-style: italic;
}}
.underline {{
text-decoration: underline;
}}
.highlight {{
background-color: yellow;
}}
</style>
</head>
<body>
<h1 class="bold">Список пользователей из чата</h1>
<p><strong>Чат:</strong> <span class="italic">{chat_title}</span></p>
<p><strong>ID чата:</strong> <span class="underline">{chat_id}</span></p>
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Имя</th>
<th>Фамилия</th>
</tr>
""")
for user in user_data:
f.write(f"""
<tr>
<td class="bold">{user['id']}</td>
<td class="italic">{user['username']}</td>
<td class="underline">{user['first_name']}</td>
<td class="highlight">{user['last_name']}</td>
</tr>
""")
f.write("""
</table>
</body>
</html>
""")
caption = f"Список пользователей из чата (HTML):\n{chat_info}"
else:
await message.edit("<emoji document_id=5274099962655816924>❗️</emoji> <b>Неверный формат файла! Укажите 'json', 'txt' или 'html' с помощью команды .formatpars.</b>")
return
await self.client.send_file("me", file_path, caption=caption)
os.remove(file_path)
await message.edit("<emoji document_id=5206607081334906820>✔️</emoji> <b>Успешно!</b>")