Module: deluge
Branch: master
Commit: 32b41fabd626ea8a13fc26565316f803b8e92a64

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                 |   14 ++++++++++++--
 deluge/tests/test_core.py           |   10 ++++++++++
 deluge/ui/gtkui/addtorrentdialog.py |    8 +++++++-
 deluge/ui/web/json_api.py           |    8 +++++++-
 4 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/deluge/core/core.py b/deluge/core/core.py
index 7d616ee..d0c80fd 100644
--- a/deluge/core/core.py
+++ b/deluge/core/core.py
@@ -41,6 +41,12 @@ import base64
 import logging
 import threading
 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
 
@@ -241,9 +247,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/tests/test_core.py b/deluge/tests/test_core.py
index 76a1bee..f56a957 100644
--- a/deluge/tests/test_core.py
+++ b/deluge/tests/test_core.py
@@ -66,6 +66,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 = {}
diff --git a/deluge/ui/gtkui/addtorrentdialog.py 
b/deluge/ui/gtkui/addtorrentdialog.py
index b26c8c1..2f9ceb1 100644
--- a/deluge/ui/gtkui/addtorrentdialog.py
+++ b/deluge/ui/gtkui/addtorrentdialog.py
@@ -43,10 +43,12 @@ import gobject
 import base64
 import logging
 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
@@ -656,7 +658,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 486dd38..8365784 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)

-- 
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