bt磁力种子与php文件,使用Python实现BT种子和磁力链接的相互转换
bt种子文件转换为磁力链接
BT种子文件相对磁力链来说存储不方便,而且在网站上存放BT文件容易引起版权纠纷,而磁力链相对来说则风险小一些。而且很多论坛或者网站限制了文件上传的类型,分享一个BT种子还需要改文件后缀或者压缩一次,其他人需要下载时候还要额外多一步下载种子的操作。
所以将BT种子转换为占用空间更小,分享更方便的磁力链还是有挺大好处的。
首先一个方案是使用bencode这个插件,通过pip方式安装或者自行下载源文件https://pypi.python.org/pypi/bencode/1.0通过python setup.py install方式安装均可。
相应的将BT种子转换为磁力链代码为:
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 [torrent file]
bt磁力种子与php文件,使用Python实现BT种子和磁力链接的相互转换相关推荐
- linux命令行下载BT种子和磁力链接
wget是linux下常用的命令行下载工具,是Linux用户是必不可少的工具,尤其对于网络管理员,经常要下载一些软件.而tget是一个简单的命令行BT下载工具,可以用于BT种子和磁力链接的下载. tg ...
- Python实现BT种子转化为磁力链接【实战】
经常看电影的朋友肯定对BT种子并不陌生,但是BT种子文件相对磁力链来说存储不方便,而且在网站上存放BT文件容易引起版权纠纷,而磁力链相对来说则风险小一些. 将BT种子转换为占用空间更小,分享更方便的磁 ...
- 用Python实现BT种子转化为磁力链接
经常看电影的朋友肯定对BT种子并不陌生,但是BT种子文件相对磁力链来说存储不方便,而且在网站上存放BT文件容易引起版权纠纷,而磁力链相对来说则风险小一些. 将BT种子转换为占用空间更小,分享更方便的磁 ...
- python编程入门指南磁力下载-Python实现BT种子转化为磁力链接【实战】
经常看电影的朋友肯定对BT种子并不陌生,但是BT种子文件相对磁力链来说存储不方便,而且在网站上存放BT文件容易引起版权纠纷,而磁力链相对来说则风险小一些. 将BT种子转换为占用空间更小,分享更方便的磁 ...
- python编程入门指南磁力下载-使用python 将bt转磁力链接
BT种子文件相对磁力链来说存储不方便,而且在网站上存放BT文件容易引起版权纠纷,而磁力链相对来说则风险小一些.而且很多论坛或者网站限制了文件上传的类型,分享一个BT种子还需要改文件后缀或者压缩一次,其 ...
- 用python实现bt下载_python实现bt种子 torrent转magnet
Python实现bt转磁链 参考前人资料主要两种方式 1,利用python的bencode模块 2,安装libtorrent模块 尝试过两种方法特记录 环境:Windows系统 python 3 be ...
- python实现bt下载器_使用Python编写基于DHT协议的BT资源爬虫
关于DHT协议 DHT协议作为BT协议的一个辅助,是非常好玩的.它主要是为了在BT正式下载时得到种子或者BT资源.传统的网络,需要一台中央服务器存放种子或者BT资源,不仅浪费服务器资源,还容易出现单点 ...
- Python Qt GUI设计:将UI文件转换为Python文件的三种妙招(基础篇—2)
目录 1.创建项目 2.将.ui文件生成.py文件 2.1.Eric 6编译 2.2.Python命令行编译 2.3.脚本编译 3.界面.逻辑分离思想 在开始本文之前提醒各位朋友,Python记得安装 ...
- python解压zip文件_python-29 python解压压缩包的几种方法
这里讨论使用Python解压例如以下五种压缩文件: .gz .tar .tgz .zip .rar 简单介绍 gz: 即gzip.通常仅仅能压缩一个文件.与tar结合起来就能够实现先打包,再压缩. t ...
- python运行文件后缀_Python程序存储成以.py为扩展名的程序文件用Python解释器执行。(4.5分)_学小易找答案...
[简答题]简述助跑的两种起动方式? [填空题]在跳远技术的发展过程中,曾经出现过 . . .等3中姿势. [判断题]一般情况下,Python语言中使用符号#表示程序中的注释.(4.5分) [论述题]请 ...
最新文章
- ipvsadm的几个参数输出的说明
- 这次终于不再为 iptables 犯迷糊了!
- linux vscode配置HBuilderX雅蓝主题
- Win32多线程编程(3) — 线程同步与通信
- sysAK(青囊)系统运维工具集:如何实现高效自动化运维?| 龙蜥技术
- boootstap-面包屑-下拉菜单
- matlab练习程序(共生矩阵)
- HTML5+Activex+Singr+ABP+MongoDB
- 医疗中的ai_医疗保健中自主AI的障碍
- 元器件-二极管、三极管
- 微信公众号H5 - 使用vue开发微信公众号网页
- could not find driver (SQL: select * from information_schema.tables where table_schema = oliver and
- python中度数怎么表示_python中如何将华氏温度转换为摄氏温度?
- 学计算机硬盘电脑多大好,笔记本电脑固态硬盘要多大的合适
- 计算机组成原理课程设计(很全面有保障)
- Linux手动安装和部署github
- 分布式系统学习共性总结:
- 08---HTML+CSS---float浮动
- 洛谷P1339 Heat Wave G(最短路,图论)
- Java Swing 邮箱管理系统(实现注册/登录、收/发邮件)
热门文章
- 个人计算机好用的pdf软件,win10好用的pdf阅读器推荐 推荐几款好用的pdf阅读器
- 可发弹幕php,JavaScript直播评论发弹幕切图功能点集合效果代码
- Java实战项目,附带源码+视频教程。
- 小程序技术能提升桌面应用安全等级?
- spring揭秘_「死磕 Spring」—– IOC 之深入理解 Spring IoC
- 软件架构--《企业IT架构转型之道》笔记
- 端口扫描工具zmap使用笔记
- 用友v11服务器的共享文件,用友U8系列财务及供应链一体化操作手册u8V11.1(标准)版.docx...
- 五种酷炫代码雨的源代码
- 【Arduino实验17 L298N 电机驱动模块】