def downloadFile(url: str, fileName: str, stimeout=3.05, showProgress: bool = False, append: bool = False): try: response = urlopen(url, timeout=stimeout) totalSize = response.length fileSize, unit = unitFix(totalSize, Unit.BYTE, Unit.MB) if showProgress: progress = ProgressTool(fileSize, 15, unit=unit.name) # mkdir path = getDirName(fileName) mkdirs(path) curcount = 0 chunksize = 16 * 1024 mode = 'wb' if not append else 'ab' with open(fileName, mode) as f: while True: chunk = response.read(chunksize) curcount += len(chunk) if showProgress: progress.setCurCount(convert(curcount, Unit.BYTE, unit)) f.write(chunk) if curcount >= totalSize: break return True, None except Exception as e: return False, e
def download(url, descpath, threadnum=15): '''Download file by m3u8-url''' try: urllist = paresUrl(url) if len(urllist) <= 0: return False threads = ThreadTool(threadnum) # Creat tmpdir path = getDirName(descpath) tmpPath = getDiffTmpPathName(path) if mkdirs(tmpPath) is False: return False # Progress progress = ProgressTool(len(urllist), 20) # Download files files = [] for i, item in enumerate(urllist): filepath = tmpPath + '/' + str(i) + '.ts' files.append(filepath) threads.start(__threadfunc__, item, filepath, progress) threads.waitAll() # merger __merger__(files, descpath) shutil.rmtree(tmpPath) threads.close() return True except: shutil.rmtree(tmpPath) return False
def downloadFile(url, fileName, stimeout=None, showprogress=False, append=False): if sys.version_info > (3, 0): from urllib.request import urlopen else: from urllib2 import urlopen try: if stimeout is None: response = urlopen(url) else: response = urlopen(url, timeout=stimeout) unit = 'mb' if convertStorageUnit(response.length, 'byte', unit) < 1: unit = 'kb' progress = None if showprogress: desc = getFileName(fileName) progress = ProgressTool(convertStorageUnit(response.length, 'byte', unit), 10, unit=unit, desc=desc) mode = 'wb' if append: mode = 'ab' curcount = 0 chunksize = 16 * 1024 with open(fileName, mode) as f: while True: chunk = response.read(chunksize) curcount += len(chunk) if progress: progress.setCurCount( convertStorageUnit(curcount, 'byte', unit)) if not chunk: break f.write(chunk) return True except: return False
def downloadFileByUrls(urlArray, fileName, stimeout=None, showprogress=False): if os.access(fileName, 0): os.remove(fileName) progress = None if showprogress: desc = getFileName(fileName) progress = ProgressTool(len(urlArray), 10, unit='', desc=desc) curcount = 1 for item in urlArray: ret = downloadFile(item, fileName, stimeout, False, append=True) if ret != True: return False if progress: progress.setCurCount(curcount) curcount += 1 return True
def downloadFileByUrls(urls: list, fileName, stimeout=3.05, showProgress=False): if os.access(fileName, 0): os.remove(fileName) progress = None if showProgress: progress = ProgressTool(len(urls), 10) for item in urls: ret, ex = downloadFile(item, fileName, stimeout, False, append=True) if ret is not True or ex is not None: return False if showProgress: progress.step() return True
def playSong(track, path): desc = f"Playing ({track.title} - {track.artist.name})" song = Song(path) chunks = make_chunks(song.seg, 100) progress = ProgressTool(len(chunks), 15) song.callback = lambda count: songCallback(song, count, len(chunks), progress, desc) print("[UV Player]\n") song.play()
def __init__(self, threadNum=3): self.config = TidalConfig() self.tool = TidalTool() self.thread = ThreadTool(int(threadNum)) self.ffmpeg = FFmpegTool(mergerTimeout=45) self.progress = ProgressTool(100) self.check = CheckTool() self.showpro = False if self.config.showprogress == 'True': self.showpro = True
def __init__(self, threadNum=3): self.config = TidalConfig() self.tool = TidalTool() self.thread = ThreadTool(int(threadNum)) self.ffmpeg = FFmpegTool(mergerTimeout=45) self.progress = ProgressTool(100) self.check = CheckTool() pathHelper.mkdirs(self.config.outputdir + "/Album/") pathHelper.mkdirs(self.config.outputdir + "/Playlist/") pathHelper.mkdirs(self.config.outputdir + "/Video/") pathHelper.mkdirs(self.config.outputdir + "/Favorite/")
def mergerByM3u8_Multithreading(self, url, filepath, showprogress=False, showshell=False): """ #Func : 多线程下载并合并文件(使用M3u8的url) #Param : url [in] 链接 #Param : filepath [in] 目标文件名 #Param : showprogress [in] 是否显示进度条 #Param : showshell [in] 是否显示cmd信息 #Return : True/False """ try: # Get urllist urllist = self.__parseM3u8(url) if len(urllist) <= 0: return False # Creat tmpdir path = pathHelper.getDirName(filepath) tmpPath = pathHelper.getDiffTmpPathName(path) if pathHelper.mkdirs(tmpPath) is False: return False # Progress self.progress = None if showprogress: self.progress = ProgressTool(len(urllist)) # Download files allpath = [] self.waitCount = len(urllist) self.completeCount = 0 for i, item in enumerate(urllist): index = i + 100001 path = tmpPath + '/' + str(index) + ".ts" path = os.path.abspath(path) allpath.append(path) if os.path.exists(path): os.remove(path) self.thread.start(self.__thradfunc_dl, item, path, 3) self.thread.waitAll() ret = self.mergerByTs(tmpPath, filepath, showshell) # ret = self.mergerByFiles(allpath, filepath, showshell) shutil.rmtree(tmpPath) return ret except: return False
def start(self, showProgress: bool = False, threadNum: int = 10) -> (bool, str): size = len(self.fileUrls) if size == 1: fileSize, parts, msg = self.__getOneUrlParts__(self.fileUrls[0]) elif size > 1: fileSize, parts, msg = self.__getMoreUrlsParts__(self.fileUrls) else: return False, "Urls is empty." if msg != "": return False, msg self.maxSize = fileSize try: check = createEmptyFile(self.filePath, fileSize) if not check: return False, "Create file failed." if self.userProgress is not None: self.userProgress.setMaxNum(fileSize) # thread threads = ThreadTool(threadNum) fileSize, unit = unitFix(fileSize, Unit.BYTE, Unit.MB) # Progress progress = None if showProgress: progress = ProgressTool(fileSize, 15, unit=unit.name) for item in parts: threads.start(__downloadPartFile__, item, self, progress, unit, 3) results = threads.waitAll() threads.close() for item in results: if item[0] is False: return False, "Some parts download failed." + item[1] return True, "" except Exception as e: return False, str(e)