Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LostInsight/236ca44a8ef755e7aea21e658defd9d1 to your computer and use it in GitHub Desktop.
Save LostInsight/236ca44a8ef755e7aea21e658defd9d1 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)
try:
torrent = bencode.bread(file_path)
except:
print(file_path, " is not a valid fastresume file.")
continue
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