Skip to content

Commit

Permalink
声音适配
Browse files Browse the repository at this point in the history
  • Loading branch information
mikumifa committed Sep 30, 2024
1 parent 0328793 commit 44de1a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ retry~=0.9.2
tinydb~=4.8.0
bili-ticket-gt-python~=0.2.7
ntplib~=0.4.0
playsound~=1.3.0
gradio-calendar~=0.0.4
PyObjC~=10.3.1;sys_platform=="darwin"
playwright~=1.46.0
Expand Down
50 changes: 25 additions & 25 deletions tab/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import retry
from gradio import SelectData
from loguru import logger
from playsound import playsound
from requests import HTTPError, RequestException

from config import global_cookieManager, main_request, configDB, time_service
Expand Down Expand Up @@ -157,7 +156,7 @@ def file_select_handler(select_data: SelectData, files):
with gr.Accordion(label='配置抢票成功声音提醒[可选]', open=False):
with gr.Row():
audio_path_ui = gr.Audio(
label="上传提示声音", type="filepath")
label="上传提示声音[只支持格式wav]", type="filepath", loop=True)

def input_phone(_phone):
global_cookieManager.set_config_value("phone", _phone)
Expand Down Expand Up @@ -219,6 +218,7 @@ def start_go(tickets_info_str, authcode_prepare_str, authcode_preorder_time, tim
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
while isRunning:
try:
Expand Down Expand Up @@ -254,6 +254,7 @@ def start_go(tickets_info_str, authcode_prepare_str, authcode_preorder_time, tim
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
attempt_times = 2 # 为避免错过开票时间, 最多尝试两次预填
while attempt_times > 0:
Expand Down Expand Up @@ -307,6 +308,7 @@ def start_go(tickets_info_str, authcode_prepare_str, authcode_preorder_time, tim
gr.update(visible=True),
gr.update(value=gt),
gr.update(value=challenge),
gr.update(),
gr.update(value=uuid.uuid1()),
]

Expand Down Expand Up @@ -336,6 +338,7 @@ def run_validation():
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
validate_con.wait()
validate_con.release()
Expand Down Expand Up @@ -378,6 +381,7 @@ def run_validation():
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
attempt_times += 1
time.sleep(1) # 休息1秒, 避免触发风控
Expand All @@ -398,6 +402,7 @@ def run_validation():
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
time.sleep(1)
else:
Expand All @@ -419,14 +424,12 @@ def run_validation():
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
logger.info("手动停止定时抢票")
return
else:
break
if not isRunning:
gr.update(value="停止", visible=True),
return

# 数据准备
tickets_info = json.loads(tickets_info_str)
Expand Down Expand Up @@ -475,6 +478,7 @@ def run_validation():
gr.update(value=gt),
gr.update(value=challenge),
gr.update(value=uuid.uuid1()),
gr.update(),
]

def run_validation():
Expand All @@ -501,6 +505,7 @@ def run_validation():
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
validate_con.wait()
validate_con.release()
Expand Down Expand Up @@ -531,13 +536,6 @@ def run_validation():
geetest_seccode = ""
if _data["code"] == 0:
logger.info("验证码成功")
gr.update(value=withTimeString("验证码成功"), visible=True),
gr.update(visible=True),
gr.update(),
gr.update(),
gr.update(),
gr.update(),
gr.update(),
else:
logger.info("验证码失败 {}", _data)
yield [
Expand All @@ -548,6 +546,8 @@ def run_validation():
gr.update(),
gr.update(),
gr.update(),
gr.update(),

]
continue
request_result = _request.post(
Expand Down Expand Up @@ -605,6 +605,7 @@ def inner_request():
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
if errno == 0:
logger.info(f"3)抢票成功")
Expand All @@ -624,6 +625,7 @@ def inner_request():
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
pushplusToken = configDB.get("pushplusToken")
if pushplusToken is not None and pushplusToken != "":
Expand All @@ -633,26 +635,17 @@ def inner_request():
if serverchanKey is not None and serverchanKey != "":
ServerChanUtil.send_message(serverchanKey, "抢票成功", "前往订单中心付款吧")

if audio_path is not None and audio_path != "":
def play_sound_in_loop(file_path):
while True:
try:
playsound(file_path)
except Exception as e:
logger.info(f"播放音乐失败: {e}")
time.sleep(1)

if audio_path != "":
yield [
gr.update(value="开始放歌, 暂未实现关闭音乐功能,想关闭音乐请重启程序", visible=True),
gr.update(value="开始放歌", visible=True),
gr.update(visible=False),
gr.update(),
gr.update(),
gr.update(),
gr.update(),
gr.update(),
gr.update(value=audio_path, type="filepath", autoplay=True),
]
play_sound_in_loop(os.path.normpath(audio_path))

break
if mode == 1:
left_time -= 1
Expand All @@ -668,6 +661,7 @@ def play_sound_in_loop(file_path):
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
except ValueError as e:
logger.info(f"{e}")
Expand All @@ -679,6 +673,8 @@ def play_sound_in_loop(file_path):
gr.update(),
gr.update(),
gr.update(),
gr.update(),

]
except HTTPError as e:
logger.error(f"请求错误: {e}")
Expand All @@ -690,6 +686,8 @@ def play_sound_in_loop(file_path):
gr.update(),
gr.update(),
gr.update(),
gr.update(),

]
except Exception as e:
logger.exception(e)
Expand All @@ -701,6 +699,7 @@ def play_sound_in_loop(file_path):
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]
finally:
time.sleep(interval / 1000.0)
Expand All @@ -713,6 +712,7 @@ def play_sound_in_loop(file_path):
gr.update(),
gr.update(),
gr.update(),
gr.update(),
]

mode_ui.change(
Expand Down Expand Up @@ -809,7 +809,7 @@ def stop():
fn=start_go,
inputs=[ticket_ui, authcode_prepare_text_ui, authcode_preorder_time_ui, time_tmp, interval_ui, mode_ui,
total_attempts_ui, api_key_input_ui, audio_path_ui],
outputs=[go_ui, stop_btn, qr_image, gt_row, gt_ui, challenge_ui, trigger],
outputs=[go_ui, stop_btn, qr_image, gt_row, gt_ui, challenge_ui, trigger, audio_path_ui],
)
stop_btn.click(
fn=stop,
Expand Down

0 comments on commit 44de1a9

Please sign in to comment.