main.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import base64
  2. import datetime
  3. import os
  4. import threading
  5. from PyQt5 import QtCore
  6. from PyQt5.QtWidgets import *
  7. from PyQt5.QtGui import *
  8. import FileOperator
  9. import TestFileSystem
  10. from MainWindow import Ui_MainWindow
  11. from icon import img
  12. import TitleSpider
  13. global canRun
  14. def GetSeries(dataList):
  15. return int(dataList.split('_')[1])
  16. class MainApp(QMainWindow, Ui_MainWindow):
  17. def __init__(self):
  18. global canRun
  19. QMainWindow.__init__(self)
  20. if not self.SetUI():
  21. canRun = False
  22. return
  23. else:
  24. canRun = True
  25. self.setupUi(self)
  26. self.SetBaseInfo()
  27. self.InitMenuBar()
  28. self.HandleButtons()
  29. self.setFixedSize(self.width(), self.height())
  30. self.SetLogText()
  31. self.progressBar.setValue(0)
  32. self.InitCheckBox()
  33. self.path = os.getcwd()
  34. self.saveName = "set.ini"
  35. self.joinedPath = os.path.join(self.path, self.saveName) # 配置文件的位置
  36. self.isTextFileExists = False
  37. self.isTextFirstColumnHaveContent = False
  38. self.isTextSecondColumnHaveContent = False
  39. # self.InitOutPutPath() # 发布绿色版时注释
  40. def InitOutPutPath(self):
  41. self.isTextFileExists = FileOperator.SaveForOutput(self.path, self.saveName)
  42. if self.isTextFileExists is False: # 如果没有文件,在已经创建文件的前提下,等待用户输入手动填入OutputText. 相关事件在button事件实现
  43. # line = FileOperator.ReadForOutput() #
  44. pass
  45. else: # 存在文件 则读取文件,并自动赋给文本框
  46. self.lines = FileOperator.ReadForOutput(self.joinedPath)
  47. if len(self.lines) >= 2:
  48. if self.lines[0] != '':
  49. if os.path.isdir(self.lines[0].strip()):
  50. self.downloadDirEdit.setText(self.lines[0].strip())
  51. self.isTextFirstColumnHaveContent = True
  52. if self.lines[1] != '':
  53. if os.path.isdir(self.lines[1].strip()):
  54. self.outputDirEdit.setText(self.lines[1].strip())
  55. self.isTextSecondColumnHaveContent = True
  56. else:
  57. pass
  58. # self.Log("检测到配置文件set.ini 为空")
  59. # QMessageBox.critical(self, "错误", "配置文件set.ini !")
  60. def InitCheckBox(self):
  61. # self.txtFileCheckBox.setChecked(True) # 默认保存Txt
  62. # self.deleteFileCheckBox.setChecked(False) # 默认保留源目录
  63. # self.copyToOutput.setChecked(True) # 默认使用复制的方式
  64. self.txtFileCheckBox.setChecked(False) # 默认不保存Txt
  65. self.deleteFileCheckBox.setChecked(True) # 默认不保留源目录
  66. self.moveToOutput.setChecked(True) # 默认不使用复制的方式
  67. self.localMode.setChecked(True)
  68. def MutiThreadCopy(self, mp4List, outputPath):
  69. t = threading.Thread(target=FileOperator.CopyFile, args=(mp4List, outputPath))
  70. t.start()
  71. t.join()
  72. def MutiThreadMove(self, mp4List, outputPath):
  73. t = threading.Thread(target=FileOperator.MoveFile, args=(mp4List, outputPath))
  74. t.start()
  75. t.join()
  76. def CheckIsChecked(self): # 按下按钮的事件里调用,检查checkbox状态,并提示。最后给对应的bool变量赋值
  77. self.isSaveTxt = self.txtFileCheckBox.isChecked()
  78. self.isDeleteDir = self.deleteFileCheckBox.isChecked()
  79. if self.copyToOutput.isChecked() or self.moveToOutput.isChecked():
  80. pass
  81. else:
  82. QMessageBox.critical(self, "错误", "请至少勾选一种输出方式!")
  83. if self.localMode.isChecked() or self.spiderMode.isChecked():
  84. pass
  85. else:
  86. QMessageBox.critical(self, "错误", "请至少勾选一种处理模式(本地模式 或 爬虫模式)!")
  87. if self.copyToOutput.isChecked():
  88. self.isCopyOutput = True
  89. else:
  90. self.isCopyOutput = False
  91. if self.moveToOutput.isChecked():
  92. self.isMoveOutput = True
  93. else:
  94. self.isMoveOutput = False
  95. if self.localMode.isChecked():
  96. self.isLocalMode = True
  97. else:
  98. self.isLocalMode = False
  99. if self.spiderMode.isChecked():
  100. QMessageBox.warning(self, "警告", "爬虫模式依赖网络,关闭本窗口前请确保代理服务是关闭状态")
  101. self.isSpiderMode = True
  102. else:
  103. self.isSpiderMode = False
  104. def SetLogText(self):
  105. self.activityLogEdit.setReadOnly(True)
  106. def Log(self, msg):
  107. self.statusbar.showMessage(msg)
  108. self.activityLogEdit.appendPlainText('[{0}]'.format(str(datetime.datetime.now())[0:19]))
  109. self.activityLogEdit.appendPlainText(msg)
  110. self.activityLogEdit.appendPlainText('')
  111. def LogOnBar(self, msg):
  112. self.statusbar.showMessage(msg)
  113. def HandleButtons(self):
  114. self.downloadDirButton.clicked.connect(self.OpenDownloadDir)
  115. self.outputDirButton.clicked.connect(self.OpenOutputDir)
  116. self.renameButton.clicked.connect(self.RenameFile)
  117. self.copyToOutput.clicked.connect(self.DisableMove)
  118. self.moveToOutput.clicked.connect(self.DisableCopy)
  119. self.localMode.clicked.connect(self.DisableSpiderMode)
  120. self.spiderMode.clicked.connect(self.DisableLocalMode)
  121. # 处理checkbox冲突
  122. def DisableCopy(self):
  123. if self.copyToOutput.isChecked():
  124. self.copyToOutput.setChecked(False)
  125. def DisableMove(self):
  126. if self.moveToOutput.isChecked():
  127. self.moveToOutput.setChecked(False)
  128. def DisableSpiderMode(self):
  129. if self.spiderMode.isChecked():
  130. self.spiderMode.setChecked(False)
  131. self.renameButton.setText("一键解密+整理+重命名")
  132. def DisableLocalMode(self):
  133. if self.localMode.isChecked():
  134. self.localMode.setChecked(False)
  135. self.renameButton.setText("一键解密+爬取+整理+重命名")
  136. def InitMenuBar(self):
  137. # 添加menu“帮助”的事件
  138. aboutAction = QAction('&关于', self)
  139. # aboutAction.setStatusTip('关于')
  140. aboutAction.triggered.connect(self.ShowAboutDialog)
  141. # 已有菜单栏,此处只需要添加菜单
  142. mainPageMenu = self.menubar.addMenu('&主页')
  143. helpMenu = self.menubar.addMenu('&帮助')
  144. # 菜单绑定之前添加的事件
  145. helpMenu.addAction(aboutAction)
  146. # 设置UI
  147. def SetUI(self):
  148. tmp = None
  149. try:
  150. tmp = open('tmp.png', "wb+")
  151. tmp.write(base64.b64decode(img))
  152. except Exception as e:
  153. QMessageBox.critical(self, "错误", "您当前安装在了C盘,软件权限不足:\n解决方案一:请关闭本程序并每次使用管理员权限运行本软件 \n解决方案二:卸载本软件再重新安装至其它非系统盘 比如:D盘、E盘!")
  154. return False
  155. finally:
  156. if tmp:
  157. tmp.close()
  158. icon = QIcon('tmp.png')
  159. os.remove("tmp.png")
  160. self.setWindowIcon(icon)
  161. return True
  162. def ShowAboutDialog(self):
  163. about_text = "<p>描述:这是一款致力于解决BiliBili UWP版下载后的视频加密、命名信息丢失和存放位置不合理等痛点的软件</p><p>版本:4.5</p><p>@Author:LZY</p><p>@github:love" \
  164. "-in-cpp</p> "
  165. QMessageBox.about(self, '说明', about_text)
  166. def OpenDownloadDir(self):
  167. if self.isTextFirstColumnHaveContent is False:
  168. dName = QFileDialog.getExistingDirectory(self, '选择下载文件夹', '/')
  169. self.downloadDirEdit.setText(dName)
  170. else:
  171. dName = QFileDialog.getExistingDirectory(self, '选择下载文件夹', self.lines[0].strip())
  172. self.downloadDirEdit.setText(dName)
  173. def OpenOutputDir(self):
  174. if self.isTextSecondColumnHaveContent is False:
  175. dName = QFileDialog.getExistingDirectory(self, '选择输出文件夹', '/')
  176. self.outputDirEdit.setText(dName)
  177. else:
  178. dName = QFileDialog.getExistingDirectory(self, '选择输出文件夹', self.lines[1].strip())
  179. self.outputDirEdit.setText(dName)
  180. def SetBaseInfo(self):
  181. self.setWindowTitle('BiliBili UWP版视频解密整理工具')
  182. self.downloadDirEdit.setToolTip(r"例如:E:\BiliDownload\44938322")
  183. self.downloadDirEdit.setPlaceholderText("路径请具体到单个数字名称的文件夹,暂不支持文件夹的批量处理")
  184. self.outputDirEdit.setPlaceholderText("您希望处理后的文件被保存到的地方")
  185. # def FindFiles(self,downloadPath):
  186. def RenameFile(self):
  187. self.CheckIsChecked()
  188. self.progressBar.setValue(0)
  189. # 进入目录查找dvi文件
  190. downloadPath = self.downloadDirEdit.toPlainText()
  191. outputPath = self.outputDirEdit.toPlainText()
  192. if os.path.isdir(downloadPath) is False or os.path.isdir(self.downloadDirEdit.toPlainText().strip()) is False:
  193. self.Log('UWP下载目录的路径存在非法输入!')
  194. else:
  195. self.Log("进入目录:{0}".format(downloadPath))
  196. dviInfoList = FileOperator.GetDviInfo(downloadPath) # 获取dvi文件信息
  197. if dviInfoList[0] is False:
  198. self.Log('没有找到.dvi文件!请检查下载目录后重试!')
  199. else:
  200. # 在outputDir下新建名为dvi[3]文件夹
  201. try:
  202. outputPath = FileOperator.MakeDir(outputPath, dviInfoList[3])
  203. except Exception as e:
  204. QMessageBox.critical(self, "错误", "已经存在同名文件夹! Error:" + str(e))
  205. return
  206. if self.isSpiderMode:
  207. self.Log("开始爬取BV:{0}, 标题:{1} 的所有视频标题,请稍后...".format(dviInfoList[1], dviInfoList[3]))
  208. try:
  209. TitleSpider.GetTxt(dviInfoList[1], outputPath)
  210. except Exception as e:
  211. QMessageBox.critical(self, "错误", "请检查网络后重试 Error:" + str(e))
  212. return
  213. # 调用爬虫产生.txt
  214. global fileName
  215. fileName = TitleSpider.fileName
  216. self.LogOnBar('已成功爬取文件: {0} ! 注:只显示部分文件名'.format(fileName[0:35]))
  217. self.Log('已成功爬取文件: {0} !'.format(fileName))
  218. elif self.isLocalMode:
  219. self.Log("开始遍历获取BV:{0}, 标题:{1} 的所有视频标题,请稍后...".format(dviInfoList[1], dviInfoList[3]))
  220. localVideoTitleList = FileOperator.GetLocalVideoTitle(downloadPath, dviInfoList[2])
  221. fileName = FileOperator.GetTxt(localVideoTitleList, dviInfoList[3], outputPath)
  222. self.Log('已成功获取文件: {0} !'.format(fileName))
  223. else:
  224. self.Log("impossible")
  225. # 找到所有downloadPath的.mp4文件
  226. mp4List = FileOperator.FindSpecialMp4Files(downloadPath, dviInfoList[2])[0] # mp4真正在的地方
  227. # Log
  228. mp4nameList = FileOperator.FindSpecialMp4Files(downloadPath, dviInfoList[2])[1]
  229. try:
  230. mp4nameList.sort(key=GetSeries)
  231. except Exception as e:
  232. QMessageBox.critical(self, "错误", "存在干扰文件!排序错误,请联系作者!" + str(e))
  233. return
  234. s = "查询到以下mp4文件:\n"
  235. for item in mp4nameList:
  236. s += (item + '\n')
  237. self.Log(s)
  238. if os.path.isdir(outputPath) is False or os.path.isdir(self.outputDirEdit.toPlainText().strip()) is False:
  239. self.Log('输出目录的路径存在非法输入!')
  240. else:
  241. # 记忆输出目录
  242. # FileOperator.WriteForOutput(self.joinedPath, os.path.dirname(downloadPath), self.outputDirEdit.toPlainText()) # 发布绿色版时注释
  243. # 解密
  244. self.Log("开始解密...")
  245. FileOperator.DecryptMp4(downloadPath, dviInfoList[2])
  246. self.Log("解密完毕!")
  247. # 复制
  248. self.CopyOrMove(self.isCopyOutput, mp4List, outputPath)
  249. # 重命名
  250. self.Log("开始重命名...")
  251. FileOperator.DoRename(outputPath, fileName, dviInfoList[2], self.isLocalMode)
  252. self.Log("重命名完毕!")
  253. # 进度条100%
  254. self.progressBar.setValue(100)
  255. # 是否保存.txt文件
  256. if self.isSaveTxt is True:
  257. pass
  258. else:
  259. self.Log("正在删除程序运行过程中产生的.txt文件")
  260. FileOperator.DeleteTxt(outputPath, fileName)
  261. self.Log("删除.txt文件成功!")
  262. # 是否删除源文件夹
  263. if self.isDeleteDir is True:
  264. self.Log("正在删除源文件夹")
  265. FileOperator.DeleteDir(downloadPath)
  266. self.Log("删除源文件夹成功!")
  267. else:
  268. pass
  269. # 重命名输出文件夹 搁置
  270. # 输出方式:复制或移动
  271. def CopyOrMove(self, isCopyTo, mp4List, outputPath):
  272. if isCopyTo is True:
  273. self.Log("进入目录:{0}".format(outputPath))
  274. self.Log("开始复制... 这可能需要一段时间...")
  275. self.MutiThreadCopy(mp4List, outputPath) # 多线程复制
  276. self.Log("复制完毕!")
  277. else:
  278. self.Log("进入目录:{0}".format(outputPath))
  279. self.Log("开始移动... 这可能需要一段时间...")
  280. self.MutiThreadMove(mp4List, outputPath) # 多线程移动
  281. self.Log("移动完毕!")
  282. def DSpiderMode(self):
  283. pass
  284. def DoLocalMode(self):
  285. pass
  286. if __name__ == '__main__':
  287. QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
  288. app = QApplication([])
  289. window = MainApp()
  290. if canRun:
  291. window.show()
  292. app.exec_()