FileOperator.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. for file in fileList:
  85. with open(file, "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. countEncChar = 0
  99. countDecChar = 0
  100. if isEncrypted is None:
  101. return
  102. if not isEncrypted: # 如果未加密
  103. pass
  104. else: # 如果加密则解密
  105. encryptedFile = open(file, 'rb')
  106. encryptedFile.seek(3)
  107. byte = encryptedFile.read()
  108. with open(file, 'wb') as decryptedFile:
  109. decryptedFile.write(byte)
  110. encryptedFile.close()
  111. decryptedFile.close()
  112. # return fileList
  113. def CopyFile(srcFileList, dstFolder):
  114. for file in srcFileList:
  115. shutil.copy(file, dstFolder)
  116. def MoveFile(srcFileList, dstFolder):
  117. for file in srcFileList:
  118. shutil.move(file, dstFolder)
  119. # 排序用到的key
  120. def GetSeries(dataList):
  121. return int(dataList.split('_')[-2])
  122. def DoRename(path, fileName, aID, isLocalPattern):
  123. # 获取.txt文件名
  124. filName = fileName
  125. # 读取.txt文件
  126. with open(filName, encoding='UTF-8') as f:
  127. lines = f.readlines() # 新文件名按行保存
  128. fileList = FindSpecialMp4Files(path, aID)[0] # 存储要copy的文件全名
  129. fileList.sort(key=GetSeries)
  130. # fileList = set(fileList) # 防止文件重复
  131. index = 0
  132. frontIndex = 0
  133. for oldDir in fileList:
  134. filetype = '.' + oldDir.split('.')[-1]
  135. frontIndex = int(oldDir.split('_')[-2])
  136. if isLocalPattern:
  137. newDir = os.path.join(path, str(frontIndex) + '. ' + lines[index].strip('\n') + filetype) # 新的文件路径
  138. index += 1
  139. else:
  140. newDir = os.path.join(path, str(frontIndex) + '. ' + lines[frontIndex-1].strip('\n') + filetype) # 新的文件路径
  141. os.rename(oldDir, newDir) # 重命名
  142. def GetInfoList(path, aID):
  143. fileTypeList = aID + '.info'
  144. fileList = [] # 含路径的文件名
  145. for dirPath, dirNames, fileNames in os.walk(path):
  146. for file in fileNames:
  147. if file == fileTypeList:
  148. file_fullname = os.path.join(dirPath, file) # 文件名
  149. fileList.append(file_fullname)
  150. fileList.sort(key=GetFileSeries) # 这里必须排序
  151. return fileList
  152. def GetLocalVideoTitle(path, aID):
  153. fileList = GetInfoList(path, aID)
  154. #  print(fileList)
  155. titleList = []
  156. findVideoTitle = re.compile(r'"PartName":"(.*?)"')
  157. for infoFile in fileList:
  158. with open(infoFile, encoding='UTF-8') as f:
  159. lines = f.readlines()
  160. s = str(lines[0])
  161. videoTitle = re.findall(findVideoTitle, s)[0]
  162. for s in videoTitle:
  163. cut = ['|', '\\', '/', ':', '?', '"', '<', '>']
  164. if s in cut:
  165. videoTitle = videoTitle.replace(s, ' ')
  166. titleList.append(videoTitle)
  167. return titleList
  168. def GetTxt(dataList, localTitle, path):
  169. fileTitle = localTitle + ".txt" # 合成.txt格式 文件名
  170. fileTitle = os.path.join(path, fileTitle)
  171. nameFile = open(fileTitle, "w", encoding="utf-8") # 写入文件
  172. j = 0
  173. for item in dataList:
  174. j += 1
  175. nameFile.write(item + "\n")
  176. nameFile.close()
  177. return fileTitle
  178. def DeleteTxt(delDir, delName):
  179. delList = os.listdir(delDir)
  180. for f in delList:
  181. if os.path.join(delDir, f) == delName:
  182. filePath = os.path.join(delDir, f)
  183. if os.path.isfile(filePath):
  184. os.remove(filePath)
  185. def DeleteDir(delDir):
  186. # 文件夹 带 - 的会删不掉,但是文件还是删的掉的
  187. # os.system(f"attrib -r {delDir}") # 增加可对文件夹产生修改的权限
  188. # shutil.rmtree(delDir, True)
  189. if os.path.exists(delDir):
  190. shutil.rmtree(delDir, onerror=readonly_handler)
  191. def readonly_handler(func, path, execinfo):
  192. os.chmod(path, stat.S_IWRITE)
  193. func(path)
  194. # 在指定的输出文件夹下面创建名称为name的文件夹
  195. def MakeDir(path, name):
  196. dir = os.path.join(path, name)
  197. if not os.path.exists(dir):
  198. os.makedirs(dir)
  199. else:
  200. dir = os.path.join(path, name + str(random.randint(0, 100)))
  201. os.makedirs(dir)
  202. return dir
  203. '''
  204. FileOperator 新增
  205. '''
  206. # 创建记忆文件
  207. def SaveForOutput(path, fileName):
  208. # fileName = "localPath.config"
  209. file = None
  210. fullpath = os.path.join(path, fileName)
  211. if not os.path.exists(fullpath): # 如果路径不存在,创建路径,写入文件,返回False
  212. try:
  213. file = open(fileName, "w", encoding="utf-8")
  214. except:
  215. return
  216. finally:
  217. if file:
  218. file.close()
  219. return False
  220. else:
  221. return True
  222. # 读文件的内容 这里的path是已经join过的
  223. def ReadForOutput(path):
  224. lines = ['', '']
  225. if os.path.isfile(path):
  226. with open(path, encoding='UTF-8') as f:
  227. lines = f.readlines() # 新文件名按行保存
  228. f.close()
  229. return lines
  230. # 写入首行的内容
  231. def WriteForOutput(path, downloadPath, outputPath):
  232. f = None
  233. try:
  234. f = open(path, "w", encoding="utf-8")
  235. f.write(downloadPath + '\n')
  236. f.write(outputPath)
  237. except:
  238. return
  239. finally:
  240. if f:
  241. f.close()
  242. # with open(path, "w", encoding="utf-8") as f:
  243. # f.write(downloadPath+'\n')
  244. # f.write(outputPath)