Python implements the method of parsing the Bit Torrent seed file content,

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.