Faheem Mohammed
2017-02-22 13:16:16 UTC
I am trying to build a small torrent downloading python application with
libtorrent. Until now I have received some codes which downloads the
torrent and displays progress details. I would like to add features like
'pausing, 'resuming', 'deleting', 'queuing' and 'multiple downloads'.
I tried some, but not so familiar with python programming. Current code is
as follows,
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)
Here there at iteration 10, it has to pause but not working.
Please look through the code and produce a detailed code for enabling
pausing, resuming, queuing and multiple downloads.
I am beginner in python so kindly produce codes for the above requirements.
I think multiprocessing is needed for multiple downloads. Please check it
It is URGENT..
REGARDS
libtorrent. Until now I have received some codes which downloads the
torrent and displays progress details. I would like to add features like
'pausing, 'resuming', 'deleting', 'queuing' and 'multiple downloads'.
I tried some, but not so familiar with python programming. Current code is
as follows,
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)
Here there at iteration 10, it has to pause but not working.
Please look through the code and produce a detailed code for enabling
pausing, resuming, queuing and multiple downloads.
I am beginner in python so kindly produce codes for the above requirements.
I think multiprocessing is needed for multiple downloads. Please check it
It is URGENT..
REGARDS