Skip to content

Commit

Permalink
Properly handle magnet links
Browse files Browse the repository at this point in the history
- Adds a handler for the metadata_received_alert that re-labels any
magnet torrents
  • Loading branch information
bdutro committed Oct 23, 2019
1 parent 41cb08a commit c8fabc2
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions labelplus/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ def hook_set_torrent(torrent_id, label_id):

deluge.component.get("EventManager").register_event_handler(
"TorrentAddedEvent", self.on_torrent_added)
deluge.component.get("AlertManager").register_handler(
"metadata_received_alert", self.on_torrent_metadata_received)
deluge.component.get("EventManager").register_event_handler(
"PreTorrentRemovedEvent", self.on_torrent_removed)

Expand Down Expand Up @@ -215,6 +217,7 @@ def hook_set_torrent(torrent_id, label_id):
log.debug("%s initialized", self.__class__.__name__)

self.__pending_labels = {}
self.__pending_magnet_ids = set()


def _load_config(self):
Expand Down Expand Up @@ -734,6 +737,20 @@ def on_torrent_added(self, torrent_id, from_state):

self._timestamp["mappings_changed"] = datetime.datetime.now()

if self._torrents[torrent_id].magnet is not None:
self.__pending_magnet_ids.add(torrent_id)


@check_init
def on_torrent_metadata_received(self, alert):
try:
torrent_id = str(alert.handle.info_hash())
except RuntimeError:
return
assert(torrent_id in self._torrents)
if torrent_id in self.__pending_magnet_ids:
self.on_torrent_added(torrent_id, False)
self.__pending_magnet_ids.remove(torrent_id)

@check_init
def on_torrent_removed(self, torrent_id):
Expand Down

0 comments on commit c8fabc2

Please sign in to comment.