import
cv2
from
pyzbar.pyzbar
import
decode
import
pyzbar.pyzbar as pyzbar
import
numpy as np
from
PIL
import
ImageGrab
import
time
import
tkinter as tk
import
threading
import
re
import
http.client
import
json
def
my_function():
import
juxing
my_thread
=
threading.Thread(target
=
my_function)
my_thread.start()
root
=
tk.Tk()
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
)
left, top, width, height
=
x_pos, y_pos, win_width, win_height
right
=
left
+
width
bottom
=
top
+
height
cv2.namedWindow(
"QR Code Scanner"
, cv2.WINDOW_NORMAL)
cv2.resizeWindow(
"QR Code Scanner"
, win_width, win_height)
def
Request(ticket):
conn
=
http.client.HTTPSConnection(
"api-sdk.mihoyo.com"
)
payload
=
json.dumps({
"app_id"
:
4
,
"device"
: "",
"ticket"
: ticket
})
headers
=
{}
conn.request(
"POST"
,
"/hk4e_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
=
{
'DS'
: '',
'cookie'
: '',
'x-rpc-client_type'
:
'2'
,
'x-rpc-app_version'
:
'2.46.1'
,
'x-rpc-sys_version'
:
'9'
,
'x-rpc-channel'
: '',
'x-rpc-device_id'
: '',
'x-rpc-device_fp'
: '',
'x-rpc-device_name'
: '',
'x-rpc-device_model'
: '',
'Referer'
:
' [url=https://app.mihoyo.co]https://app.mihoyo.co[/url]'
}
conn.request(
"GET"
,
"/auth/api/getGameToken?uid="
,
payload, headers)
res
=
conn.getresponse()
data
=
res.read()
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"
:
4
,
"device"
: "",
"payload"
: {
"proto"
:
"Account"
,
"raw"
: f
"{{\"uid\":\"0000000\",\"token\":\"{token}\"}}"
},
"ticket"
: ticket
})
headers
=
{
'DS'
: '',
'cookie'
: '',
'x-rpc-client_type'
:
'2'
,
'x-rpc-app_version'
:
'2.46.1'
,
'x-rpc-sys_version'
:
'9'
,
'x-rpc-channel'
:
' xiaomi'
,
'x-rpc-device_id'
: '',
'x-rpc-device_fp'
: '',
'x-rpc-device_name'
: '',
'x-rpc-device_model'
: '',
'Referer'
:
' [url=https://app.mihoyo.com]https://app.mihoyo.com[/url]'
,
'Content-Type'
:
'application/json'
}
conn.request(
"POST"
,
"/hk4e_cn/combo/panda/qrcode/confirm"
,
payload, headers)
res
=
conn.getresponse()
while
True
:
screenshot
=
cv2.cvtColor(
np.array(ImageGrab.grab(bbox
=
(left, top, right, bottom))),
cv2.COLOR_BGR2RGB
)
gray
=
cv2.cvtColor(screenshot, cv2.COLOR_RGB2GRAY)
codes
=
decode(gray, symbols
=
[pyzbar.ZBarSymbol.QRCODE])
if
codes:
print
(codes[
0
].data.decode())
pattern
=
r
"ticket=([a-f0-9]+)"
match
=
re.search(pattern, codes[
0
].data.decode())
if
match:
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)
ConfirmRequest(match.group(
1
))
time.sleep(
1
)
time.sleep(
0.05
)
cv2.imshow(
"QR Code Scanner"
, screenshot)
if
cv2.waitKey(
1
) !
=
-
1
:
break
cv2.destroyAllWindows()