def parse_m3u8file(self): self.m3u8file.parse_file() self.tssegments = self.m3u8file.get_tssegments() self.__all_tsseg_len = len(self.tssegments) if len(self.tssegments) == 0: myprint.myprint(self.m3u8file.m3u8_obj.data) raise M3u8DownloaderNoStreamException()
def merge(self): # reorder self.tsfiles.sort(key=lambda x: x.index) with open(self.output_file, 'wb') as merged: for tsfile in self.tsfiles: myprint.myprint(tsfile.output_file) with open(tsfile.output_file, 'rb') as mergefile: shutil.copyfileobj(mergefile, merged)
def download_file(fileurl, headers, filename, check=None, verify=True): with requests.get(fileurl, headers=headers, stream=True, verify=verify) as r: # noqa if check and not check(r): myprint.myprint('Not a valid ts file') myprint.myprint(r.content) raise DownloadFileNotValidException() with open(filename, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): if chunk: f.write(chunk)
def _download_ts(self, tsseg, index, dd_ts, trycnt): uri = tsseg['uri'] if trycnt > self.max_try: raise M3u8DownloaderMaxTryException try: outfile = M3u8File.get_path_by_url(uri, self.ts_tmpfolder) url = urljoin(self.base_url, uri) tsfile = TsFile(url, self.headers, outfile, index, self.sslverify) if not uri in dd_ts: tsfile.get_file() dd_ts.append(uri) self.tsfiles.append(tsfile) self.on_progress(len(self.tsfiles), self.__all_tsseg_len) except DownloadFileNotValidException: trycnt = trycnt + 1 self._download_ts(tsseg, index, dd_ts, trycnt) except Exception as e: myprint.myprint(e) myprint.myprint('Exception occurred, ignore ...') trycnt = trycnt + 1 self._download_ts(tsseg, index, dd_ts, trycnt)
def wrapper(*args, **kwargs): myprint.myprint('Executing: {} ...'.format(proc_name)) func(*args, **kwargs) myprint.myprint('Finished: {} '.format(proc_name))