FileOperator.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. # -*- coding = utf-8 -*-
  2. # @Time : 2022/6/22 12:29
  3. # @Author : 刘正阳
  4. # @File : FileOperator.py
  5. # @Software : PyCharm
  6. import os
  7. import random
  8. import re
  9. import shutil
  10. import stat
  11. '''
  12. 传入path值,读取当前path一级目录下的.dvi文件,返回[isDviFounded,bid,aid,title]列表
  13. '''
  14. global localFileName
  15. def GetDviInfo(path):
  16. isDviFounded = False
  17. file_type = '.dvi'
  18. dviFile = None
  19. bid = None
  20. aid = None
  21. title = None
  22. description = None
  23. filelist = os.listdir(path)
  24. for file in filelist:
  25. if file_type in file:
  26. isDviFounded = True
  27. dviFile = os.path.join(path, file)
  28. if isDviFounded is False:
  29. return [isDviFounded, bid, aid, title]
  30. else:
  31. with open(dviFile, encoding='UTF-8') as f:
  32. lines = f.readlines()
  33. s = str(lines[0])
  34. findBid = re.compile(r'"Bid":"(.*?)"')
  35. findDviTitle = re.compile(r'"Title":"(.*?)"')
  36. findAid = re.compile(r'"Aid":"(.*?)"')
  37. bid = re.findall(findBid, s)[0]
  38. aid = re.findall(findAid, s)[0]
  39. title = re.findall(findDviTitle, s)[0]
  40. for s in title:
  41. cut = ['|', '\\', '/', ':', '?', '"', '<', '>']
  42. if s in cut:
  43. title = title.replace(s, ' ')
  44. return [isDviFounded, bid, aid, title]
  45. def GetFileSeries(fileList):
  46. return int(fileList.split('\\')[-2])
  47. def FindAllMp4Files(path):
  48. # 这里是不需要对输出结果排序的,因为在移动这些文件后,DoRename调用被移动的文件,会排好序
  49. fileTypeList = ['mp4', 'MP4', 'mP4', 'Mp4']
  50. fileList = [] # 存储要copy的文件全名
  51. fileNamelist = []
  52. for dirPath, dirNames, fileNames in os.walk(path):
  53. for file in fileNames:
  54. fileType = file.split('.')[-1]
  55. if fileType in fileTypeList:
  56. file_fullname = os.path.join(dirPath, file) # 文件全名
  57. fileList.append(file_fullname)
  58. fileNamelist.append(file)
  59. return [fileList, fileNamelist]
  60. def FindSpecialMp4Files(path, aID):
  61. # 提取出含有指定特征的fileList
  62. fileTypeList = ['mp4', 'MP4', 'mP4', 'Mp4']
  63. fileList = [] # 存储要copy的文件全名
  64. fileNameList = []
  65. # 获取要被命名的文件,包含了文件夹有其它文件或文件夹的情况
  66. for dirPath, dirNames, fileNames in os.walk(path):
  67. for file in fileNames:
  68. if aID in file and file.split('.')[-1] in fileTypeList: #
  69. oldName = os.path.join(dirPath, file) # 文件全名
  70. if os.path.isdir(oldName):
  71. continue
  72. fileList.append(oldName)
  73. fileNameList.append(file)
  74. return [fileList, fileNameList]
  75. # 检测文件是否加密 是则解密
  76. def DecryptMp4(path, aID):
  77. isEncrypted = None
  78. s = None
  79. encryptedFile = None
  80. decryptedFile = None
  81. countEncChar = 0 # 检测'xff'数量
  82. countDecChar = 0 # 检测'x00'数量
  83. fileList = FindSpecialMp4Files(path, aID)[0]
  84. testFile = fileList[0]
  85. with open(testFile, "rb") as f:
  86. s = str(f.readline())[3:14]
  87. f.close()
  88. sList = s.split('\\') # ['xff', 'xff', 'xff']
  89. for item in sList:
  90. if 'xff' in item:
  91. countEncChar += 1
  92. if 'x00' in item:
  93. countDecChar += 1
  94. if countEncChar == 3:
  95. isEncrypted = True # 加密
  96. if countDecChar == 3:
  97. isEncrypted = False # 未加密
  98. if isEncrypted is None:
  99. return
  100. if not isEncrypted: # 如果未加密
  101. pass
  102. else: # 如果加密则解密
  103. for file in fileList:
  104. encryptedFile = open(file, 'rb')
  105. encryptedFile.seek(3)
  106. byte = encryptedFile.read()
  107. with open(file, 'wb') as decryptedFile:
  108. decryptedFile.write(byte)
  109. encryptedFile.close()
  110. decryptedFile.close()
  111. # return fileList
  112. def CopyFile(srcFileList, dstFolder):
  113. for file in srcFileList:
  114. shutil.copy(file, dstFolder)
  115. def MoveFile(srcFileList, dstFolder):
  116. for file in srcFileList:
  117. shutil.move(file, dstFolder)
  118. # 排序用到的key
  119. def GetSeries(dataList):
  120. return int(dataList.split('_')[-2])
  121. def DoRename(path, fileName, aID):
  122. # 获取.txt文件名
  123. filName = fileName
  124. # 读取.txt文件
  125. with open(filName, encoding='UTF-8') as f:
  126. lines = f.readlines() # 新文件名按行保存
  127. fileList = FindSpecialMp4Files(path, aID)[0] # 存储要copy的文件全名
  128. fileList.sort(key=GetSeries)
  129. # fileList = set(fileList) # 防止文件重复
  130. index = 0
  131. frontIndex = 0
  132. for oldDir in fileList:
  133. filetype = '.' + oldDir.split('.')[-1]
  134. frontIndex = int(oldDir.split('_')[-2])
  135. newDir = os.path.join(path, str(frontIndex) + '. ' + lines[index].strip('\n') + filetype) # 新的文件路径
  136. index += 1
  137. os.rename(oldDir, newDir) # 重命名
  138. def GetInfoList(path, aID):
  139. fileTypeList = aID + '.info'
  140. fileList = [] # 含路径的文件名
  141. for dirPath, dirNames, fileNames in os.walk(path):
  142. for file in fileNames:
  143. if file == fileTypeList:
  144. file_fullname = os.path.join(dirPath, file) # 文件名
  145. fileList.append(file_fullname)
  146. fileList.sort(key=GetFileSeries) # 这里必须排序
  147. return fileList
  148. def GetLocalVideoTitle(path, aID):
  149. fileList = GetInfoList(path, aID)
  150. #  print(fileList)
  151. titleList = []
  152. findVideoTitle = re.compile(r'"PartName":"(.*?)"')
  153. for infoFile in fileList:
  154. with open(infoFile, encoding='UTF-8') as f:
  155. lines = f.readlines()
  156. s = str(lines[0])
  157. videoTitle = re.findall(findVideoTitle, s)[0]
  158. titleList.append(videoTitle)
  159. return titleList
  160. def GetTxt(dataList, localTitle, path):
  161. fileTitle = localTitle + ".txt" # 合成.txt格式 文件名
  162. fileTitle = os.path.join(path, fileTitle)
  163. nameFile = open(fileTitle, "w", encoding="utf-8") # 写入文件
  164. j = 0
  165. for item in dataList:
  166. j += 1
  167. nameFile.write(item + "\n")
  168. nameFile.close()
  169. return fileTitle
  170. def DeleteTxt(delDir, delName):
  171. delList = os.listdir(delDir)
  172. for f in delList:
  173. if os.path.join(delDir, f) == delName:
  174. filePath = os.path.join(delDir, f)
  175. if os.path.isfile(filePath):
  176. os.remove(filePath)
  177. def DeleteDir(delDir):
  178. # 文件夹 带 - 的会删不掉,但是文件还是删的掉的
  179. # os.system(f"attrib -r {delDir}") # 增加可对文件夹产生修改的权限
  180. # shutil.rmtree(delDir, True)
  181. if os.path.exists(delDir):
  182. shutil.rmtree(delDir, onerror=readonly_handler)
  183. def readonly_handler(func, path, execinfo):
  184. os.chmod(path, stat.S_IWRITE)
  185. func(path)
  186. # 在指定的输出文件夹下面创建名称为name的文件夹
  187. def MakeDir(path, name):
  188. dir = os.path.join(path, name)
  189. if not os.path.exists(dir):
  190. os.makedirs(dir)
  191. else:
  192. dir = os.path.join(path, name + str(random.randint(0, 100)))
  193. os.makedirs(dir)
  194. return dir
  195. '''
  196. FileOperator 新增
  197. '''
  198. # 创建记忆文件
  199. def SaveForOutput(path, fileName):
  200. # fileName = "localPath.config"
  201. fullpath = os.path.join(path, fileName)
  202. if not os.path.exists(fullpath): # 如果路径不存在,创建路径,写入文件,返回False
  203. file = open(fileName, "w", encoding="utf-8")
  204. file.close()
  205. return False
  206. else:
  207. return True
  208. # 读文件的内容 这里的path是已经join过的
  209. def ReadForOutput(path):
  210. lines = ['', '']
  211. if os.path.isfile(path):
  212. with open(path, encoding='UTF-8') as f:
  213. lines = f.readlines() # 新文件名按行保存
  214. f.close()
  215. return lines
  216. # 写入首行的内容
  217. def WriteForOutput(path, downloadPath, outputPath):
  218. with open(path, "w", encoding="utf-8") as f:
  219. f.write(downloadPath+'\n')
  220. f.write(outputPath)