import
json
import
time
import
cv2
from
pyzbar.pyzbar
import
decode
import
pyzbar.pyzbar as pyzbar
import
numpy as np
import
tkinter as tk
import
threading
import
re
import
http.client
import
win32gui
import
win32ui
import
win32con
import
uuid
GameType
=
4
cookie
=
"stuid=;stoken=;mid=0mmmmato08_mhy;"
uid
=
""
def
get_uuid():
get_timestamp_uuid
=
uuid.uuid1()
return
str
(get_timestamp_uuid)
device
=
get_uuid()
def
my_function():
root
=
tk.Tk()
root.overrideredirect(
True
)
root.wm_attributes(
"-topmost"
,
True
)
win_width
=
300
win_height
=
300
screen_width
=
root.winfo_screenwidth()
screen_height
=
root.winfo_screenheight()
x_pos
=
(screen_width
/
/
2
)
-
(win_width
/
/
2
)
y_pos
=
(screen_height
/
/
2
)
-
(win_height
/
/
2
)
root.geometry(
'{}x{}+{}+{}'
.
format
(win_width, win_height, x_pos, y_pos))
root.attributes(
'-transparentcolor'
,
'white'
)
canvas
=
tk.Canvas(root, bg
=
'white'
, highlightthickness
=
0
)
canvas.pack(fill
=
'both'
, expand
=
True
)
canvas.create_rectangle(
5
,
5
, win_width
-
5
, win_height
-
5
, outline
=
'red'
, width
=
2
)
root.mainloop()
my_thread
=
threading.Thread(target
=
my_function)
my_thread.start()
width, height
=
300
,
300
screen_width
=
tk.Tk().winfo_screenwidth()
screen_height
=
tk.Tk().winfo_screenheight()
left
=
(screen_width
-
width)
/
/
2
top
=
(screen_height
-
height)
/
/
2
right
=
left
+
width
bottom
=
top
+
height
hwnd
=
win32gui.GetDesktopWindow()
hdc
=
win32gui.GetWindowDC(hwnd)
dc
=
win32ui.CreateDCFromHandle(hdc)
def
capture_screen():
saveDC
=
dc.CreateCompatibleDC()
saveBitMap
=
win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(dc, right
-
left, bottom
-
top)
saveDC.SelectObject(saveBitMap)
saveDC.BitBlt((
0
,
0
), (right
-
left, bottom
-
top),
dc, (left, top), win32con.SRCCOPY)
bmpinfo
=
saveBitMap.GetInfo()
bmpstr
=
saveBitMap.GetBitmapBits(
True
)
screenshot
=
np.frombuffer(bmpstr, dtype
=
'uint8'
).reshape(
(bmpinfo[
'bmHeight'
], bmpinfo[
'bmWidth'
],
4
))[:, :, :
3
]
screenshot
=
cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY)
saveDC.DeleteDC()
win32gui.DeleteObject(saveBitMap.GetHandle())
return
screenshot
def
Request(ticket):
conn
=
http.client.HTTPSConnection(
"api-sdk.mihoyo.com"
)
payload
=
json.dumps({
"app_id"
: GameType,
"device"
: device,
"ticket"
: ticket
})
headers
=
{}
if
GameType
=
=
4
:
conn.request(
"POST"
,
"/hk4e_cn/combo/panda/qrcode/scan"
, payload, headers)
elif
GameType
=
=
8
:
conn.request(
"POST"
,
"/hkrpg_cn/combo/panda/qrcode/scan"
, payload, headers)
res
=
conn.getresponse()
data
=
res.read()
data
=
json.loads(data.decode(
"utf-8"
))
retcode
=
data[
"retcode"
]
return
retcode
def
ConfirmRequest(ticket):
conn
=
http.client.HTTPSConnection(
"api-takumi.miyoushe.com"
)
payload
=
''
headers
=
{
'cookie'
: cookie,
}
conn.request(
"GET"
,
"/auth/api/getGameToken"
,
'', headers)
res
=
conn.getresponse()
data
=
res.read()
print
(data.decode(
"utf-8"
))
data
=
json.loads(data.decode(
"utf-8"
))
token
=
data[
"data"
][
"game_token"
]
conn
=
http.client.HTTPSConnection(
"api-sdk.mihoyo.com"
)
payload
=
json.dumps({
"app_id"
: GameType,
"device"
: device,
"payload"
: {
"proto"
:
"Account"
,
"raw"
: f
"{{\"uid\":\"{uid}\",\"token\":\"{token}\"}}"
},
"ticket"
: ticket
})
headers
=
{
'cookie'
: cookie,
}
conn.request(
"POST"
,
"/hk4e_cn/combo/panda/qrcode/confirm"
,
payload, headers)
res
=
conn.getresponse()
frame_count
=
0
frame_start_time
=
time.time()
while
True
:
screenshot
=
capture_screen()
codes
=
decode(screenshot, symbols
=
[pyzbar.ZBarSymbol.QRCODE])
if
codes:
print
(
"扫码成功!"
)
pattern
=
r
"ticket=([a-f0-9]+)"
match
=
re.search(pattern, codes[
0
].data.decode())
if
match:
print
(match.group(
1
))
start_time
=
time.time()
retcode
=
Request(match.group(
1
))
end_time
=
time.time()
if
retcode
=
=
0
:
elapsed_time
=
end_time
-
start_time
print
(
"抢码成功耗时 %.3f 秒"
%
elapsed_time)
random
=
1.3
time.sleep(random)
print
(
"防止过快被察觉插入随机延迟"
)
start_time
=
time.time()
ConfirmRequest(match.group(
1
))
end_time
=
time.time()
elapsed_time
=
end_time
-
start_time
print
(
"确认登陆成功耗时 %.3f 秒"
%
elapsed_time)
time.sleep(
1
)
else
:
print
(
"未知二维码抢码失败"
)
time.sleep(
1
)
frame_count
+
=
1
if
time.time()
-
frame_start_time >
=
1
:
fps
=
frame_count
print
(f
"FPS:{fps}"
+
"\r"
, end
=
'', flush
=
True
)
frame_count
=
0
frame_start_time
=
time.time()
cv2.imshow(
"QR Code Scanner"
, screenshot)
if
cv2.waitKey(
1
) !
=
-
1
:
break
cv2.destroyAllWindows()