123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import os
- import random
- import re
- import shutil
- '''
- 传入path值,读取当前path一级目录下的.dvi文件,返回[isDviFounded,bid,aid,title]列表
- '''
- global localFileName
- def GetDviInfo(path):
- isDviFounded = False
- file_type = '.dvi'
- dviFile = None
- bid = None
- aid = None
- title = None
- description = None
- filelist = os.listdir(path)
- for file in filelist:
- if file_type in file:
- isDviFounded = True
- dviFile = os.path.join(path, file)
- if isDviFounded is False:
- return [isDviFounded, bid, aid, title]
- else:
- with open(dviFile, encoding='UTF-8') as f:
- lines = f.readlines()
- s = str(lines[0])
- findBid = re.compile(r'"Bid":"(.*?)"')
- findDviTitle = re.compile(r'"Title":"(.*?)"')
- findAid = re.compile(r'"Aid":"(.*?)"')
- bid = re.findall(findBid, s)[0]
- aid = re.findall(findAid, s)[0]
- title = re.findall(findDviTitle, s)[0]
- for s in title:
- cut = ['|', '\\', '/', ':', '?', '"', '<', '>']
- if s in cut:
- title = title.replace(s, ' ')
- return [isDviFounded, bid, aid, title]
- def GetFileSeries(fileList):
- return int(fileList.split('\\')[-2])
- def FindAllMp4Files(path):
-
- fileTypeList = ['mp4', 'MP4', 'mP4', 'Mp4']
- fileList = []
- fileNamelist = []
- for dirPath, dirNames, fileNames in os.walk(path):
- for file in fileNames:
- fileType = file.split('.')[-1]
- if fileType in fileTypeList:
- file_fullname = os.path.join(dirPath, file)
- fileList.append(file_fullname)
- fileNamelist.append(file)
- return [fileList, fileNamelist]
- def CopyFile(srcFileList, dstFolder):
- for file in srcFileList:
- shutil.copy(file, dstFolder)
- def MoveFile(srcFileList, dstFolder):
- for file in srcFileList:
- shutil.move(file, dstFolder)
- def GetSeries(dataList):
- return int(dataList.split('_')[-2])
- def DoRename(path, fileName, aID):
-
- filName = fileName
-
- with open(filName, encoding='UTF-8') as f:
- lines = f.readlines()
-
- fileTypeList = ['mp4', 'MP4', 'mP4', 'Mp4']
- fileList = []
-
- for dirPath, dirNames, fileNames in os.walk(path):
- for file in fileNames:
- if aID in file and file.split('.')[-1] in fileTypeList:
- oldName = os.path.join(dirPath, file)
- if os.path.isdir(oldName):
- continue
- fileList.append(oldName)
- fileList.sort(key=GetSeries)
-
- for oldDir in fileList:
- filetype = '.' + oldDir.split('.')[-1]
- index = int(oldDir.split('_')[-2]) - 1
- newDir = os.path.join(path, str(index + 1) + '. ' + lines[index].strip('\n') + filetype)
- os.rename(oldDir, newDir)
- def GetInfoList(path, aID):
- fileTypeList = aID + '.info'
- fileList = []
- for dirPath, dirNames, fileNames in os.walk(path):
- for file in fileNames:
- if file == fileTypeList:
- file_fullname = os.path.join(dirPath, file)
- fileList.append(file_fullname)
- fileList.sort(key=GetFileSeries)
- return fileList
- def GetLocalVideoTitle(path, aID):
- fileList = GetInfoList(path, aID)
-
- titleList = []
- findVideoTitle = re.compile(r'"PartName":"(.*?)"')
- for infoFile in fileList:
- with open(infoFile, encoding='UTF-8') as f:
- lines = f.readlines()
- s = str(lines[0])
- videoTitle = re.findall(findVideoTitle, s)[0]
- titleList.append(videoTitle)
- return titleList
- def GetTxt(dataList, localTitle, path):
- fileTitle = localTitle + ".txt"
- fileTitle = os.path.join(path, fileTitle)
- nameFile = open(fileTitle, "w", encoding="utf-8")
- j = 0
- for item in dataList:
- j += 1
- nameFile.write(item + "\n")
- nameFile.close()
- return fileTitle
- def DeleteTxt(delDir, delName):
- delList = os.listdir(delDir)
- for f in delList:
- if os.path.join(delDir, f) == delName:
- filePath = os.path.join(delDir, f)
- if os.path.isfile(filePath):
- os.remove(filePath)
- def DeleteDir(delDir):
-
- os.system(f"attrib -r {delDir}")
- shutil.rmtree(delDir, True)
- def MakeDir(path, name):
- dir = os.path.join(path, name)
- if not os.path.exists(dir):
- os.makedirs(dir)
- else:
- dir = os.path.join(path, name + str(random.randint(0, 100)))
- os.makedirs(dir)
- return dir
|