Modify add_torrent_url to return a deferred that returns the torrent_id as a result if successful

This commit is contained in:
Andrew Resch 2009-07-04 23:06:14 +00:00
commit 8c23baa4e2

View file

@ -40,7 +40,7 @@ import shutil
import threading import threading
import pkg_resources import pkg_resources
from twisted.internet import reactor from twisted.internet import reactor, defer
from twisted.internet.task import LoopingCall from twisted.internet.task import LoopingCall
import twisted.web.client import twisted.web.client
@ -269,21 +269,28 @@ class Core(component.Component):
:param url: str, the url pointing to the torrent file :param url: str, the url pointing to the torrent file
:param options: dict, the options to apply to the torrent on add :param options: dict, the options to apply to the torrent on add
:returns: the torrent_id as a str or None :returns: the torrent_id as a str or None, if calling locally, then it
will return a Deferred that fires once the torrent has been added
""" """
log.info("Attempting to add url %s", url) log.info("Attempting to add url %s", url)
def on_get_page(page): def on_get_page(page):
# We got the data, so attempt adding it to the session # We got the data, so attempt adding it to the session
self.add_torrent_file(url.split("/")[-1], base64.encodestring(page), options) try:
torrent_id = self.add_torrent_file(url.split("/")[-1], base64.encodestring(page), options)
except Exception, e:
d.errback(e)
else:
d.callback(torrent_id)
def on_get_page_error(reason): def on_get_page_error(reason):
log.error("Error occured downloading torrent from %s", url) log.error("Error occured downloading torrent from %s", url)
log.error("Reason: %s", reason) log.error("Reason: %s", reason)
# XXX: Probably should raise an exception to the client here # XXX: Probably should raise an exception to the client here
return d.errback(reason)
twisted.web.client.getPage(url).addCallback(on_get_page).addErrback(on_get_page_error) twisted.web.client.getPage(url).addCallback(on_get_page).addErrback(on_get_page_error)
d = defer.Deferred()
return d
@export @export
def add_torrent_magnets(self, uris, options): def add_torrent_magnets(self, uris, options):