Skip to content

Instantly share code, notes, and snippets.

@yoosefi
Last active May 16, 2024 23:59
Show Gist options
  • Save yoosefi/9c5d70c0f5f256ce4f2d to your computer and use it in GitHub Desktop.
Save yoosefi/9c5d70c0f5f256ce4f2d to your computer and use it in GitHub Desktop.
Bulk string replace for Transmission torrent destination paths
#!/usr/bin/python2
# MAKE SURE TRANSMISSION IS CLOSED FIRST, DUH
#
# this file works by iterating every file in the current directory (which should be the "resume" directory!),
# scanning them for the old path and updating the serialized data with the new path if found
#
# if you have renamed a drive or massively moved things around and want to rename a part of the destination path
# without having to hand-pick those torrents through the gui to move their associated destinations
# you can run this in your transmission "resume" directory.
# the directory is usually at ~/.config/transmission/resume
#
# tested from 2012 to 2024
# updated 2024.05.16 for auto-backups.
#
# BEGIN CONFIG
# multiple old-path-fragments to new-path-fragments (literal strings)
# don't use trailing slashes for directories, since transmission sometimes doesn't use them in destinations
replace = {
'/OLD/FOO':'/NEW/FOO',
'/OLD/BAR':'/NEW/BAR'
}
# END CONFIG
import os
import shutil
if (not os.path.exists('../resume.bak')):
os.mkdir('../resume.bak')
for resume_file in os.listdir('.') :
resume = open(resume_file).read()
for old in replace :
if resume.find(old) == -1 : continue
print resume_file
shutil.copy(resume_file,'../resume.bak/'+os.path.basename(resume_file))
dstart = resume.find('11:destination') + 14
dlen = resume[dstart:resume.find(':',dstart)]
d = resume[dstart+len(dlen)+1:dstart+len(dlen)+1+int(dlen)]
d = d.replace(old,replace[old])
open(resume_file,'w').write('%s%s:%s%s'%(resume[:dstart],len(d),d,resume[dstart+len(dlen)+1+int(dlen):]))
break
# https://github.com/yoosefi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment