python获取bt种子的详细信息
- - CSDN博客编程语言推荐文章bt种子具体文件结构如下: . 全部内容必须都为bencoding编码类型. 整个文件为一个字典结构,包含如下关键字 . announce:tracker服务器的URL(字符串) . announce-list(可选):备用tracker服务器列表(列表) . creation date(可选):种子创建的时间,Unix标准时间格式,从1970 1月1日 00:00:00到创建时间的秒数(整数) .
pieces:同单文件结构中相同
知道了这些就很好解析了
Parser.py
# -*- coding: utf-8 -*- from bencode import bdecode class Parser(object): def __init__(self,filePath): self.path = filePath metainfo_file = open(str(self.path), 'rb') self.metainfo = bdecode(metainfo_file.read()) metainfo_file.close() def getStruct(self): print self.metainfo.keys() #如果是单文件就返回:0 #如果是多文件就返回:1 def checkType(self): if 'files' in self.metainfo['info']: return 1 else: return 0 def getCreationDate(self): if 'creation date' in self.metainfo: return self.metainfo['creation date'] else: return '' def getInfo(self): return self.metainfo['info'].keys() #获得文件名 def getName(self): info = self.metainfo['info'] if 'name.utf-8' in info: filename=info['name.utf-8'] else: filename = info['name'] for c in filename: if c=="'": filename=filename.replace(c,"\\\'") return filename #多文件的情况下,获得所有文件,返回为:dic def getInfoFiles(self): return self.metainfo['info']['files'] #返回创建时间 def getCreatedBy(self): if 'created by' in self.metainfo: return self.metainfo['created by'] else: return '' #获得编码方式 def getEncoding(self): if 'encoding' in self.metainfo: return self.metainfo['encoding'] return "" def getComments(self): info = self.metainfo['info'] if 'comment.utf-8' in self.metainfo: comment=self.metainfo['comment.utf-8'] return comment else: return ''test.py
#-*- coding:utf-8 -*- import Parser import time if __name__ == "__main__": parser=Parser.Parser('test1.torrent') print parser.getStruct() print parser.getCreationDate() print 'creation date:'+time.strftime('%Y-%m-%d',time.localtime(parser.getCreationDate())) print 'comments:'+parser.getComments() print 'name:'+parser.getName() print 'encoding:'+parser.getEncoding() print 'created by:'+parser.getCreatedBy() print parser.getInfo() print parser.getInfoFiles() print parser.checkType() print parser.getSize()