forked from south1907/addmember-telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_members.py
191 lines (149 loc) · 5.61 KB
/
add_members.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
import sys
import os
import json
import platform
import signal
import readchar
from datetime import datetime
import time
from telethon.tl.functions.channels import JoinChannelRequest
from telethon import sync, TelegramClient, events
from telethon.tl.types import InputPeerUser
from utils import *
with open('config.json', 'r', encoding='utf-8') as f:
config = json.loads(f.read())
root_path = os.path.dirname(os.path.abspath(__file__))
folder_session = root_path + '/session/'
folder_data = root_path + '/data/'
accounts = config['accounts']
# start_time
start_time = datetime.now()
group_source = config['group_source']
group_target = config['group_target']
api_id = int(config['api_id'])
api_hash = config['api_hash']
total_time_in_round = 120
total_user_big_sleep = 35
time_big_sleep = 7200
skip_user = 2
# list client
clients = []
# data user need add to group
path_folder_file = folder_data + '/' + str(group_source)
users = read_data_member(path_folder_file)
# date_online_from
from_date_active = '19700101'
if 'from_date_active' in config:
from_date_active = config['from_date_active']
i = 0 # alway from 0 because have logic check user_id from target group
count_added = 0 # count added success (in 1 big round)
total_count_added = 0 # total count added success
assert len(accounts) > 0
# init TelegramClient
for phone in accounts:
client = TelegramClient(folder_session + phone, api_id, api_hash)
client.connect()
if client.is_user_authorized():
# join group
client(JoinChannelRequest(group_target))
entity_group = client.get_entity(group_target)
clients.append({
'phone': phone,
'client': client,
'group_target': entity_group
})
else:
logging.info(phone + ' login fail')
total_user = len(users)
total_client = len(clients)
logging.info('total member need to add : ' + str(total_user))
logging.info('total account run: ' + str(total_client))
# use first client to get list user_id of target group
list_user_id_in_target = get_list_user_id_of_group(clients[0]['client'], group_target)
while i < total_user:
# count_added if added 35 user
if count_added >= (total_user_big_sleep * total_client):
logging.info('Big sleep, sleep 2 hours')
for cli in clients:
cli['client'].disconnect()
time.sleep(2)
time.sleep(time_big_sleep)
for cli in clients:
cli['client'].connect()
time.sleep(2)
count_added = 0 # reset
logging.info('current index user: ' + str(i))
user = users[i]
# not add user overdue (not online far away)
if user['date_online'] != 'online' and user['date_online'] < from_date_active:
i += 1
logging.info('User ' + str(user['user_id']) + ' has time active: ' +
user['date_online'] + ' is overdue')
continue
if user['user_id'] in list_user_id_in_target:
i += 1
logging.info('User ' + str(user['user_id']) + ' in target group, not add')
continue
current_index = count_added % total_client
current_client = clients[count_added % total_client]
logging.info('Adding user id: ' + str(user['user_id']))
if current_client['phone'] not in user:
i += 1
logging.info('ignore user id: ' + str(user['user_id']) + ' by not have information for client: ' + current_client['phone'])
continue
user_to_add = InputPeerUser(int(user[current_client['phone']]['user_id']), int(user[current_client['phone']]['access_hash']))
status_add = add_member_to_group(current_client['client'], current_client['group_target'], user_to_add)
logging.info("status_add: " + status_add)
if status_add == 'SUCCESS':
logging.info('Added member ' + str(user['user_id']) + ' successfully ;-)')
logging.info('sleep: ' + str(total_time_in_round / total_client))
time.sleep(total_time_in_round / total_client)
count_added += 1
total_count_added += 1
if status_add == 'FLOOD' or status_add == 'FLOOD_WAIT':
logging.info('FLOOD, remove client: ' + current_client['phone'])
current_client['client'].disconnect()
clients.remove(current_client)
# cal total_client
total_client = len(clients)
if status_add == 'USER_PRIVACY':
logging.info(status_add + ', skip user')
time.sleep(skip_user / total_client)
#this is the code block that saves user_ids that cause errors
#this works but it overwrites the data each time, we need to append it
path_file = 'baduser' + '.json'
with open(path_file, 'a', encoding='utf-8') as f:
bad_user = str(user['user_id'])
json.dump(bad_user, f, indent=4, ensure_ascii=False)
f.write('\n')
#client.disconnect()
if status_add == 'ERROR_OTHER':
logging.info(status_add + ', skip user')
time.sleep(total_time_in_round / total_client)
#this works but it overwrites the data each time, we need to append it
path_file = 'baduser' + '.json'
with open(path_file, 'a', encoding='utf-8') as f:
bad_user = str(user['user_id'])
json.dump(bad_user, f, indent=4, ensure_ascii=False)
f.write('\n')
# if status_add is not FLOOD and FLOOD_WAIT
if status_add != 'FLOOD' and status_add != 'FLOOD_WAIT':
i += 1
# check client empty
if total_client == 0:
logging.info('END: accounts is empty')
break
for cli in clients:
cli['client'].disconnect()
time.sleep(2)
end_time = datetime.now()
logging.info("added: " + str(total_count_added))
logging.info("total time: " + str(end_time - start_time))
#we need to be able to write a list of bad users that we cant add due to privacy
#the code block below is the way that get_members.py opens a file and writes to it
#we will try to hack that to include it in this file
#this writes the info, we also need the string that enumerates the data location
#path_file = path_group + str(phone) + '.json'
#with open(path_file, 'w', encoding='utf-8') as f:
#json.dump(results, f, indent=4, ensure_ascii=False)
#client.disconnect()