forked from AsaChiri/DDRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainRunner.py
139 lines (124 loc) · 5.91 KB
/
MainRunner.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
import datetime
import logging
import threading
import time
import traceback
from multiprocessing import Process, Value
from Telegram import TelegramBotApi
import json
import utils
from BiliLive import BiliLive
from BiliLiveRecorder import BiliLiveRecorder
from BiliVideoChecker import BiliVideoChecker
from DanmuRecorder import BiliDanmuRecorder
from Processor import Processor
from Uploader import Uploader
class MainRunner():
def __init__(self, config):
self.config = config
self.prev_live_status = False
self.current_state = Value(
'i', int(utils.state.WAITING_FOR_LIVE_START))
self.state_change_time = Value('f', time.time())
if self.config.get('root', {}).get('enable_baiduyun', False):
from bypy import ByPy
_ = ByPy()
self.bl = BiliLive(self.config)
self.blr = None
self.bdr = None
def proc(self, config: dict, record_dir: str, danmu_path: str, current_state, state_change_time) -> None:
p = Processor(config, record_dir, danmu_path)
success = p.run()
t = TelegramBotApi(config)
if success:
res = t.common_request('POST', 'sendMessage', data={
'chat_id': -1001566570431,
'text': '已完成录制:' + json.dumps(self.bl.room_info)
}).json()
print(res)
else:
res = t.common_request('POST', 'sendMessage', data={
'chat_id': -1001566570431,
'text': '已完成录制(转码出错):' + json.dumps(self.bl.room_info)
}).json()
print(res)
if config.get('spec', {}).get('uploader', {}).get('record', {}).get('upload_record', False) or config.get('spec', {}).get('uploader', {}).get('clips', {}).get('upload_clips', False):
current_state.value = int(utils.state.UPLOADING_TO_BILIBILI)
state_change_time.value = time.time()
u = Uploader(p.outputs_dir, p.splits_dir, config)
if u.uploader.access_token is None:
current_state.value = int(utils.state.ERROR)
state_change_time.value = time.time()
return
d = u.upload(p.global_start)
if not config.get('spec', {}).get('uploader', {}).get('record', {}).get('keep_record_after_upload', True) and d.get("record", None) is not None and not config.get('root', {}).get('uploader', {}).get('upload_by_edit', False):
rc = BiliVideoChecker(d['record']['bvid'],
p.splits_dir, config)
rc.start()
if not config.get('spec', {}).get('uploader', {}).get('clips', {}).get('keep_clips_after_upload', True) and d.get("clips", None) is not None and not config.get('root', {}).get('uploader', {}).get('upload_by_edit', False):
cc = BiliVideoChecker(d['clips']['bvid'],
p.outputs_dir, config)
cc.start()
if config.get('root', {}).get('enable_baiduyun', False) and config.get('spec', {}).get('backup', False):
current_state.value = int(utils.state.UPLOADING_TO_BAIDUYUN)
state_change_time.value = time.time()
try:
from bypy import ByPy
bp = ByPy()
bp.upload(p.merged_file_path)
except Exception as e:
logging.error('Error when uploading to Baiduyun:' +
str(e)+traceback.format_exc())
current_state.value = int(utils.state.ERROR)
state_change_time.value = time.time()
return
if current_state.value != int(utils.state.LIVE_STARTED):
current_state.value = int(utils.state.WAITING_FOR_LIVE_START)
state_change_time.value = time.time()
def run(self):
try:
while True:
if not self.prev_live_status and self.bl.live_status:
start = datetime.datetime.now()
self.blr = BiliLiveRecorder(self.bl, start)
self.bdr = BiliDanmuRecorder(self.bl, start)
record_process = Process(
target=self.blr.run)
danmu_process = Process(
target=self.bdr.run)
danmu_process.start()
record_process.start()
self.current_state.value = int(utils.state.LIVE_STARTED)
self.state_change_time.value = time.time()
self.prev_live_status = True
record_process.join()
danmu_process.join()
self.current_state.value = int(
utils.state.PROCESSING_RECORDS)
self.state_change_time.value = time.time()
self.prev_live_status = False
proc_process = Process(target=self.proc, args=(
self.config, self.blr.record_dir, self.bdr.danmu_dir, self.current_state, self.state_change_time))
proc_process.start()
try:
self.bl.check_live_status()
except Exception as e:
logging.error(
"Status Error"+str(e)+traceback.format_exc())
else:
time.sleep(self.config.get(
'root', {}).get('check_interval', 60))
except KeyboardInterrupt:
return
except Exception as e:
logging.error('Error in Mainrunner:' +
str(e)+traceback.format_exc())
self.current_state.value = int(utils.state.ERROR)
self.state_change_time.value = time.time()
return
class MainThreadRunner(threading.Thread):
def __init__(self, config):
threading.Thread.__init__(self)
self.mr = MainRunner(config)
def run(self):
self.mr.run()