Add support for saving .fastresume files. They are not loaded on add
yet.
This commit is contained in:
parent
0e73700f15
commit
78244649b8
|
@ -110,6 +110,12 @@ class Core(dbus.service.Object):
|
|||
# Start the AlertManager
|
||||
self.alerts = AlertManager(self.session)
|
||||
|
||||
# Register alert functions
|
||||
self.alerts.register_handler("torrent_finished_alert",
|
||||
self.on_alert_torrent_finished)
|
||||
self.alerts.register_handler("torrent_paused_alert",
|
||||
self.on_alert_torrent_paused)
|
||||
|
||||
# Register set functions in the Config
|
||||
self.config.register_set_function("listen_ports",
|
||||
self.on_set_listen_ports)
|
||||
|
@ -159,7 +165,7 @@ class Core(dbus.service.Object):
|
|||
del self.config
|
||||
del deluge.configmanager
|
||||
del self.session
|
||||
|
||||
|
||||
# Exported Methods
|
||||
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
|
||||
in_signature="", out_signature="")
|
||||
|
@ -230,15 +236,14 @@ class Core(dbus.service.Object):
|
|||
in_signature="s", out_signature="")
|
||||
def pause_torrent(self, torrent_id):
|
||||
log.debug("Pausing torrent %s", torrent_id)
|
||||
if self.torrents.pause(torrent_id):
|
||||
self.torrent_paused(torrent_id)
|
||||
if not self.torrents.pause(torrent_id):
|
||||
log.warning("Error pausing torrent %s", torrent_id)
|
||||
|
||||
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge")
|
||||
def pause_all_torrents(self):
|
||||
"""Pause all torrents in the session"""
|
||||
if self.torrents.pause_all():
|
||||
# Emit 'torrent_all_paused' signal
|
||||
self.torrent_all_paused()
|
||||
if not self.torrents.pause_all():
|
||||
log.warning("Error pausing all torrents..")
|
||||
|
||||
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge")
|
||||
def resume_all_torrents(self):
|
||||
|
@ -481,3 +486,21 @@ class Core(dbus.service.Object):
|
|||
def on_set_max_upload_slots_per_torrent(self, key, value):
|
||||
log.debug("max_upload_slots_per_torrent set to %s..", value)
|
||||
self.torrents.set_max_uploads(value)
|
||||
|
||||
## Alert handlers ##
|
||||
def on_alert_torrent_finished(self, alert):
|
||||
log.debug("on_alert_torrent_finished")
|
||||
# Get the torrent_id
|
||||
torrent_id = str(alert.handle.info_hash())
|
||||
log.debug("%s is finished..", torrent_id)
|
||||
# Write the fastresume file
|
||||
self.torrents.write_fastresume(torrent_id)
|
||||
|
||||
def on_alert_torrent_paused(self, alert):
|
||||
log.debug("on_alert_torrent_paused")
|
||||
# Get the torrent_id
|
||||
torrent_id = str(alert.handle.info_hash())
|
||||
# Write the fastresume file
|
||||
self.torrents.write_fastresume(torrent_id)
|
||||
# Emit torrent_paused signal
|
||||
self.torrent_paused(torrent_id)
|
||||
|
|
|
@ -49,7 +49,7 @@ class Torrent:
|
|||
self.total_uploaded = 0
|
||||
# Set the allocation mode
|
||||
self.compact = compact
|
||||
|
||||
|
||||
def get_state(self):
|
||||
"""Returns the state of this torrent for saving to the session state"""
|
||||
return (self.torrent_id, self.filename, self.compact)
|
||||
|
|
|
@ -63,6 +63,8 @@ class TorrentManager:
|
|||
log.debug("TorrentManager init..")
|
||||
# Set the libtorrent session
|
||||
self.session = session
|
||||
# Get the core config
|
||||
self.config = ConfigManager("core.conf")
|
||||
# Per torrent connection limit and upload slot limit
|
||||
self.max_connections = -1
|
||||
self.max_uploads = -1
|
||||
|
@ -87,8 +89,6 @@ class TorrentManager:
|
|||
def add(self, filename, filedump=None, compact=None):
|
||||
"""Add a torrent to the manager and returns it's torrent_id"""
|
||||
log.info("Adding torrent: %s", filename)
|
||||
# Get the core config
|
||||
config = ConfigManager("core.conf")
|
||||
|
||||
# Make sure 'filename' is a python string
|
||||
filename = str(filename)
|
||||
|
@ -103,7 +103,8 @@ class TorrentManager:
|
|||
# Get the data from the file
|
||||
try:
|
||||
log.debug("Attempting to open %s for add.", filename)
|
||||
filedump = open(os.path.join(config["torrentfiles_location"],
|
||||
filedump = open(
|
||||
os.path.join(self.config["torrentfiles_location"],
|
||||
filename), "rb").read()
|
||||
except IOError:
|
||||
log.warning("Unable to open %s", filename)
|
||||
|
@ -115,12 +116,12 @@ class TorrentManager:
|
|||
|
||||
# Make sure we are adding it with the correct allocation method.
|
||||
if compact is None:
|
||||
compact = config["compact_allocation"]
|
||||
compact = self.config["compact_allocation"]
|
||||
|
||||
try:
|
||||
handle = self.session.add_torrent(
|
||||
lt.torrent_info(torrent_filedump),
|
||||
config["download_location"],
|
||||
self.config["download_location"],
|
||||
compact)
|
||||
except RuntimeError:
|
||||
log.warning("Error adding torrent")
|
||||
|
@ -135,17 +136,18 @@ class TorrentManager:
|
|||
|
||||
log.debug("Attemping to save torrent file: %s", filename)
|
||||
# Test if the torrentfiles_location is accessible
|
||||
if os.access(os.path.join(config["torrentfiles_location"]), os.F_OK) \
|
||||
if os.access(
|
||||
os.path.join(self.config["torrentfiles_location"]), os.F_OK) \
|
||||
is False:
|
||||
# The directory probably doesn't exist, so lets create it
|
||||
try:
|
||||
os.makedirs(os.path.join(config["torrentfiles_location"]))
|
||||
os.makedirs(os.path.join(self.config["torrentfiles_location"]))
|
||||
except IOError:
|
||||
log.warning("Unable to create torrent files directory..")
|
||||
|
||||
# Write the .torrent file to the torrent directory
|
||||
try:
|
||||
save_file = open(os.path.join(config["torrentfiles_location"],
|
||||
save_file = open(os.path.join(self.config["torrentfiles_location"],
|
||||
filename),
|
||||
"wb")
|
||||
save_file.write(filedump)
|
||||
|
@ -207,16 +209,21 @@ class TorrentManager:
|
|||
except:
|
||||
return False
|
||||
|
||||
status = self.torrents[torrent_id].get_status(
|
||||
["total_done", "total_wanted"])
|
||||
|
||||
# Only delete the .fastresume file if we're still downloading stuff
|
||||
if status["total_done"] < status["total_wanted"]:
|
||||
self.delete_fastresume(torrent_id)
|
||||
return True
|
||||
|
||||
def resume_all(self):
|
||||
"""Resumes all torrents.. Returns a list of torrents resumed"""
|
||||
torrent_was_resumed = False
|
||||
for key in self.torrents.keys():
|
||||
try:
|
||||
self.torrents[key].handle.resume()
|
||||
if self.resume(key):
|
||||
torrent_was_resumed = True
|
||||
except:
|
||||
else:
|
||||
log.warning("Unable to resume torrent %s", key)
|
||||
|
||||
return torrent_was_resumed
|
||||
|
@ -264,7 +271,34 @@ class TorrentManager:
|
|||
state_file.close()
|
||||
except IOError:
|
||||
log.warning("Unable to save state file.")
|
||||
|
||||
|
||||
def delete_fastresume(self, torrent_id):
|
||||
"""Deletes the .fastresume file"""
|
||||
torrent = self.torrents[torrent_id]
|
||||
path = "%s/%s.fastresume" % (
|
||||
self.config["torrentfiles_location"],
|
||||
torrent.filename)
|
||||
log.debug("Deleting fastresume file: %s", path)
|
||||
try:
|
||||
os.remove(path)
|
||||
except IOError:
|
||||
log.warning("Unable to delete the fastresume file: %s", path)
|
||||
|
||||
def write_fastresume(self, torrent_id):
|
||||
"""Writes the .fastresume file for the torrent"""
|
||||
torrent = self.torrents[torrent_id]
|
||||
resume_data = lt.bencode(torrent.handle.write_resume_data())
|
||||
path = "%s/%s.fastresume" % (
|
||||
self.config["torrentfiles_location"],
|
||||
torrent.filename)
|
||||
log.debug("Saving fastresume file: %s", path)
|
||||
try:
|
||||
fastresume = open(path,"wb")
|
||||
fastresume.write(resume_data)
|
||||
fastresume.close()
|
||||
except IOError:
|
||||
log.warning("Error trying to save fastresume file")
|
||||
|
||||
def set_max_connections(self, value):
|
||||
"""Sets the per-torrent connection limit"""
|
||||
self.max_connections = value
|
||||
|
|
Loading…
Reference in New Issue