有时候我们不想打开迅雷或者其他软件查看种子种包含文件时候,如下代码就能胜任。
编码需要自动转换
pytorrent 项目:https://github.com/ndroi/pytorrent
#! -*-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)
关键字词:
相关文章
- python 经常报错'module' object has no attribute 'X509_up_ref'
- python安装cv2 无法安装
- Python来识别字符串所属语言类型(langid 、langdetect)
- Python3 字典的复制与修改
- Python3 print 不换行打印教程
- Python3 bytes to string 字节码转字符串
- Scrapy Proxy Python下爬虫使用代理
- Scrapy 爬虫入门 内建设置参考 Python爬虫教程实战
- Scrapy 爬虫入门 Items 与 Item Pipeline Python爬虫教程实战
- Scrapy 爬虫入门 Spider 与 Selectors (选择器) Python爬虫教程实战

