Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to control torrent download in python using libtorrent

Faheem KurikkalFaheem Kurikkal Member Posts: 1

import libtorrent as lt
import time
import sys
def get_libtorrent_session_and_start_dht(start_port, end_port):
ses = lt.session()
ses.listen_on(start_port, end_port)
ses.add_dht_router('dht.transmissionbt.com', start_port)
ses.add_dht_router('router.bittorrent.com', start_port)
ses.add_dht_router('router.utorrent.com', start_port)
ses.start_dht()
return ses

p.terminate()

const_state_str = ['queued', 'checking', 'downloading metadata', \
'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
const_pause_torrent = "pause_torrent"
const_resume_torrent = "resume_torrent"
const_kill_torrent = "kill_torrent"
const_user_logged_in = "user_logged_in"
const_user_logged_out = "user_logged_out"
const_quit_seeding = "quit_seeding"

ses = get_libtorrent_session_and_start_dht(6881, 6891)
torrent_info = None
torrent_handle = None
params = {
'save_path': '/home/faheem/Downloads/',
'storage_mode': lt.storage_mode_t(2)
}
link = "magneturl"
try:
#torrent_info = lt.torrent_info("/home/faheem/Downloads/abcd.torrent")
torrent_handle = lt.add_magnet_uri(ses,link,params)
# ses.add_torrent({'ti': torrent_info, 'save_path': "/home/faheem/"})
except Exception as exception:
raise exception
print "Getting meta data"
while not torrent_handle.has_metadata():
time.sleep(1)
print 'got metadata, starting torrent download...'

current_iteration = 0
while not torrent_handle.is_seed():
torrent_status = torrent_handle.status()

print "current_iteration: " + str(current_iteration)

if current_iteration == 10:
    print "Calling torrent_handle.pause()...Pausing Torrent"
    sys.stdout.flush()
    torrent_handle.pause()
    while not torrent_handle.status().paused:
        print "Torrent Is Not Paused Yet"
        time.sleep(1)
        sys.stdout.flush()
    while torrent_handle.status().paused:
        print "Torrent Is Paused!"
        torrent_handle.pause()
        time.sleep(20)
        sys.stdout.flush()

if const_state_str[torrent_status.state] == "checking":
    print 'Checking Torrent....'
    continue

print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
      (torrent_status.progress * 100, torrent_status.download_rate / 1000, torrent_status.upload_rate / 1000, \
       torrent_status.num_peers, const_state_str[torrent_status.state]), \
    sys.stdout.flush()

current_iteration += 1

if torrent_status.paused:
    print "Is Paused"
else:
    print "Is Not Paused"

time.sleep(1)

In this code, I would like to implement pause,resume and queuing. Please help... URGENT

Tagged:
Sign In or Register to comment.