def setTag(self, tag, srcfile, coverpath=None): path = pathHelper.getDirName(srcfile) name = pathHelper.getFileNameWithoutExtension(srcfile) ext = pathHelper.getFileExtension(srcfile) oext = ext if 'm4a' in ext or 'mp4' in ext: oext = '.mp3' if 'mp3' not in oext: coverpath = None tmpfile = path + '/' + 'TMP' + name + oext try: data = AudioSegment.from_file(srcfile, format=ext[1:]) check = data.export(tmpfile, format=oext[1:], tags=tag, cover=coverpath) check.close() except Exception as e: pathHelper.remove(tmpfile) return if fileHelper.getFileSize(tmpfile) > 0: pathHelper.remove(srcfile) os.rename(tmpfile, path + '/' + name + oext) else: pathHelper.remove(tmpfile)
def setTrackMetadata(self, track_info, file_path, album_info, index): path = pathHelper.getDirName(file_path) name = pathHelper.getFileNameWithoutExtension(file_path) exte = pathHelper.getFileExtension(file_path) tmpfile = path + '/' + self.tmpfileFlag + name + exte try: tag = { 'Artist': track_info['artist']['name'], 'Album': track_info['album']['title'], 'Title': track_info['title'], 'CopyRight': track_info['copyright'], 'Track': track_info['trackNumber'] } if index is not None: tag['Track'] = str(index) if album_info is not None: tag['Date'] = album_info['releaseDate'] tag['Year'] = album_info['releaseDate'].split('-')[0] # tmp file pathHelper.copyFile(file_path, tmpfile) # set metadata ext = os.path.splitext(tmpfile)[1][1:] data = AudioSegment.from_file(tmpfile, ext) check = data.export(tmpfile, format=ext, tags=tag) # check file size if fileHelper.getFileSize(tmpfile) > 0: pathHelper.remove(file_path) pathHelper.copyFile(tmpfile, file_path) except: pass if os.access(tmpfile, 0): pathHelper.remove(tmpfile)
def __isNeedDownload(self, path, url): curSize = fileHelper.getFileSize(path) if curSize <= 0: return True netSize = netHelper.getFileSize(url) if curSize >= netSize: return False return True
def checkPaths(self): index = 0 flag = False errIndex = [] for path in self.paths: if fileHelper.getFileSize(path) <= 0: errIndex.append(index) flag = True index = index + 1 return flag, errIndex
def getFileContent(self,path, isBin=False): mode = 'r' if isBin: mode = 'rb' try: size = fileHelper.getFileSize(path) if size <= 0: return "" with open(path, mode, encoding="utf-8") as fd: content = fd.read(size) return content except Exception as e: return ""