PHP Code
<?php
#!/usr/bin/env python
import time
import sys
import libtorrent
import requests
def download_file(url):
url = url[:len(url) - 1] if url.endswith('/') else url
req = requests.get(url, stream=True)
fname = url.split('/')[-1]
block = 1024
with open(fname, 'wb') as fd:
for chunk in req.iter_content(chunk_size=block):
if chunk:
fd.write(chunk)
fd.flush()
return fname
def download_torrent(torrent):
info_str = '\rsize: {0:.2f} mb | {1:.2f}% complete | eta: (h/m/s) {2} [down: {3:.2f} kb/s | up: {4:.2f} kb/s | peers: {5}] {6}'
states = ['queued', 'checking', 'downloading metadata', 'downloading',
'finished', 'seeding', 'allocating', 'checking fastresume']
session = libtorrent.session()
session.listen_on(6881, 6891)
info = libtorrent.torrent_info(torrent)
handle = session.add_torrent({'ti' : info, 'save_path' : './'})
print 'starting {0}\n'.format(handle.name())
while not handle.is_seed():
stat = handle.status()
size = stat.total_wanted / 1000000.0
progress = stat.progress * 100.0
down_speed = stat.download_rate / 1000.0
up_speed = stat.upload_rate / 1000.0
peers = stat.num_peers
state = '\033[1m' + states[stat.state] + '\033[0m'
seconds_left = (stat.total_wanted - stat.total_wanted_done) / stat.download_rate if stat.download_rate > 0 else 0
m, s = divmod(seconds_left, 60)
h, m = divmod(m, 60)
eta = '{0}.{1}.{2}'.format(h, m, s)
print info_str.format(size,
progress,
eta,
down_speed,
up_speed,
peers,
state),
sys.stdout.flush()
time.sleep(1)
print '{0} complete'.format(handle.name())
return handle.name()
if __name__ == '__main__':
if len(sys.argv) != 2:
print 'invalid arguments'
print 'usage: {0} [*.torrent | url]'.format(sys.argv[0])
sys.exit(1)
else:
to_down = sys.argv[1]
if to_down.startswith(r'http://') or to_down.startswith(r'https://'):
download_torrent(download_file(to_down))
else:
download_torrent(to_down)
Check out the pastebin version because the code formatting sucks ass: http://pastebin.com/JN8TuNFK
I may actually expand onto this as it seems like a fun project. For now this is as simple as it gets.
Just run the script with the torrent file or url as the first argument. Use linux for best results and make sure libtorrent and requests are installed.
Enjoy! 🤝


![[Image: vBKaZOL.gif]](https://i.imgur.com/vBKaZOL.gif)
