Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Basic torrent client

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! 🤝
Can you continue your project on github?
Thanks for this, its pretty good!
[Image: vBKaZOL.gif]
(Aug 14, 2015 06:26 AM)Long Morrison Wrote:  Can you continue your project on github?
Thanks for this, its pretty good!

I was actually thinking about doing so. I'll see sometime and maybe put it up on github.
Thanks for the feedback! 😁
This is very cool Introvert, thanks for this, love learning from your source code as always!
(Aug 14, 2015 11:08 AM)dopepizza Wrote:  This is very cool Introvert, thanks for this, love learning from your source code as always!

No problem and thanks for all the kind words you leave on my threads. It really motivates me to keep coding. 😁


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by gigajewFeb 3, 2018 07:31 PM 0 296 Feb 3, 2018 07:31 PM
Last Post: gigajew
[.NET] by Asa ButterfieldJan 18, 2016 11:22 PM 3 383 Jan 18, 2016 11:22 PM
Last Post: RiperFish
[.NET] by DecimJan 15, 2016 02:09 PM 14 1,447 Jan 15, 2016 02:09 PM
Last Post: Forecraft