Skip to content

Instantly share code, notes, and snippets.

@neilzheng
Created January 13, 2020 03:59
Show Gist options
  • Save neilzheng/b5248d90863e12db8ed4436b2d16de8f to your computer and use it in GitHub Desktop.
Save neilzheng/b5248d90863e12db8ed4436b2d16de8f to your computer and use it in GitHub Desktop.
'''
batch change tracker for qbittorrent 4.2-
'''
import sys
import os
import glob
import bencode
if __name__ == '__main__':
if len(sys.argv) != 4:
print('usage: {} dir_name old_tracker new_tracker'.format(sys.argv[0]))
sys.exit(1)
dir_name = sys.argv[1]
old_tracker = sys.argv[2]
new_tracker = sys.argv[3]
files = glob.glob(os.path.join(dir_name, '*.torrent'))
for tname in files:
rname = '{}.fastresume'.format(os.path.splitext(tname)[0])
tdata = bencode.bread(tname)
print('Proccesing torrent ({})'.format(tdata['info']['name']))
print(tdata['announce'])
if old_tracker not in tdata['announce']:
print('Tracker not match, skipping.')
continue
print('Updating tracker in torrent.')
tdata['announce'] = tdata['announce'].replace(old_tracker, new_tracker)
rdata = bencode.bread(rname)
print('Updating tracker in fastresume.')
for tlist in rdata['trackers']:
for i in range(0, len(tlist)):
if old_tracker in tlist[i]:
tlist[i] = tlist[i].replace(old_tracker, new_tracker)
bencode.bwrite(tdata, tname)
bencode.bwrite(rdata, rname)
print('Updated.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment