Module: deluge
Branch: 1.3-stable
Commit: 292ffb35acc875f8ab8b956312bf35250b5585bd

Author: John Garland <[email protected]>
Date:   Wed Mar  9 00:37:35 2011 +1100

Handle redirection when adding a torrent by url

---

 deluge/core/core.py                 |   10 ++++++++--
 deluge/ui/gtkui/addtorrentdialog.py |    8 +++++++-
 deluge/ui/web/json_api.py           |    8 +++++++-
 tests/test_core.py                  |   10 ++++++++++
 4 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/deluge/core/core.py b/deluge/core/core.py
index 832872e..7a59713 100644
--- a/deluge/core/core.py
+++ b/deluge/core/core.py
@@ -43,11 +43,13 @@ import threading
 import pkg_resources
 import warnings
 import tempfile
+from urlparse import urljoin
 
 
 from twisted.internet import reactor, defer
 from twisted.internet.task import LoopingCall
 import twisted.web.client
+import twisted.web.error
 
 from deluge.httpdownloader import download_file
 from deluge.log import LOG as log
@@ -249,9 +251,13 @@ class Core(component.Component):
             return self.add_torrent_file(filename, base64.encodestring(data), 
options)
 
         def on_download_fail(failure):
-            if failure.check(twisted.web.client.PartialDownloadError):
+            if failure.check(twisted.web.error.PageRedirect):
+                new_url = urljoin(url, failure.getErrorMessage().split(" to 
")[1])
+                result = download_file(new_url, tempfile.mkstemp()[1], 
headers=headers, force_filename=True)
+                result.addCallbacks(on_download_success, on_download_fail)
+            elif failure.check(twisted.web.client.PartialDownloadError):
                 result = download_file(url, tempfile.mkstemp()[1], 
headers=headers, force_filename=True,
-                                       allow_compression=False)
+                        allow_compression=False)
                 result.addCallbacks(on_download_success, on_download_fail)
             else:
                 # Log the error and pass the failure onto the client
diff --git a/deluge/ui/gtkui/addtorrentdialog.py 
b/deluge/ui/gtkui/addtorrentdialog.py
index cb50cb3..a6a05b7 100644
--- a/deluge/ui/gtkui/addtorrentdialog.py
+++ b/deluge/ui/gtkui/addtorrentdialog.py
@@ -41,10 +41,12 @@ import gettext
 import gobject
 import base64
 import os
+from urlparse import urljoin
 
 import pkg_resources
 
 import twisted.web.client
+import twisted.web.error
 from deluge.ui.client import client
 from deluge.httpdownloader import download_file
 import deluge.component as component
@@ -653,7 +655,11 @@ class AddTorrentDialog(component.Component):
             dialog.destroy()
 
         def on_download_fail(result):
-            if result.check(twisted.web.client.PartialDownloadError):
+            if result.check(twisted.web.error.PageRedirect):
+                new_url = urljoin(url, result.getErrorMessage().split(" to 
")[1])
+                result = download_file(new_url, tmp_file, on_part)
+                result.addCallbacks(on_download_success, on_download_fail)
+            elif result.check(twisted.web.client.PartialDownloadError):
                 result = download_file(url, tmp_file, on_part, 
allow_compression=False)
                 result.addCallbacks(on_download_success, on_download_fail)
             else:
diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py
index 984c178..78ca139 100644
--- a/deluge/ui/web/json_api.py
+++ b/deluge/ui/web/json_api.py
@@ -40,12 +40,14 @@ import shutil
 import logging
 import hashlib
 import tempfile
+from urlparse import urljoin
 
 from types import FunctionType
 from twisted.internet import reactor
 from twisted.internet.defer import Deferred, DeferredList
 from twisted.web import http, resource, server
 import twisted.web.client
+import twisted.web.error
 
 from deluge import common, component, httpdownloader
 from deluge.configmanager import ConfigManager, get_config_dir
@@ -645,7 +647,11 @@ class WebApi(JSONComponent):
             return result
 
         def on_download_fail(result):
-            if result.check(twisted.web.client.PartialDownloadError):
+            if result.check(twisted.web.error.PageRedirect):
+                new_url = urljoin(url, result.getErrorMessage().split(" to 
")[1])
+                result = httpdownloader.download_file(new_url, tmp_file, 
headers=headers)
+                result.addCallbacks(on_download_success, on_download_fail)
+            elif result.check(twisted.web.client.PartialDownloadError):
                 result = httpdownloader.download_file(url, tmp_file, 
headers=headers,
                                                       allow_compression=False)
                 result.addCallbacks(on_download_success, on_download_fail)
diff --git a/tests/test_core.py b/tests/test_core.py
index ce7f666..e149482 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -65,6 +65,16 @@ class CoreTestCase(unittest.TestCase):
 
         return d
 
+    def test_add_torrent_url_with_redirect(self):
+        url = "http://deluge-torrent.org/test_torrent.php?test=redirect";
+        options = {}
+        info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00"
+
+        d = self.core.add_torrent_url(url, options)
+        d.addCallback(self.assertEquals, info_hash)
+
+        return d
+
     def test_add_torrent_url_with_partial_download(self):
         url = "http://deluge-torrent.org/test_torrent.php?test=partial";
         options = {}

-- 
You received this message because you are subscribed to the Google Groups 
"deluge-commit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/deluge-commit?hl=en.

Reply via email to