-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
190 lines (179 loc) · 8.52 KB
/
test_cli.py
File metadata and controls
190 lines (179 loc) · 8.52 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import datetime
import random
import time
import upload_vid_tele
import telethon
from telethon import TelegramClient, events, functions, errors
import asyncio
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.tl.functions.messages import ImportChatInviteRequest
import test
download_path = "15mb_vid/"
upload_path = "edited_video/"
api_id = 24259180
api_hash = "63cf9778009838a7d48f75866a5b6fc9"
# Ql & Public
download_chat_id = -1002126759941
upload_chat_id = -1002006536487
client = TelegramClient('khanh_chi', api_id, api_hash)
newest_msg_id_source = 0
# -------------------------------------------------------------
async def JoinGroup():
dict_link = test.GetNameGroup()
# dict_link = {
# 'public_link': ['laugaiviet'],
# 'private_link': []
# }
# try:
# chat = await client.get_entity('QOEuIFQqvhE0ZjAx')
# await client(JoinChannelRequest(chat))
# ca
# print(err.seconds)
count = 0
count_success = 0
count_failure = 0
count_gr = 0
for link in dict_link:
# join private group
if link == 'private_link':
for idx in dict_link.get(link):
print(f"private: {idx} - {datetime.datetime.now()}")
# check if gr has joined -> skip
if test.CheckGrJoined(idx) == 0:
try:
await client(ImportChatInviteRequest(idx))
# sleep 61s to avoid ban
time_sleep = 61
print(f'sleep time: {time_sleep}')
time.sleep(time_sleep)
gr = await client.get_entity(idx)
# sleep 61s to avoid ban
time_sleep = 61
print(f'sleep time: {time_sleep}')
time.sleep(time_sleep)
count_success += 1
print(f'success join private: {idx}, count_success : {count_success}')
await upload_vid_tele.UpLoadVid(client, gr)
# banned for fast request
except telethon.errors.rpcerrorlist.FloodWaitError as err:
count_failure += 1
print(f'failed join private: {idx}, count_failure : {count_failure}')
test.WriteLog(idx, 'got banned')
print("write log ok")
print(f"got banned -> sleep in: {err.seconds}")
time.sleep(err.seconds)
# user name invalid
except telethon.errors.rpcerrorlist.UsernameInvalidError as err:
count_failure += 1
print(f"Invalid name: {err.message}")
print(f'failed join private: {idx}, count_failure : {count_failure}')
test.WriteLog(idx, 'invalid name')
print("write log ok")
# need admin to approve
except telethon.errors.rpcerrorlist.InviteRequestSentError as err:
count_failure += 1
print(f"requested to admin: {err.message}")
print(f'failed join private: {idx}, count_failure : {count_failure}')
test.WriteLog(idx, 'requested to admin')
print("write log ok")
# other exception
except Exception as err:
count_failure += 1
print(f"other exception: {err}")
print(f'failed join private: {idx}, count_failure : {count_failure}')
test.WriteLog(idx, f'{err}')
print("write log ok")
else:
print(f'this gr has joined: {idx}')
try:
gr = await client.get_entity(idx)
# sleep 61s to avoid ban
time_sleep = 61
print(f'sleep time: {time_sleep}')
time.sleep(time_sleep)
await upload_vid_tele.UpLoadVid(client, gr)
count += 1
print(f"count: {count} - {datetime.datetime.now()}")
continue
except Exception as err:
print(f'got an err: {err}')
count += 1
print(f"count: {count} - {datetime.datetime.now()}")
# sleep randomly 61s to avoid ban
time_sleep = 61
print(f'sleep time: {time_sleep}')
time.sleep(time_sleep)
# join public group
else:
for idx in dict_link.get(link):
print(f"public: {idx} - {datetime.datetime.now()}")
# check if gr has joined -> skip
if test.CheckGrJoined(idx) == 0:
try:
gr = await client.get_entity(idx)
# sleep randomly 61s to avoid ban
time_sleep = 61
print(f'sleep time: {time_sleep}')
time.sleep(time_sleep)
await client(JoinChannelRequest(gr))
count_success += 1
print(f'success join public: {idx}, count_success : {count_success}')
test.WriteJoinedGr(idx)
# sleep randomly 61s to avoid ban
time_sleep = 61
print(f'sleep time: {time_sleep}')
time.sleep(time_sleep)
await upload_vid_tele.UpLoadVid(client, gr)
# banned for fast request
except telethon.errors.rpcerrorlist.FloodWaitError as err:
count_failure += 1
print(f'failed join public: {idx}, count_failure : {count_failure}')
test.WriteLog(idx, 'got banned')
print("write log ok")
print(f"got banned -> sleep in: {err.seconds}")
time.sleep(err.seconds)
# usr_name invalid
except telethon.errors.rpcerrorlist.UsernameInvalidError as err:
count_failure += 1
print(f"Invalid name: {err.message}")
print(f'failed join public: {idx}, count_failure : {count_failure}')
test.WriteLog(idx, 'invalid name')
print("write log ok")
# need admin approve
except telethon.errors.rpcerrorlist.InviteRequestSentError as err:
count_failure += 1
print(f"requested to admin: {err.message}")
print(f'failed join public: {idx}, count_failure : {count_failure}')
test.WriteLog(idx, 'requested to admin')
print("write log ok")
# other exception
except Exception as err:
count_failure += 1
print(f"other exception: {err}")
print(f'failed join public: {idx}, count_failure : {count_failure}')
test.WriteLog(idx, f'{err}')
print("write log ok")
else:
print(f'this gr has joined: {idx}')
try:
gr = await client.get_entity(idx)
# sleep randomly 61s to avoid ban
time_sleep = 61
print(f'sleep time: {time_sleep}')
time.sleep(time_sleep)
await upload_vid_tele.UpLoadVid(client, gr)
count += 1
print(f"count: {count} - {datetime.datetime.now()}")
continue
except Exception as err:
print(f'got exception: {err}')
count += 1
print(f"count: {count} - {datetime.datetime.now()}")
# sleep randomly 61s to avoid ban
time_sleep = 61
print(f'sleep time: {time_sleep}')
time.sleep(time_sleep)
# --------------------------------------------------------------------------------
with client:
client.loop.run_until_complete(JoinGroup())
# -----------------------------------------------------------------------------------