forked from AsaChiri/DDRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiliLiveRecorder.py
64 lines (57 loc) · 2.8 KB
/
BiliLiveRecorder.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
import datetime
import logging
import os
import re
import traceback
import requests
import urllib3
import utils
from BiliLive import BiliLive
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class BiliLiveRecorder(BiliLive):
def __init__(self, config: dict, global_start: datetime.datetime):
BiliLive.__init__(self, config)
self.record_dir = utils.init_record_dir(
self.room_id, global_start, config.get('root', {}).get('data_path', "./"))
def record(self, record_url: str, output_filename: str) -> None:
try:
logging.info(self.generate_log('√ 正在录制...' + self.room_id))
default_headers = {
'Accept-Encoding': 'identity',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36 ',
'Referer': re.findall(
r'(https://.*\/).*\.flv',
record_url)[0]
}
headers = {**default_headers, **
self.config.get('root', {}).get('request_header', {})}
resp = requests.get(record_url, stream=True,
headers=headers,
timeout=20)
with open(output_filename, "wb") as f:
for chunk in resp.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
except Exception as e:
logging.error(self.generate_log(
'Error while recording:' + str(e)))
def run(self) -> None:
logging.basicConfig(level=utils.get_log_level(self.config),
format='%(asctime)s %(thread)d %(threadName)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
handlers=[logging.FileHandler(os.path.join(self.config.get('root', {}).get('logger', {}).get('log_path', "./log"), "LiveRecoder_"+datetime.datetime.now(
).strftime('%Y-%m-%d_%H-%M-%S')+'.log'), "a", encoding="utf-8")])
while True:
try:
if self.live_status:
urls = self.get_live_urls()
filename = utils.generate_filename(self.room_id)
c_filename = os.path.join(self.record_dir, filename)
self.record(urls[0], c_filename)
logging.info(self.generate_log('录制完成' + c_filename))
else:
logging.info(self.generate_log('下播了'))
break
except Exception as e:
logging.error(self.generate_log(
'Error while checking or recording:' + str(e)+traceback.format_exc()))