import bencode, hashlib, base64, urllib
torrent = open('ubuntu-12.04.2-server-amd64.iso.torrent', 'rb').read()
metadata = bencode.bdecode(torrent)
hashcontents = bencode.bencode(metadata['info'])
digest = hashlib.sha1(hashcontents).digest()
b32hash = base64.b32encode(digest)
params = {'xt': 'urn:btih:%s' % b32hash,
'dn': metadata['info']['name'],
'tr': metadata['announce'],
'xl': metadata['info']['length']}
paramstr = urllib.urlencode(params)
magneturi = 'magnet:?%s' % paramstr
print magneturi
还有另外一个效率相对较高,而且更方便的方案是安装libtorrent,在ubuntu只需要apt-get install python-libtorrent即可对应转换磁力链的代码为:
import libtorrent as bt
info = bt.torrent_info('test.torrent')
print "magnet:?xt=urn:btih:%s&dn=%s" % (info.info_hash(), info.name())
转换磁力链接为bt种子文件
下面来看一个反过程,将磁力链转化为种子文件。
1、需要先安装python-libtorrent包 ,在ubuntu环境下,可以通过以下指令完成安装:
# sudo apt-get install python-libtorrent
2、代码如下:
#!/usr/bin/env python
import shutil
import tempfile
import os.path as pt
import sys
import libtorrent as lt
from time import sleep
def magnet2torrent(magnet, output_name=None):
if output_name and \
not pt.isdir(output_name) and \
not pt.isdir(pt.dirname(pt.abspath(output_name))):
print("Invalid output folder: " + pt.dirname(pt.abspath(output_name)))
print("")
sys.exit(0)
tempdir = tempfile.mkdtemp()
ses = lt.session()
params = {
'save_path': tempdir,
'duplicate_is_error': True,
'storage_mode': lt.storage_mode_t(2),
'paused': False,
'auto_managed': True,
'duplicate_is_error': True
}
handle = lt.add_magnet_uri(ses, magnet, params)
print("Downloading Metadata (this may take a while)")
while (not handle.has_metadata()):
try:
sleep(1)
except KeyboardInterrupt:
print("Aborting...")
ses.pause()
print("Cleanup dir " + tempdir)
shutil.rmtree(tempdir)
sys.exit(0)
ses.pause()
print("Done")
torinfo = handle.get_torrent_info()
torfile = lt.create_torrent(torinfo)
output = pt.abspath(torinfo.name() + ".torrent")
if output_name:
if pt.isdir(output_name):
output = pt.abspath(pt.join(
output_name, torinfo.name() + ".torrent"))
elif pt.isdir(pt.dirname(pt.abspath(output_name))):
output = pt.abspath(output_name)
print("Saving torrent file here : " + output + " ...")
torcontent = lt.bencode(torfile.generate())
f = open(output, "wb")
f.write(lt.bencode(torfile.generate()))
f.close()
print("Saved! Cleaning up dir: " + tempdir)
ses.remove_torrent(handle)
shutil.rmtree(tempdir)
return output
def showHelp():
print("")
print("USAGE: " + pt.basename(sys.argv[0]) + " MAGNET [OUTPUT]")
print(" MAGNET\t- the magnet url")
print(" OUTPUT\t- the output torrent file name")
print("")
def main():
if len(sys.argv) <2:
showHelp()
sys.exit(0)
magnet = sys.argv[1]
output_name = None
if len(sys.argv) >= 3:
output_name = sys.argv[2]
magnet2torrent(magnet, output_name)
if __name__ == "__main__":
main()
3、用法如下
# python Magnet_To_Torrent2.py <magnet link>[torrent file]
pythonista可以说时iOS端功能最强的自动化软件了优点:安装stash之后可以通过pip自己装想用的包,但是要做好无法安装的情况学py的基础语法或者对性能要求较低的文本信息的处理还可以,图片、视频就别多想了可以用来写wedgit,我给手机的负一屏写了一个用来查看我知乎点赞,收藏,感谢的小控件,还有很多功能可以开发的可以写系统分享菜单的功能有超棒的差不多可以媲美VS的自动补全我写过的常用程序有选中的文本是同学学号的话,运行我写的脚本,可以查看我这个同学校园卡的照片选中的如果是磁力链(没有磁力头的那种)可以自动加上磁力头上面提到的wedgit处理我通讯录的脚本,我手机系统是英文,中文通讯录所有联系人都会被归于#一类,给联系方式的名称加上拼音就可以按照姓氏拼音排序了查看我图书馆座位预约情况,如果没签到,顺便帮我签个到缺点:受iPad性能所限(别以为iPad的性能已经够高了,在pc面前还是不够看)其实你想要写的大部分耗性能(特别是内存)的程序都是有一定几率卡死的wedgit对内存有限制,可能你写的时候可以跑,但实际用的时候就会报错,这好像是由于系统对wedgit的限制导致的