Python implements the method of parsing the Bit Torrent seed file content,
This example describes how to parse the content of BitTorrent files in Python. We will share this with you for your reference. The details are as follows:
There are a lot of seed files. Sometimes I can't remember what is in them, and I don't want to drag and drop them to thunder or btsoftware one by one,
I checked the Python script online and modified it a little. The Code is as follows and pasted into the text editor:
Save it as a py suffix and run it directly.
Import redef tokenize (text, match = re. compile ("([idel]) | (/d +): | (-? /D + )"). match): I = 0 while I <len (text): m = match (text, I) s = m. group (m. lastindex) I = m. end () if m. lastindex = 2: yield "s" yield text [I: I + int (s)] I = I + int (s) else: yield sdef decode_item (next, token ): if token = "I": # integer: "I" value "e" data = int (next () if next ()! = "E": raise ValueError elif token = "s": # string: "s" value (virtual tokens) data = next () elif token = "l" or token = "d": # Container: "l" (or "d") values "e" data = [] tok = next () while tok! = "E": data. append (decode_item (next, tok) tok = next () if token = "d": data = dict (zip (data [0: 2], data [1:: 2]) else: raise ValueError return datadef decode (text): try: src = tokenize (text) data = decode_item (src. next, src. next () for token in src: # look for more tokens raise SyntaxError ("trailing junk") failed T (AttributeError, ValueError, StopIteration): raise SyntaxError ("syntax error ") return dataif _ name _ = "_ main _": # put the name of the file to be read here data = open ("The_Shawshank_Redemption.torrent", "rb "). read () torrent = decode (data) myfile = file ("testit.txt", 'w') a = U' file name '. encode ('gbk') B = u'file size '. encode ('gbk') print "% s/t % s/n" % (a, B) for file in torrent ["info"] ["files"]: print "% s/t % d Mb" % ("/". join (file ["path"]), file ["length"]/1024/1024) print "-----------------------------------------------------------------"
Be sure to save the file in Utf-8 format and do not save it in ASCII format; otherwise Chinese may be garbled or cannot be compiled
PS: here we recommend a related online tool for your reference:
Online Conversion Tool for BT seed torrent/magnetic link:
Http://tools.jb51.net/aideddesign/bt2mag