zoukankan      html  css  js  c++  java
  • 【Python】torrentParser1.00

    代码:

    #------------------------------------------------------------------------------------
    # torrentParser1.00,用于解析torrent文件
    # 2018年5月8日
    #------------------------------------------------------------------------------------
    from bencode import bdecode
    import time
    
    #-------------------------------------
    # torrentParser类
    #-------------------------------------
    class torrentParser(object):
        # 构造函数
        def __init__(self,filePathname):
            self.filePathname=filePathname
            with open(filePathname,'rb') as fObj:
                self.fileDic=bdecode(fObj.read()) 
        
        # 得到所有键值
        def getKeys(self):  
            return self.fileDic.keys()
    
        # 获得tracker服务器的URL
        def getAnnounce(self):  
            if 'announce' in self.fileDic:  
                return self.fileDic['announce'].decode('utf-8') 
            return ''
    
        # 获得tracker服务器的URL列表
        def getAnnounceList(self):
            retval=[]
    
            if 'announce-list' in self.fileDic:  
                arr=self.fileDic['announce-list']
                
                for childArr in arr:
                    for item in childArr:
                        retval.append(item.decode('utf-8'))
    
            return retval
    
        # 得到制作日期
        def getCreateTime(self):  
            if 'creation date' in self.fileDic: 
                unixTimestamp=self.fileDic['creation date']
                firmalTime = time.localtime(unixTimestamp)        
                dt = time.strftime('%Y-%m-%d %H:%M:%S', firmalTime)
    
                return dt  
            else:  
                return ''
    
        # 获得编码方式  
        def getEncoding(self):  
            if 'encoding' in self.fileDic:  
                return self.fileDic['encoding'].decode('utf-8') #去掉字符串前面的b'
            return ''
        
        # 是否包含多个文件
        def hasMultiFiles(self):  
            if 'files' in self.fileDic['info']:  
                return True  
            else:  
                return False
        
        # 获得文件名  
        def getTitle(self): 
            filename=''
            info = self.fileDic['info'] 
            
            if 'name.utf-8' in info:  
                filename=info['name.utf-8']  
            else:  
                filename = info['name']  
            
            return filename.decode('utf-8') # x 开头编码的数据解码成中文
    
        # 获得备注(可选项)
        def getComment(self):  
            if 'comment' in self.fileDic:  
                return self.fileDic['comment'].decode('utf-8') 
            return ''
    
        # 获得创建者(可选项)
        def getCreatedBy(self):  
            if 'created by' in self.fileDic:  
                return self.fileDic['created by'].decode('utf-8') 
            return ''
    
        # 多文件的情况下,得到多个文件的个数
        def getFileCount(self):  
            return len(self.fileDic['info']['files'])
        
        # 多文件的情况下,获得所有文件,返回为:dic   
        def getFiles(self): 
            files=[]
        
            for item in self.fileDic['info']['files']:
                file={}
    
                for key in item.keys():
                    value=item.get(key)
                    #value=value.decode('utf-8');
                    file[key]=value
                
                files.append(file)
    
            return files
    
        # 单文件情况下,取文件名
        def getSingleFileName(self):
            return str(self.fileDic['info']['name'],'utf-8')
    
        # 单文件情况下,取文件长度
        def getSingleFileLength(self):
            return self.fileDic['info']['length']
        
        # 单文件情况下,取文件md5sum
        def getSingleFileMd5sum(self):
            if 'md5sum' in self.fileDic['info']:
                return str(self.fileDic['info']['md5sum'],'utf-8')
            else:
                return ''
        
        # 单文件情况下,取文件长度
        def getSingleFilePieceLength(self):
            return self.fileDic['info']['piece length']
        
        # 单文件情况下,取文件pieces
        def getSingleFilePieces(self):
            return self.fileDic['info']['pieces']
    
        # 得到文件简报
        def getBrief(self):
            retval=""
            retval=retval+"File:"+self.filePathname+"
    "
            retval=retval+"announce:"+self.getAnnounce()+"
    "
    
            arr=self.getAnnounceList()
            if(len(arr)>0):
                retval=retval+"announce list:"+"
    "
    
                for it in arr:
                    retval=retval+"	"+it+"
    "    
            
            retval=retval+"Create time:"+self.getCreateTime()+"
    "
            retval=retval+"Ecoding:"+self.getEncoding()+"
    "
            retval=retval+"Title:"+self.getTitle()+"
    "
            retval=retval+"Comment:"+self.getComment()+"
    "
            retval=retval+"Created by:"+self.getCreatedBy()+"
    "
    
            hasMulti=self.hasMultiFiles()
            retval=retval+"has multi files:"+str(hasMulti)+"
    "
            if hasMulti==True:
                retval=retval+"[多文件结构]"+"
    "
    
                retval=retval+"包含文件个数为:"+str(self.getFileCount())+"
    "
                retval=retval+"Files:"+"
    "
    
                files=self.getFiles();
                index=1
                for item in files:
                    #retval=retval+"	"+str(item)+"
    "    
                    retval=retval+"	file#"+str(index)+"
    "    
                    for key in item.keys():
                        value=item.get(key)
                        
                        #if isinstance(value,bytes):
                        retval=retval+"		"+str(key)+":"+str(value)+"
    " # 这里转化有问题
                    retval=retval+"
    "    
    
                    index=index+1
            else:
                retval=retval+"[单文件结构]"+"
    "
                retval=retval+"文件名为:"+self.getSingleFileName()+"
    "
                retval=retval+"文件长度:"+str(self.getSingleFileLength())+"byte
    "
                retval=retval+"文件md5sum:"+self.getSingleFileMd5sum()+"
    "
                retval=retval+"文件块长度:"+str(self.getSingleFilePieceLength())+"byte
    "
                #retval=retval+"文件Pieces:"+str(self.getSingleFilePieces())+"
    "
    
    
    
            return retval
    
    def main():
        tp=torrentParser(filePathname='./1.torrent')
        print(tp.getBrief());
    
    # Start
    main()

    输出:

    C:Usershorn1Desktoppython38-torrentParser>python torrentParser.py
    File:./1.torrent
    announce:udp://fr33domtracker.h33t.com:3310/announce
    announce list:
            udp://fr33domtracker.h33t.com:3310/announce
            http://fr33dom.h33t.com:3310/announce
            udp://tracker.publichd.eu/announce
            http://tracker.publichd.eu/announce
            http://tracker.publicbt.com:80/announce
            http://exodus.desync.com:6969/announce
            http://exodus.desync.com/announce
            http://tracker.prq.to:6969/announce
            http://ipv4.tracker.harry.lu/announce
            udp://tracker.1337x.org:80/announce
            udp://tracker.openbittorrent.com:80/announce
            udp://tracker.ccc.de:80
            udp://tracker.istole.it:80/announce
            udp://10.rarbg.com/announce
    Create time:2012-08-27 19:07:23
    Ecoding:UTF-8
    Title:[dydao.com]The.Mask.of.Love.2012.HDTV.720p.x264.AC3-HD.mkv
    Comment:www.dydao.com
    Created by:uTorrent/3130
    has multi files:False
    [单文件结构]
    文件名为:[dydao.com]The.Mask.of.Love.2012.HDTV.720p.x264.AC3-HD.mkv
    文件长度:4695257841byte
    文件md5sum:
    文件块长度:8388608byte

    2018年5月8日

  • 相关阅读:
    【翻译】CEDCE2010 制作魅力绘制而要知道的光学小知识
    Guilty Gear Xrd 资源Rip(1)
    使用 Intel GPA 与 分析3D程序和抓取模型
    【翻译】西川善司的「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,后篇
    【翻译】西川善司「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,前篇(2)
    【翻译】西川善司「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,前篇(1)
    【翻译】CEDEC2014跨世代多平台并行开发PS4版如龙维新开发的一年
    【翻译】CEDEC2014[跨越我的尸体2]跨越Stylized Rendering
    【翻译】CEDEC2012 SQUARE ENIX GPGPU实现高速GI烘培工具的方法
    Bootstrap4(15): 下拉菜单
  • 原文地址:https://www.cnblogs.com/heyang78/p/9010163.html
Copyright © 2011-2022 走看看