This commit is contained in:
parent
c88442abe4
commit
385b808dc1
|
@ -30,6 +30,10 @@ import pickle
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
# Constants
|
||||||
|
|
||||||
|
TORRENTS_SUBDIR = "torrentfiles"
|
||||||
|
|
||||||
# Information for a single torrent
|
# Information for a single torrent
|
||||||
|
|
||||||
class torrent:
|
class torrent:
|
||||||
|
@ -65,11 +69,11 @@ class persistent_state:
|
||||||
self.max_connections = 80
|
self.max_connections = 80
|
||||||
|
|
||||||
self.use_DHT = True
|
self.use_DHT = True
|
||||||
self.base_dir = "~/Temp"
|
|
||||||
self.torrents_subdir + "torrentfiles"
|
|
||||||
self.max_active_torrents = 1
|
self.max_active_torrents = 1
|
||||||
self.auto_seed_ratio = -1
|
self.auto_seed_ratio = -1
|
||||||
|
|
||||||
|
self.temp = 0
|
||||||
|
|
||||||
# Prepare queue (queue is pickled, just like everything else)
|
# Prepare queue (queue is pickled, just like everything else)
|
||||||
self.queue = [] # queue[x] is the unique_ID of the x-th queue position. Simple.
|
self.queue = [] # queue[x] is the unique_ID of the x-th queue position. Simple.
|
||||||
|
|
||||||
|
@ -80,12 +84,13 @@ class persistent_state:
|
||||||
# The manager for the torrent system
|
# The manager for the torrent system
|
||||||
|
|
||||||
class manager:
|
class manager:
|
||||||
def __init__(self, client_ID, version, user_agent, state_filename):
|
def __init__(self, client_ID, version, user_agent, base_dir, state_filename):
|
||||||
self.state_filename = state_filename
|
self.state_filename = state_filename
|
||||||
|
self.base_dir = base_dir
|
||||||
|
|
||||||
# Ensure directories exist
|
# Ensure directories exist
|
||||||
if not self.torrents_subdir in os.listdir(self.base_dir):
|
if not TORRENTS_SUBDIR in os.listdir(self.base_dir):
|
||||||
os.mkdir(self.base_dir + "/" + self.torrents_subdir)
|
os.mkdir(self.base_dir + "/" + TORRENTS_SUBDIR)
|
||||||
|
|
||||||
# Start up the core
|
# Start up the core
|
||||||
assert(len(version) == 4)
|
assert(len(version) == 4)
|
||||||
|
@ -101,8 +106,8 @@ class manager:
|
||||||
|
|
||||||
# Unpickle the state, or create a new one
|
# Unpickle the state, or create a new one
|
||||||
try:
|
try:
|
||||||
pkl_file = open(state_filename, 'rb')
|
pkl_file = open(self.base_dir + "/" + self.state_filename, 'rb')
|
||||||
self.state = pickle.load(pkl_file)
|
self.state = pickle.load(pkl_file) #xxx LOCALIZE to base_dir!
|
||||||
pkl_file.close()
|
pkl_file.close()
|
||||||
|
|
||||||
# Sync with the core: tell core about torrents, and get unique_IDs
|
# Sync with the core: tell core about torrents, and get unique_IDs
|
||||||
|
@ -112,7 +117,7 @@ class manager:
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
# Pickle the state
|
# Pickle the state
|
||||||
output = open(self.state_filename, 'wb')
|
output = open(self.base_dir + "/" + self.state_filename, 'wb')
|
||||||
pickle.dump(self.state, output)
|
pickle.dump(self.state, output)
|
||||||
output.close()
|
output.close()
|
||||||
|
|
||||||
|
@ -120,7 +125,7 @@ class manager:
|
||||||
self.save_fastresume_data()
|
self.save_fastresume_data()
|
||||||
|
|
||||||
# Shutdown torrent core
|
# Shutdown torrent core
|
||||||
pytorrent.quit()
|
pytorrent_core.quit()
|
||||||
|
|
||||||
def add_torrent(self, filename, save_dir, compact):
|
def add_torrent(self, filename, save_dir, compact):
|
||||||
print "add_torrent"
|
print "add_torrent"
|
||||||
|
@ -198,7 +203,7 @@ class manager:
|
||||||
return self.unique_IDs[unique_ID].user_paused
|
return self.unique_IDs[unique_ID].user_paused
|
||||||
|
|
||||||
def is_paused(self, unique_ID):
|
def is_paused(self, unique_ID):
|
||||||
return pytorrent_core.is_paused(unique_ID])
|
return pytorrent_core.is_paused(unique_ID)
|
||||||
|
|
||||||
# Enforce the queue: pause/unpause as needed, based on queue and user_pausing
|
# Enforce the queue: pause/unpause as needed, based on queue and user_pausing
|
||||||
# This should be called after changes to relevant parameters (user_pausing, or
|
# This should be called after changes to relevant parameters (user_pausing, or
|
||||||
|
@ -218,16 +223,16 @@ class manager:
|
||||||
# Pause and resume torrents
|
# Pause and resume torrents
|
||||||
for index in range(len(self.state.queue)):
|
for index in range(len(self.state.queue)):
|
||||||
unique_ID = self.state.queue[index]
|
unique_ID = self.state.queue[index]
|
||||||
if (index < self.state.max_active_torrents or self.state_max_active_torrents == -1)
|
if (index < self.state.max_active_torrents or self.state_max_active_torrents == -1) \
|
||||||
and self.is_paused(unique_ID)
|
and self.is_paused(unique_ID) \
|
||||||
and not self.is_user_paused(unique_ID):
|
and not self.is_user_paused(unique_ID):
|
||||||
pytorrent_core.resume(unique_ID)
|
pytorrent_core.resume(unique_ID)
|
||||||
elif not self.is_paused(unique_ID) and
|
elif not self.is_paused(unique_ID) and \
|
||||||
(index >= self.state.max_active_torrents or self.is_user_paused(unique_ID)):
|
(index >= self.state.max_active_torrents or self.is_user_paused(unique_ID)):
|
||||||
pytorrent_core.pause(unique_ID)
|
pytorrent_core.pause(unique_ID)
|
||||||
|
|
||||||
def calc_ratio(self, unique_ID, torrent_state):
|
def calc_ratio(self, unique_ID, torrent_state):
|
||||||
up = float(torrent_state['total_upload'] + self.unique_IDs[unique_ID].uploaded_memory
|
up = float(torrent_state['total_upload'] + self.unique_IDs[unique_ID].uploaded_memory)
|
||||||
down = float(torrent_state["total_done"])
|
down = float(torrent_state["total_done"])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -250,9 +255,9 @@ class manager:
|
||||||
|
|
||||||
time.sleep(0.01) # Ensure we use a unique time for the new filename
|
time.sleep(0.01) # Ensure we use a unique time for the new filename
|
||||||
new_name = str(time.time()) + ".torrent"
|
new_name = str(time.time()) + ".torrent"
|
||||||
full_new_name = self.state.base_dir + "/" + self.torrents_subdir + newName
|
full_new_name = self.base_dir + "/" + TORRENTS_SUBDIR + "/" + new_name
|
||||||
|
|
||||||
if new_name in os.listdir(self.state.base_dir + "/" + self.torrents_subdir):
|
if new_name in os.listdir(self.base_dir + "/" + TORRENTS_SUBDIR):
|
||||||
raise PyTorrentCoreError("Could not cache torrent file locally, failed: " + new_name)
|
raise PyTorrentCoreError("Could not cache torrent file locally, failed: " + new_name)
|
||||||
|
|
||||||
shutil.copy(filename, full_new_name)
|
shutil.copy(filename, full_new_name)
|
||||||
|
|
|
@ -368,6 +368,8 @@ static PyObject *torrent_save_fastresume(PyObject *self, PyObject *args)
|
||||||
out.unsetf(std::ios_base::skipws);
|
out.unsetf(std::ios_base::skipws);
|
||||||
|
|
||||||
bencode(std::ostream_iterator<char>(out), data);
|
bencode(std::ostream_iterator<char>(out), data);
|
||||||
|
|
||||||
|
Py_INCREF(Py_None); return Py_None;
|
||||||
} else
|
} else
|
||||||
PYTORRENTCORE_RAISE_PTR(PyTorrentCoreError, "Invalid handle or no metadata for fastresume.");
|
PYTORRENTCORE_RAISE_PTR(PyTorrentCoreError, "Invalid handle or no metadata for fastresume.");
|
||||||
}
|
}
|
||||||
|
@ -1205,6 +1207,7 @@ static PyMethodDef pytorrent_core_methods[] = {
|
||||||
{"get_num_torrents", torrent_get_num_torrents, METH_VARARGS, "."},
|
{"get_num_torrents", torrent_get_num_torrents, METH_VARARGS, "."},
|
||||||
{"reannounce", torrent_reannounce, METH_VARARGS, "."},
|
{"reannounce", torrent_reannounce, METH_VARARGS, "."},
|
||||||
{"is_paused", torrent_is_paused, METH_VARARGS, "."},
|
{"is_paused", torrent_is_paused, METH_VARARGS, "."},
|
||||||
|
{"is_seeding", torrent_is_seeding, METH_VARARGS, "."},
|
||||||
{"pause", torrent_pause, METH_VARARGS, "."},
|
{"pause", torrent_pause, METH_VARARGS, "."},
|
||||||
{"resume", torrent_resume, METH_VARARGS, "."},
|
{"resume", torrent_resume, METH_VARARGS, "."},
|
||||||
{"get_torrent_info", torrent_get_torrent_info, METH_VARARGS, "."},
|
{"get_torrent_info", torrent_get_torrent_info, METH_VARARGS, "."},
|
||||||
|
|
|
@ -11,16 +11,21 @@
|
||||||
|
|
||||||
import pytorrent
|
import pytorrent
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
import os
|
||||||
|
|
||||||
manager = pytorrent.manager("PT", "0500", "pytorrent - testing only", "test_state.dat")
|
manager = pytorrent.manager("PT", "0500", "pytorrent - testing only",
|
||||||
|
os.path.expanduser("~") + "/Temp",
|
||||||
|
"test_state.dat")
|
||||||
|
|
||||||
my_torrent = manager.add_torrent("ubuntu.torrent", ".", True)
|
#my_torrent = manager.add_torrent("ubuntu.torrent", ".", True)
|
||||||
|
|
||||||
print "Unique ID:", my_torrent
|
#print "Unique ID:", my_torrent
|
||||||
|
|
||||||
while True:
|
for i in range(2):
|
||||||
print "STATE:"
|
print "STATE:"
|
||||||
print manager.get_state(my_torrent)
|
print manager.get_state(0)#my_torrent)
|
||||||
print ""
|
print ""
|
||||||
|
|
||||||
sleep(2)
|
sleep(2)
|
||||||
|
|
||||||
|
manager.quit()
|
||||||
|
|
Loading…
Reference in New Issue