def get_host_video_data(self): """ 取得 Host資料。 """ while True: try: res = download_request(url=self.data['url'], timeout=5) if res: data = res.json() res.close() return data except BaseException as error: time.sleep(5)
def video(self, i, res, host, m3u8_count): """ 請求 URL 下載影片。 """ host_value = 0 url = f"{host[host_value]['host']}{res['video']['720p'].split('.')[0]}_{i:03d}.ts" ok = False while True: try: if not self.stop and not self.exit: data = download_request(url=url, stream=True, timeout=3) if data: while True: if self.data['video_ts'] == i: with open( f'{self.path["path"]}/{self.folder_name}/{self.file_name}.mp4', 'ab') as v: self.write_undone(index=i, m3u8_count=m3u8_count) shutil.copyfileobj(data.raw, v) self.data['video_ts'] += 1 self.write_undone(index=self.data['video_ts'], m3u8_count=m3u8_count) if self.remove_file: self.del_file() ok = True data.close() del data break elif self.stop or self.exit: data.close() del data break time.sleep(1) if ok: break if self.exit: break time.sleep(3) except (requests_RequestException, requests_ConnectionError, requests_ChunkedEncodingError, ConnectionResetError) as e: if host_value - 1 > len(host): host_value = 0 else: host_value += 1 url = f"{host[host_value]['host']}{res['video']['720p'].split('.')[0]}_{i:03d}.ts" print(e, url) time.sleep(1) except BaseException as error: print('基礎錯誤', error)
def get_m3u8_data(self, res): """ 取得 m3u8 資料。 """ index = 0 url = res['host'][index]['host'] + res['video']['720p'] while True: try: m3u8_data = download_request(url=url, timeout=5) if m3u8_data: data = m3u8_data.text m3u8_data.close() return data except: index += 1 url = res['host'][index]['host'] + res['video']['720p'] time.sleep(5)
def get_host_video_data(self): """ 取得 Host資料。 """ error_value = 1 self.data.update({'status': f'取得資料中'}) while True: self.download_video.emit(self.data) try: res = download_request(url=self.data['url'], timeout=(15, 15)) if res: data = res.json() res.close() self.data.update({'status': '成功取得資料'}) self.download_video.emit(self.data) return data except BaseException as error: pass self.data.update({'status': f'取得資料中(失敗{error_value}次)'}) error_value += 1 time.sleep(5)