python下载bt文件_python通过info下载BT种子并分析文件保存到数据库

1.[代码][Python]代码

# _*_ coding: utf-8 _*_

'''

https://zoink.it

'''

import urllib,urllib2,os,MySQLdb,gzip

from io import BytesIO

from btdht import Parser

def save(filename, content):

try:

file = open(filename, 'wb')

file.write(content)

file.close()

except IOError,e:

print e

def getTorrents(info_hash):

url="https://zoink.it/torrent/%s.torrent"%info_hash.upper()

#url="http://torrage.com/torrent/%s.torrent"%info_hash.upper()

#print url

try:

torrent=urllib2.urlopen(url,timeout=30)

buffer = BytesIO(torrent.read())

gz = gzip.GzipFile(fileobj=buffer)

raw_data=gz.read()

save(".\\torrents\\"+info_hash+".torrent",raw_data)

except IOError,e:

print e

#print "downloading+"+info_hash+".torrent failed"

return False

#print "downloading+"+info_hash+".torrent success"

return True

def getAllTorrents(table):

try:

os.mkdir("torrents")

except WindowsError,e:

print e

try:

conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='456',port=3306,charset="UTF8")

cur=conn.cursor()

conn.select_db('dht')

sql="select * from "+table

count=cur.execute(sql)

print "thers are %s row in table"% count

result=cur.fetchall()

i=0

for r in result:

#the torrent info is empty

if r[1]!="":

print 'torrent exist'

continue

#download the torrent file success

state=getTorrents(r[0])

if state:

#count the torrent file

i=i+1

try:

parser=Parser.Parser(".\\torrents\\"+r[0]+".torrent")

except :

print 'bt file error'

name=parser.getName()

print '============================================================='

print r[0]

#print name

#update the tuple

#print r[0]

encoding=parser.getEncoding()

comment=parser.getComments()

#print comment.decode('utf-8')

try:

if encoding:

print name

print encoding

print name.decode(encoding)+" "+encoding

sql="update hash_info set hash_info.info='%s' where hash_info.hash='%s'"%(name.decode(encoding),r[0].decode('utf-8'))

#print sql

cur.execute(sql)

else:

print name

print name.decode('utf-8')+" "+encoding

sql="update hash_info set hash_info.info='%s' where hash_info.hash='%s'"%(name.decode('utf-8'),r[0].decode('utf-8'))

#print sql

cur.execute(sql)

conn.commit()

except :

print 'mysql or decode error'

else:

try:

#torrent file download failed

sql="update hash_info set hash_info.info='%s' where hash_info.hash='%s'"%("error",r[0].decode('utf-8'))

cur.execute(sql)

conn.commit()

except:

print 'error'

cur.close()

conn.close()

except MySQLdb.Error,e:

print 'mysql error %d:%s'%(e.args[0],e.args[1])

print 'the torrent files :'+str(i)

if __name__=="__main__":

getAllTorrents("hash_info")

'''

info_hash="5302C30A88347F10E1F0A5BF334A8AC85D545AC0"

getTorrents(info_hash)

'''

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值