2024-09-25 19:51

将Windows系统qBittorrent中的种子免校验转移到飞牛私有云的qBittorrent中

qBittorrent 软件中所有的种子信息都有备份,保存在BT_backup文件夹中,该文件夹一般位于C:\Users\Administrator\AppData\Local\qBittorrent\BT_backup目录中。其中包括种子文件*.torrent及种子恢复文件*.fastresume

.fastresume文件是Bencode 编码,可以直接用文本编辑器打开,虽然有乱码,但是不影响查看文件夹相关信息,可以用来确定旧文件夹及查看脚本修改是否生效。当然不能直接修改,直接编辑器修改后编码就被破坏了,所以编辑器批量替换路径也是不行的。

1、万事先备份,请先备份BT_backup文件夹

2、然后,直接一个脚本解决:

  1. import os
  2. import re
  3. # 备份后的 BT_backup 文件夹路径,linux系统,所以根目录直接是/,如果是 win 系统,应该是类似 C:\格式开头,请根据自己的实际路径修改
  4. torrentsDir = r'/Users/dt27/Downloads/BT_backup/'
  5. # 新旧文件夹及其对应关系写在这里,
  6. # 格式:b'旧文件夹路径': '新文件夹路径'
  7. # 多个文件夹之间用英文逗号,分隔。
  8. # Windows 文件夹这里写两个斜杠\\是因为单独一个斜杠\在代码中有特殊意义,所以要写两个表示转义,不明白的按我这么写就行了。
  9. # 如果不确定旧文件夹,这一行可以直接写fastresumePaths = {} 这样会把所有种子的旧文件夹列出来。
  10. fastresumePaths = {b'D:\\TV\\': b'/vol1/1000/TV', b'D:\\Movie\\': b'/vol1/1000/Movie', b'D:\\Music\\': b'/vol1/1000/Music'}
  11. if not fastresumePaths:
  12. print('Finding paths...\n')
  13. detectedPaths = []
  14. # Loop through torrents directory
  15. for file in os.listdir(torrentsDir):
  16. if file.endswith('.fastresume'):
  17. with open(torrentsDir + file, 'rb') as fastresumeFile:
  18. filedata = fastresumeFile.read()
  19. #pattern = re.compile(br'save_path(\d+):')
  20. #query = pattern.search(filedata)
  21. query = re.search(br'save_path(\d+):', filedata)
  22. if query is None:
  23. print(f'Could not find path in {file}')
  24. else:
  25. pathLen = int(query.group(1))
  26. path = filedata[query.end():query.end() + pathLen]
  27. # check if user entered paths to change or not
  28. if fastresumePaths:
  29. print(fastresumePaths)
  30. if path in fastresumePaths:
  31. newPath = fastresumePaths[path]
  32. filedata = filedata.replace(
  33. bytes(str(pathLen), 'utf-8') + b':' + path,
  34. bytes(str(len(newPath)), 'utf-8') + b':' + newPath
  35. )
  36. print(f"Replaced {path} with {newPath}")
  37. with open(torrentsDir + file, 'wb') as fastresumeFile:
  38. fastresumeFile.write(filedata)
  39. else:
  40. if path not in detectedPaths:
  41. detectedPaths.append(path)
  42. if fastresumePaths is None or not fastresumePaths:
  43. if not detectedPaths:
  44. print('No paths were found')
  45. else:
  46. print('The following paths were found\n')
  47. for path in detectedPaths:
  48. print(path)
  49. print('\nCopy the following code into the "fastresumePaths" variable to replace paths.\n')
  50. maxPathLength = len(max(detectedPaths, key=len)) + 4
  51. print(f"# {'Original Path'.ljust(maxPathLength)} New Path")
  52. print(f"# {'-'*(maxPathLength)} {'-'*(maxPathLength)}\n")
  53. print('fastresumePaths = {')
  54. for path in detectedPaths:
  55. print(f" {str(path).ljust(maxPathLength)}: {str(path).ljust(maxPathLength)},")
  56. print('}\n')

脚本来源:Replace paths easily in qBittorrent .fastresume (useful when switching from windows to linux)

本脚本在 MacOS 中测试无误,其他系统执行脚本时可能会出现报错信息,请根据报错信息自己排查。
一般就是路径格式或字符串格式的问题。

DT27

原创文章,欢迎转载。转载请注明:转载自 DT27's Blog,谢谢!
原文链接:https://dt27.cn/soft/1996/

添加新评论