-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
225 lines (207 loc) · 7.84 KB
/
main.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import requests
import json
import time
import random
import sys
import getopt
# 一些默认的参数
sckey = "" # 用于 servre酱 消息推送,若不需要,无需修改保持现状
prefix = "御坂"
suffix = "号"
zfill_n = 0
chk_range = "1,20001" # 闭区间
filename_out = "lists.txt"
msg_title = "Misaka-ID" # 自定义推送消息的标题
msg_context = "" # 自定义推送消息的正文
# 以下一般无需修改
url = "https://passport.bilibili.com/web/generic/check/nickname"
hea = {
"Accept": "application/json, text/plain, */*",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/80.0.3987.132 Safari/537.36",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9"
}
help_info = '''
-r [--range] 编号检测范围(闭区间,英文逗号分隔)
例如: --range=1,200 表示从 1 检测到 200
-p [--prefix] 名称前缀 默认为"御坂"
-s [--suffix] 名称后缀 默认为"号"
-z [--zfill] 将编号补齐的位数
例如: --zfill=5 会将 1 补齐为 00001
-k [--key] 用于 "server酱" 推送的sckey (push token)
-f [--filename] 用于设置保存结果的文件名 默认为 lists.txt
--msg_title 自定义推送消息的标题 默认为 "Misaka-ID"
--msg_context 自定义推送消息的正文
'''
def options():
"""用于处理传入参数"""
print("")
global chk_range, prefix, suffix, zfill_n, sckey, filename_out, msg_title, msg_context
opts, args = getopt.getopt(sys.argv[1:], '-h-r:-p:-s:-z:-k:-f:',
['help', 'range=', 'prefix=', 'suffix=', 'zfill=', 'key=', 'filename=', 'msg_title=',
'msg_context='])
if len(opts) < 1: # 若未接收到已经预设的命令行参数,则直接采用默认参数
print("[*] 未检测到传入参数,采用默认格式,如 御坂2233号\n")
return 0
for opt_name, opt_value in opts:
if opt_name in ('-h', '--help'):
print("[+] Help info :\n" + help_info)
exit()
if opt_name in ('-r', '--range'):
print("[+] 范围: ", opt_value)
chk_range = str(opt_value)
continue
if opt_name in ('-p', '--prefix'):
print("[+] 前缀: ", opt_value)
prefix = str(opt_value)
continue
if opt_name in ('-s', '--suffix'):
print("[+] 后缀: ", opt_value)
suffix = str(opt_value)
continue
if opt_name in ('-z', '--zfill'):
print("[+] 补齐位数: ", opt_value)
zfill_n = int(opt_value)
continue
if opt_name in ('-k', '--key'):
sckey = str(opt_value)
print("[+] Sckey: ", sckey[:12] + "*" * (len(sckey) - 6 - 12) + sckey[(len(sckey) - 6):])
continue
if opt_name in ('-f', '--filename'):
filename_out = str(opt_value)
print("[+] 输出文件名: ", filename_out)
continue
if opt_name in '--msg_title':
msg_title = str(opt_value)
print("[+] 消息标题: ", msg_title)
continue
if opt_name in '--msg_context':
msg_context = str(opt_value)
print("[+] 消息正文: ", msg_context)
continue
print("")
def loop(_start_n, _end_n):
global i
for i in range(_start_n, _end_n):
name_i = prefix + str(i) + suffix
check(name_i)
sleep()
def loop_zfill(_start_n, _end_n):
global i
for i in range(_start_n, _end_n):
name_i = prefix + str(i).zfill(zfill_n) + suffix
check(name_i)
sleep()
def check(_nickname):
par = {"nickName": "%s" % _nickname} # request.get 参数
retry_n = 0
error_status = 0
while retry_n <= 3:
if retry_n != 0:
print("\nRetry:%d times:%d :" % (i, retry_n))
try:
res = requests.get(url=url, params=par, headers=hea, timeout=10)
except (requests.exceptions.ConnectTimeout, requests.exceptions.ReadTimeout):
print(_nickname, "timeout")
error_status = 1
retry_n += 1
time.sleep(2)
except Exception as err_info:
print("%s Error!\n" % _nickname, err_info)
error_status = 2
retry_n += 1
time.sleep(5)
else:
error_status = 0
result = json.loads(res.text)
if result["code"] == 0:
print(_nickname, "unregistered")
lst_unregistered.append(_nickname)
break
elif result["code"] == 2001 or result["code"] == 40014:
print(_nickname, "registered")
lst_registered.append(_nickname)
break
else:
print(_nickname, "unknown")
error_status = 3
retry_n += 1
time.sleep(3)
# 以下--循环(异常重试)结束后 对错误状态进行判断 并写入列表
if error_status == 1:
timeout.append(_nickname)
elif error_status == 2:
errs.append(_nickname)
elif error_status == 3:
lst_unknown.append(_nickname)
if (i % 20 == 0) or (i + 1 == i_end):
# 每检测20个写一次文件
write_result()
def write_result():
f.seek(0)
f.write("Unregistered: " + str(lst_unregistered) + "\nRegistered: " + str(lst_registered) + "\nTimeout: " + str(
timeout) + "\nError: " + str(errs) + "\nUnknown: " + str(
lst_unknown) + "\n\nUnregistered_count = %d\nRegistered_count = %d\n" % (
len(lst_unregistered), len(lst_registered)))
f.flush()
def sleep():
if i == 0:
return 0
if i % 100 == 0:
sleep_time_1 = random.randint(5, 15)
print("\n达到整百,随机暂停 %d s\n" % sleep_time_1)
time.sleep(sleep_time_1)
elif i % 10 == 0:
time.sleep(random.uniform(0.2, 0.6)) # 取0.2-0.7之间的随机float
else:
time.sleep(0.02)
def send_wxmsg(_sckey, _title="misaka", _context="正文"):
url_postmsg = "https://sc.ftqq.com/%s.send" % _sckey
_context = _context + "\n\n" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
data = {
"text": "%s" % _title,
"desp": "%s" % _context
}
try:
res = requests.post(url=url_postmsg, data=data)
msg_back = json.loads(res.text)
if msg_back["errmsg"] == "success":
print("消息推送成功!")
else:
print("发送可能失败 返回值:%s" % (msg_back["errmsg"]))
except Exception:
print("消息发送错误")
if __name__ == "__main__":
# Some vars
start_time = time.time()
options()
i = 1 # 初始化检测编号
lst_unregistered = []
lst_registered = []
lst_unknown = []
timeout = []
errs = []
chk_range = chk_range.split(",")
i_start = int(chk_range[0])
i_end = int(chk_range[1]) + 1
# run
f = open("./" + filename_out, "w+", encoding="utf-8")
try:
if zfill_n == 0: # 判断是否补齐0位,并进入for循环
loop(i_start, i_end)
else:
loop_zfill(i_start, i_end)
except KeyboardInterrupt:
print("\nRaised Control-C. Cancled!\n")
except Exception as err_info_1:
print("\nError!\n", err_info_1)
else:
total_time = time.time() - start_time
print("\n\nFinished\nTotal time: %f s\n" % total_time)
if not sckey == "":
print("Server酱推送中...")
send_wxmsg(_sckey=sckey, _title=msg_title,
_context="%s\n\nFinished.\n\nTotal time: %f s" % (msg_context, total_time))
f.write("\n\nType: " + prefix + r"%d" + suffix + " --zfill=%d\n" % zfill_n)
f.close()