-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
65 lines (59 loc) · 2.05 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
# -*- coding: utf-8 -*-
# @Time: 2023/2/5 16:44
# @FileName: main.py
# @Software: PyCharm
# @GitHub: KimmyXYC
import time
import sys
import signal
from loguru import logger
from App.Push import Pusher
from App.Live import BiliLive
from Utils.Parameter import get_value
from Utils.Tool import get_room_id
from Utils.Base import read_yaml_file
def signal_handler(signal, frame):
logger.success("检测到终止信号, 开始停播")
config = read_yaml_file()
if config["user_info"]["room_id"]:
BiliLive(config).stop_live()
sys.exit(0)
def main():
config = read_yaml_file()
pusher = Pusher(push_config=config["push"])
if not config["user_info"]["cookies"]:
logger.error("Cookies 未填写, 无法执行直播任务")
return
if config["user_info"]["room_id"]:
streamer = BiliLive(config, pusher=pusher)
start_time = time.time()
try:
streamer.start_live()
except Exception as e:
logger.error(f"直播发生错误: {e}")
time.sleep(3)
streamer.stop_live()
streamer.share_room()
streamer.get_live_receive()
duration = time.time() - start_time
logger.success(f"直播完成, 共耗时 {int(duration)} 秒")
pusher.push(f"直播完成, 共耗时 {int(duration)} 秒")
else:
logger.warning("room_id 未填写, 正在尝试自动获取……")
mid = get_value("DedeUserID", config["user_info"]["cookies"])
if get_room_id(mid):
logger.success("获取成功")
logger.info("请重启程序以开启自动直播任务")
else:
logger.error("获取失败, 请手动填写 room_id")
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
logger.remove()
handler_id = logger.add(sys.stderr, level="INFO")
logger.add(sink='run.log',
format="{time} - {level} - {message}",
level="INFO",
rotation="20 MB",
enqueue=True)
main()