async def _get(self, session, url, headers): async with session.get(url, headers=headers) as res: code = res.status ret = {'err': False, 'status': code} if code == 475: ret['err'] = True elif code == 302: loc = res.headers['Location'] ret['new_url'] = loc log.info('Redirecting...') r = await self._get(session, url=loc, headers=headers) if r['err']: ret['err'] = True elif code == 200: log.info(f'Start to record from url [{url}]') self.break_warning = 0 ff = open(self.file_name, 'wb') while chunk := await res.content.read(2048): ff.write(chunk) self.rec_on = True ff.close() self.rec_on = False else:
def assmble_users(self, m=False): for c in self.live_list: _c = self._user_info(c) u = User(_c, m) if u.is_akarin is False: self.users.append(u) else: log.info(f"Akarin user: {c['url']}, omitted...")
async def record(self): h = header(self.url) async with aiohttp.ClientSession() as s: ret = await self._get(session=s, url=self.url, headers=h) if ret['err']: log.info(f'record error, status {ret["status"]}') else: log.info("Record ended") return
async def watch_lives_staus(self): while True: livings = 0 for user in self.lives.users: if user.live_status == 1: livings += 1 continue await user.get_live_status() log.info(f"{livings} lives is online") await asyncio.sleep(10)
async def watch_config(self): while True: if self.lives.reconfigured: self.lives.parse_user_config() self.lives.assmble_users() else: log.info('Configuration remains the same, will be checked again after 60 seconds') # set this at the bottom to ensure the first loop will parse user info self.lives.config_changed() await asyncio.sleep(60)
async def record(self): while True: for user in self.lives.users: if user.live_status == 1 and user.is_recording is False: log.info(f"Detect live [{user.room_title}] on...") user.is_recording = True # this cannot be awaited... asyncio.create_task(self._rec(user, user.room_title, self.lives.sdir)) # there must be a sleep to block this while loop await asyncio.sleep(10)
def parse_save_dir(self, v): if v is not None: s = v else: t = toml.load(self.fconfig) if 'save_dir' in t: s = t['save_dir'] else: s = Fpath.save_dir if not s.exists(): log.info(f"Creating saving directory at {s}") s.mkdir(parents=True) return Path(s)
def parse_user_config(self): log.info(f'Start to read configurations from {self.fconfig}') cf = toml.load(self.fconfig) for u in cf['users']: log.info(f"Loading user: {u['url']}") self.live_list.append(u) log.info(f"Loaded {len(self.live_list)} users in total")
def __init__(self, user_info, manual=False): self.qn = None self.live_time = None self.live_frequency = None self.user_name = None self.uid = None self.mid = None self.room_title = None self.live_status = 0 self.stream_url = None # is case of user is 404, will be updated later # self.is_akarin = True self.is_recording = False self.api = Api() if not manual: log.info("=========") self._update_user(user_info)
def get_stream_url(self): log.info(f"Fetching live stream url for [{self.room_title}]") res = sreq(self.api.stream_api) if not res['err']: self.stream_url = URL( res['res']['data']['play_url']['durl'][0]['url']) log.info(f"Stream url is [{self.stream_url}]") else: log.info(f"fetch live stream failed, status [{res['code']}]")
def get_user_name(self): log.info( f"Fetching user name with uid [{self.uid}], url is ]{self.api.user_info}]" ) res = sreq(self.api.user_info) if not res['err']: self.is_akarin = False user = res['res'] self.user_name = user['data']['name'] log.info(f'get user name: [{self.user_name}]') else: self.is_akarin = True log.info( f"get user name failed, url is [{self.api.user_info}], status [{res['code']}]" )
async def get_live_status(self): log.info( f"Updating live status for [{self.user_name}], url is [{self.api.live_status}]" ) res = await req(self.api.live_status) if not res['err']: # staus == 1 -> live on; staus == 0 -> live off self.live_status = res['res']['data']['live_status'] log.info( f"room [{self.room_title}] in status [{self.live_status}]") else: log.info( f'get live status failed, room id is [{self.mid}] , status [{res["code"]}]' )
def get_room_from_mid(self): log.info( f"Updating room info from mid [{self.mid}], url is [{self.api.room_info}]" ) res = sreq(self.api.room_info) if not res['err']: room = res['res'] self.uid = room['data']['uid'] self.room_title = room['data']['title'] self.live_status = room['data']['live_status'] log.info( f'get room title: [{self.room_title}], live status [{self.live_status}]' ) self.api.set_uid(self.uid) self.get_user_name() else: log.info( f"get room info failed, url is [{self.api.room_info}], status [{res['code']}]" )
def get_room_from_uid(self): log.info( f"Updating room info with uid [{self.uid}], url is [{self.api.room_from_uid}]" ) res = sreq(self.api.room_from_uid) if not res['err']: room = res['res'] room_url = URL(room['data']['url']) self.mid = room_url.parts[-1] self.room_title = room['data']['title'] self.live_status = room['data']['liveStatus'] self.api.set_mid(self.mid) log.info( f'get room title: [{self.room_title}], live status [{self.live_status}]' ) else: log.info( f"get room info failed, url is: [{self.api.room_from_uid}], [status {res['code']}]" )