Skip to content

Instantly share code, notes, and snippets.

@Chairo
Created August 28, 2013 14:48
Show Gist options
  • Save Chairo/6366846 to your computer and use it in GitHub Desktop.
Save Chairo/6366846 to your computer and use it in GitHub Desktop.
Python搭建BT资源搜索站(四)
# -*- coding:utf-8 -*-
import libtorrent as lt, hashlib
from pymongo import Connection
def file_path(info_hash):
'''get new file path and name'''
_md5 = hashlib.md5(info_hash).hexdigest()
return {'path':'%s/%s/'%(_md5[0:2], _md5[2:5]), 'file_name':'%s.torrent'%_md5}
#解析BT文件并更新对应信息到Mongo数据库
def update(info_hash, db):
_temp = file_path(info_hash)
_path = ('%s/%s'%('torrents', _temp['path']))
_file_name = _temp['file_name']
_file = '%s%s'%(_path, _file_name)
e = lt.bdecode(open(_file, 'rb').read())
try:
info = lt.torrent_info(e) #获取BT文件info内容
_files = []
if not e.get('info').get('files'): #BT文件中只包含一个文件的情况
_files.append({'path':info.name(),'length':e.get('info').get('length')})
else:
for _f in e.get('info').get('files'):
_files.append({'path':_f.get('path.utf-8', _f.get('path')), 'length':_f.get('length')})
db.info_hash.update({"info_hash":info_hash}, {"analyze":1, "file":_file, "name":info.name(), "files":_files}) #保存BT种子中文件列表、文件名称信息到Mongo,并标识该info_hash为已分析
except Exception, what:
print what, info_hash
db.info_hash.update({"info_hash":info_hash}, {"analyze":1, "error":1})
pass
#连接Mongo
mongo = Connection(host='127.0.0.1', port=27017)
db = mongo.bt #连接bt这个数据库,没有会自动新建
_files = db.info_hash.find({"analyze": {"$exists": False}, "download":1}) #查询所有已经下载BT种子但没有分析过的数据
if _files.count()>0:
for item in _files:
update(item.get('info_hash'), db)
else:
print 'no files '
@laohur
Copy link

laohur commented Apr 12, 2014

如果在纯ipv6环境下,可以只用磁力链接么?
正准备建一个pt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment