Skip to content

Instantly share code, notes, and snippets.

@AlexanderMakarov
Last active April 8, 2023 06:41
Show Gist options
  • Save AlexanderMakarov/51ba3fcb3118dc5093d846875185abf1 to your computer and use it in GitHub Desktop.
Save AlexanderMakarov/51ba3fcb3118dc5093d846875185abf1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import bencode
import re
import sys
# Inspired by https://github.com/ctminime/QB_Migrate_to_Linux
# Need `pip3 install bencode.py`.
# Run in folder like "/home/user/.local/share/data/qBittorrent/BT_backup/" (configured in qBitTorrent).
# Takes 2 parameters:
# - path (string) change from,
# - path (string) change to.
# See below for Win -> Unix paths convertion help.
assert len(sys.argv) > 2, "Please specify first path change from, next path change to."
save_path_from = sys.argv[1]
save_path_to = sys.argv[2]
# For each file in the directory that has the extension ".fastresume"
directory_path = os.getcwd()
for file in os.listdir(directory_path):
if file.endswith(".fastresume"):
file_path = os.path.join(directory_path, file)
torrent = bencode.bread(file_path)
save_path_orig = (torrent['save_path'])
if save_path_orig.startswith(save_path_from):
save_path = save_path_orig.replace(save_path_from, save_path_to)
# Uncomment below to convert Win -> Unix
#save_path = re.sub("\\\\", "/", save_path)
# Need change 2 locations for qBitTorrent.
torrent['save_path'] = save_path
torrent['qBt-savePath'] = save_path
bencode.bwrite(torrent, file_path)
# Print the new torrent path
print("%s: %s -> %s" % (os.path.basename(file_path), save_path_orig, torrent['save_path']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment