主页 > 编程资料 > Python >
发布时间:2018-06-02 作者:apizl 阅读:323次

有时候我们不想打开迅雷或者其他软件查看种子种包含文件时候,如下代码就能胜任。

编码需要自动转换

pytorrent 项目:https://github.com/ndroi/pytorrent

python解析种子信息获取种子创建时间文件列表等等数据pytorrent-master.zip

#! -*-encoding:utf8 -*-
import pytorrent,json
def getCoding(strInput):
    '''
    获取编码格式
    '''
    if isinstance(strInput, unicode):
        return "unicode"
    try:
        strInput.decode("utf8")
        return 'utf8'
    except:
        pass
    try:
        strInput.decode("gbk")
        return 'gbk'
    except:
        pass
def tran2UTF8(strInput):
    '''
    转化为utf8格式
    '''
    strCodingFmt = getCoding(strInput)
    if strCodingFmt == "utf8":
        return strInput
    elif strCodingFmt == "unicode":
        return strInput.encode("utf8")
    elif strCodingFmt == "gbk":
        return strInput.decode("gbk").encode("utf8")
def tran2GBK(strInput):
    '''
    转化为gbk格式
    '''
    strCodingFmt = getCoding(strInput)
    if strCodingFmt == "gbk":
        return strInput
    elif strCodingFmt == "unicode":
        return strInput.encode("gbk")
    elif strCodingFmt == "utf8":
        return strInput.decode("utf8").encode("gbk")
t = pytorrent.Torrent()
t.load("test.torrent")  # your torrent file
name= tran2UTF8(t.data["info"]["name"])
date= t.data['creation date']  ##创建种子时间
announce= t.data['announce']  ##服务器
# print t.data
files = t.data["info"]["files"]
filesList=[]
for item in files:
    listText = ''
    for val in item['path']:
        listText = listText + ' ' + tran2UTF8(val)
    filesList.append(listText+" - " + str(item['length'] / 1024 / 1024)+"MB")
# t.data["info"]["name"]="my_name" #change info. some software may read ["info"]["name.utf-8"]
# t.dump("dump.torrent")  #the new torrent file
data={'name':name,'filesList':filesList,'date':date,'announce':announce}
print json.dumps(data)


文章由爱资料原创本文地址:https://www.apizl.com/archives/view-134212-1.html,转载请以链接形式标明本文地址!
关键字词: