-
Notifications
You must be signed in to change notification settings - Fork 0
/
biliibli.py
83 lines (76 loc) · 2.87 KB
/
biliibli.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
import requests
import sys
import time
import subprocess
import random
import logging
from pathlib import Path
import config
from utils import bot_send
class Bilibili:
def __init__(self, id) -> None:
self.id = id
self.title = ''
self.file_name = ''
self.logger = logging.getLogger(f'bilibili/{self.id}')
def check_stream(self) -> bool:
# 开播检测
room_init_data = requests.get(f"https://api.live.bilibili.com/room/v1/Room/room_init?id={self.id}").json()
if room_init_data['code'] == 0:
vid = room_init_data['data']['room_id']
else:
self.logger.info('Get room ID from API failed: %s', room_init_data)
vid = self.id
room_info_data = requests.get(f"https://api.live.bilibili.com/room/v1/Room/get_info?room_id={vid}").json()
if room_info_data['code'] != 0:
self.logger.debug(room_init_data)
return False
room_info_data = room_info_data['data']
self.title = room_info_data['title']
self.logger.info(f'room {self.id} status {room_info_data["live_status"]}')
if room_info_data['live_status'] != 1:
return False
return True
def download(self, filename) -> int:
# 调用streamlink录制直播
if len(config.STREAM_LINK_PATH) == 0:
path = 'streamlink'
else:
path = config.STREAM_LINK_PATH
proc_args = [ path,
'--loglevel', 'trace',
'--config', Path(__file__).absolute().parent / "streamlink_cookies.conf",
f'https://live.bilibili.com/{self.id}',
'best',
'-o', Path(__file__).absolute().parent / config.RECORD_DIR / filename
]
self.logger.info(f'start recording {self.id}')
proc = subprocess.Popen(proc_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
try:
with proc.stdout as stdout:
for line in iter(stdout.readline, b''): # b'\n'-separated lines
#print(line.decode(), end='', file=sys.stderr)
self.logger.debug(f'streamlink:{line.decode().rstrip()}')
retval = proc.wait()
except KeyboardInterrupt:
if sys.platform != 'win32':
proc.communicate(b'q')
raise
return retval
def run(self, group_id):
try:
while True:
if not self.check_stream():
time.sleep(config.LOOP_INTERVAL)
else:
break
start_time = time.strftime("%Y%m%d_%H%M%S", time.localtime())
self.file_name = f'bilibili_{self.id}_{start_time}_{self.title}.flv'
message = f'开始录制直播间{self.id}\n标题:{self.title}\n开始时间:{start_time}'
bot_send(group_id, message)
retval = self.download(self.file_name)
self.logger.info(f'streamlink ret code:{retval} for {self.file_name}')
return (retval, self.file_name)
except:
self.logger.exception("Unexpected expection at Bilibili.run")
self.logger.exception(sys.exc_info)