-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtg-chats-users-import.py
executable file
·188 lines (148 loc) · 7.28 KB
/
tg-chats-users-import.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
#!/usr/bin/env python3
# Connect to Telegram as user (Client API)
from telethon import TelegramClient, events, sync, connection, types
# Adding someone else to such chat or channel
from telethon.tl.functions.channels import InviteToChannelRequest
# Filter chat admins
from telethon.tl.types import ChannelParticipantsAdmins
# Filter chat bots
#from telethon.tl.types import ChannelParticipantsBots
from pathlib import Path
import logging
# Defaults
proxy_host = ''
proxy_port = 443
proxy_secret = '00000000000000000000000000000000'
# Load config
from config import *
# Setup Logger configuration (console output)
log = logging.getLogger()
log.setLevel(logging.getLevelName(log_level or 'DEBUG'))
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)-5s] %(message)s', '%d.%m.%Y %H:%M:%S'))
log.addHandler(consoleHandler)
if log_path:
fileHandler = logging.FileHandler("{0}/{1}.log".format(log_path or Path(__file__).resolve().parent, Path(log_name).stem or Path(__file__).stem))
fileHandler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)-5s - %(message)s', '%d.%m.%Y %H:%M:%S'))
log.addHandler(fileHandler)
def filter_users(users, exclude_hidden:bool=True, exclude_bots:bool=True, exclude_deleted:bool=True, exclude_offline:int=30, users_blacklist:list=[], phase:str='FILTER'):
from datetime import date
log.debug(f"✔ [{phase}] Exclude: hidden usernames = {exclude_hidden}, bots = {exclude_bots}, deleted users = {exclude_deleted}, offline users = {exclude_offline} day(s), blacklist = {users_blacklist}")
usernames_hidden_ids = []
usernames_bots = []
usernames_deleted = []
usernames_offline = []
usernames_blacklist = []
usernames_collected = []
try:
for user in users:
# Calculate when user was online
if isinstance(user.status, types.UserStatusOffline):
user_was_online = date.today() - date(user.status.was_online.year, user.status.was_online.month, user.status.was_online.day)
# Exclude users with hidden username
if user.username is None and exclude_hidden:
usernames_hidden_ids.append(user.id)
continue
# Exclude deleted users
elif user.deleted and exclude_deleted:
usernames_deleted.append(user.username)
continue
# Exclude bots
elif user.bot and exclude_bots:
usernames_bots.append(user.username)
continue
# Exclude users, which was online a long time ago
elif isinstance(user.status, types.UserStatusOffline) and exclude_offline > 0 and user_was_online.days > exclude_offline:
usernames_offline.append(user.username)
continue
# Exclude users from blacklist
elif user.username in users_blacklist:
usernames_blacklist.append(user.username)
continue
# Perfect users
else:
usernames_collected.append(user.username)
log.info (f"✔ [{phase}] Hidden usernames excluded total: {len(usernames_hidden_ids)}")
log.debug(f"✔ [{phase}] Hidden usernames excluded id list: {usernames_hidden_ids}")
log.info (f"✔ [{phase}] Deleted users excluded total: {len(usernames_deleted)}")
log.debug(f"✔ [{phase}] Deleted users excluded list: {usernames_deleted}")
log.info (f"✔ [{phase}] Bots excluded total: {len(usernames_bots)}")
log.debug(f"✔ [{phase}] Bots excluded list: {usernames_bots}")
if exclude_offline > 0:
log.info (f"✔ [{phase}] Offline more than {exclude_offline} day(s) users excluded total: {len(usernames_offline)}")
log.debug(f"✔ [{phase}] Offline users list: {usernames_offline}")
log.info (f"✔ [{phase}] Blacklist excluded total: {len(usernames_blacklist)}")
log.debug(f"✔ [{phase}] Blacklist excluded list: {usernames_blacklist}")
log.info (f"✔ [{phase}] Total excluded: {len(usernames_hidden_ids) + len(usernames_deleted) + len(usernames_bots) + len(usernames_offline) + len(usernames_blacklist)}")
log.info (f"✔ [{phase}] Total collected: {len(usernames_collected)}")
log.debug(f"✔ [{phase}] Collected usernames list: {usernames_collected}")
return usernames_collected
except Exception as e:
log.error(f"■ [{phase}] Problem while try to filter users")
log.debug(f"■ [{phase}] Exception: \n{e}")
log.info("▶ Start script")
if proxy_host:
client = TelegramClient(
api_id,
api_id, api_hash,
proxy=(proxy_host, proxy_port, proxy_secret),
connection=connection.tcpmtproxy.ConnectionTcpMTProxyRandomizedIntermediate
)
else:
client = TelegramClient(
api_id,
api_id, api_hash
)
client.start()
usernames_import = []
for chat in export_chats:
usernames_export = []
log.info(f"✔ [EXPORT] Parse all users from chat @{chat} ...")
users = client.get_participants(chat)
log.info (f"✔ [EXPORT] Users parsed: {len(users)}")
log.debug(f"✔ [EXPORT] Users: \n{users}")
usernames_export = filter_users(
users=users,
exclude_bots=exclude_bots,
exclude_deleted=exclude_deleted,
exclude_offline=exclude_offline_days,
users_blacklist=usernames_blacklist
)
# Exclude chat admins
if exclude_admins:
usernames_admins = []
for user in client.iter_participants(chat, filter=ChannelParticipantsAdmins):
if user.username in usernames_export:
usernames_export.remove(user.username)
usernames_admins.append(user.username)
log.info (f"✔ [FILTER] Admins usernames excluded total: {len(usernames_admins)}")
log.debug(f"✔ [FILTER] Admins usernames excluded list: {usernames_admins}")
usernames_import = usernames_import + usernames_export
log.info(f"✔ [FILTER] After filter: {len(usernames_import)}")
if len(export_chats) > 1:
log.info("✔ [DEDUP] Deduplicate users from all chats...")
before_dedup = len(usernames_import)
usernames_import = list(set(usernames_import))
log.info(f"✔ [DEDUP] After deduplication: {len(usernames_import)} (duplicates: {before_dedup - len(usernames_import)})")
log.info(f"✔ [COMPARE] Parse all users from target chat @{target_chat} ...")
users = client.get_participants(target_chat)
usernames_target = []
if len(users) > 0:
usernames_target = filter_users(
users=users,
exclude_bots=exclude_deleted,
exclude_deleted=exclude_deleted,
exclude_offline=0,
phase='COMPARE'
)
log.info("✔ [COMPARE] Exclude from import users, which already exist in target chat...")
usernames_import = list( set(usernames_import) - set(usernames_target) )
log.info(f"✔ [COMPARE] TOTAL: {len(usernames_import)}")
if import_limit > 0:
log.info(f"✔ [IMPORT] Add {import_limit} user(s) to chat @{target_chat}")
usernames_import = usernames_import[:import_limit]
else:
log.info(f"✔ [IMPORT] Add all users to chat @{target_chat}")
log.info(f"☻ [IMPORT] Users: {usernames_import}")
client(InviteToChannelRequest(target_chat, usernames_import))
log.info("⚑ Finish script")