elfs: (Default)
[personal profile] elfs
As most people here know, I'm a big fan of anime, and most of my anime comes to me via BitTorrent. One of things that I've found commonplace is that many trackers obfuscate or otherwise mangle their torrent names. BitTorrent is tolerant of these shennanigans and even has a handler to download the files "as named on the command line" or "as named in the file."

I prefer to know what I'm dealing with before I load it into the torrent handler directly, so I wrote the following little script. It takes any torrent file and renames it to match the internal file or directory name.

 #!/bin/env python

import os
import os.path
import sys

header = 'file name.....: '
head2 = 'directory name: '

for i in sys.argv[1:]:
    try:
        f = os.popen( "btshowmetainfo.py %s" % i )
        while(1):
            l = f.readline()
            if l == '':
                raise "No."
            if l[0:len(header)] == header:
                fn = l[len(header):-1] + '.torrent'
                if os.path.exists( fn ):
                    print >>sys.stderr, "Could not rename %s as %s" % ( i, fn )
                    break
                os.rename( i, fn )
                break
            if l[0:len(head2)] == head2:
                fn = l[len(head2):-1] + '.torrent'
                if os.path.exists( fn ):
                    print >>sys.stderr, "Could not rename %s as %s" % ( i, fn )
                    break
                os.rename( i, fn )
                break
    except:
        print >>sys.stderr, "Could not rename %s" % i

May I suggest?

Date: 2005-04-02 12:12 am (UTC)
fallenpegasus: amazon (Default)
From: [personal profile] fallenpegasus
#!/usr/bin/python

import os
import os.path
import sys
import BitTorrent.bencode

if len(sys.argv) == 1:
    print '%s file1.torrent file2.torrent file3.torrent ...' % argv[0]
    exit(2)

for fn in sys.argv[1:]:
    metainfo_file = open(fn, 'rb')
    metainfo = BitTorrent.bencode.bdecode(metainfo_file.read())
    metainfo_file.close()
    info = metainfo['info']
    newfn = info['name']  + '.torrent'
    if (fn == newfn):
        pass  # name already correct, do nothing
    elif os.path.exists(newfn):
        print >>sys.stderr, "Wont overwrite existing %s with %s" % (newfn, fn)
    else:
        os.rename(fn, newfn)


It probably needs exception handling added.

Profile

elfs: (Default)
Elf Sternberg

December 2025

S M T W T F S
 12345 6
78910111213
14151617181920
21222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 20th, 2026 03:11 am
Powered by Dreamwidth Studios