From b0d9532f675854a6a32d22f7b6b7b4cd32683443 Mon Sep 17 00:00:00 2001
From: Walter Purcaro <vuolter@users.noreply.github.com>
Date: Sun, 2 Aug 2015 07:10:51 +0200
Subject: Fix https://github.com/pyload/pyload/issues/1640

---
 module/plugins/internal/Addon.py  |  7 +++--
 module/plugins/internal/Hoster.py | 64 ++++++++++++++++-----------------------
 module/plugins/internal/Plugin.py | 22 +++++++++++---
 3 files changed, 47 insertions(+), 46 deletions(-)

(limited to 'module')

diff --git a/module/plugins/internal/Addon.py b/module/plugins/internal/Addon.py
index ef2932b11..649eccbc1 100644
--- a/module/plugins/internal/Addon.py
+++ b/module/plugins/internal/Addon.py
@@ -25,7 +25,7 @@ def threaded(fn):
 class Addon(Plugin):
     __name__    = "Addon"
     __type__    = "hook"  #@TODO: Change to `addon` in 0.4.10
-    __version__ = "0.02"
+    __version__ = "0.03"
     __status__  = "testing"
 
     __config__   = []  #: [("name", "type", "desc", "default")]
@@ -166,8 +166,9 @@ class Addon(Plugin):
 
 
     #: Deprecated method, use `download_preparing` instead (Remove in 0.4.10)
-    def downloadPreparing(self, *args, **kwargs):
-        return self.download_preparing(*args, **kwargs)
+    def downloadPreparing(self, pyfile):
+        if pyfile.plugin.req is not None:  #@TODO: Remove in 0.4.10
+            return self.download_preparing(pyfile)
 
 
     def download_finished(self, pyfile):
diff --git a/module/plugins/internal/Hoster.py b/module/plugins/internal/Hoster.py
index ea54d9589..d707154bb 100644
--- a/module/plugins/internal/Hoster.py
+++ b/module/plugins/internal/Hoster.py
@@ -43,7 +43,7 @@ def create_getInfo(klass):
 class Hoster(Plugin):
     __name__    = "Hoster"
     __type__    = "hoster"
-    __version__ = "0.15"
+    __version__ = "0.16"
     __status__  = "testing"
 
     __pattern__ = r'^unmatchable$'
@@ -75,7 +75,7 @@ class Hoster(Plugin):
         #: Account handler instance, see :py:class:`Account`
         self.account = None
         self.user    = None
-        self.req     = None
+        self.req     = None  #: Browser instance, see `network.Browser`
 
         #: Associated pyfile instance, see `PyFile`
         self.pyfile = pyfile
@@ -98,7 +98,8 @@ class Hoster(Plugin):
         self.html = None
 
         #: Dict of the amount of retries already made
-        self.retries = {}
+        self.retries    = {}
+        self.retry_free = False  #@TODO: Recheck in 0.4.10
 
         self._setup()
         self.init()
@@ -132,10 +133,12 @@ class Hoster(Plugin):
 
     def _setup(self):
         if self.account:
+            self.req             = self.pyload.requestFactory.getRequest(self.__name__, self.user)
             self.chunk_limit     = -1  #: -1 for unlimited
             self.resume_download = True
             self.premium         = self.account.is_premium(self.user)
         else:
+            self.req             = self.pyload.requestFactory.getRequest(self.__name__)
             self.chunk_limit     = 1
             self.resume_download = False
             self.premium         = False
@@ -150,15 +153,11 @@ class Hoster(Plugin):
 
         if self.account:
             if not self.user:
-                self.user, data = self.account.select()
+                self.user = self.account.select()[0]
 
-            if not self.user or not self.account.is_logged(self.user, relogin=True):
+            if not self.user or not self.account.is_logged(self.user, True):
                 self.account = False
 
-        #: Browser instance, see `network.Browser`
-        self.req = self.pyload.requestFactory.getRequest(self.__name__,
-                                                         self.user if self.account else None)
-
 
     def preprocessing(self, thread):
         """
@@ -166,10 +165,20 @@ class Hoster(Plugin):
         """
         self.thread = thread
 
-        self.load_account()
+        if self.retry_free:
+            self.account = False
+        else:
+            self.load_account()  #@TODO: Move to PluginThread in 0.4.10
+            self.retry_free = False
+
         self._setup()
         self.setup()
 
+        self.pyload.hookManager.downloadPreparing(self.pyfile)  #@TODO: Recheck in 0.4.10
+
+        if self.pyfile.abort:
+            self.abort()
+
         self.pyfile.setStatus("starting")
         self.log_debug("PROCESS URL " + self.pyfile.url, "PLUGIN VERSION %s" % self.__version__)
 
@@ -316,17 +325,15 @@ class Hoster(Plugin):
         raise Retry(encode(reason))  #@TODO: Remove `encode` in 0.4.10
 
 
-    def restart(self, reason=None, reset=False):
+    def restart(self, reason=None, nopremium=False):
         if not reason:
-            reason = _("Fallback to free download") if reset else _("Restart")
-
-        if reset:
-            if not self.premium:
-                return
+            reason = _("Fallback to free download") if nopremium else _("Restart")
 
-            self.premium = False
-            self.account = False
-            self.req = self.pyload.requestFactory.getRequest(self.__name__)
+        if nopremium:
+            if self.premium:
+                self.retry_free = True
+            else:
+                self.fail(reason, _("Download was already free"))
 
         raise Retry(encode(reason))  #@TODO: Remove `encode` in 0.4.10
 
@@ -639,22 +646,3 @@ class Hoster(Plugin):
                 self.skip(pyfile[0])
 
             self.log_debug("File %s not skipped, because it does not exists." % self.pyfile.name)
-
-
-    def clean(self):
-        """
-        Clean everything and remove references
-        """
-        if hasattr(self, "pyfile"):
-            del self.pyfile
-
-        if hasattr(self, "req"):
-            if self.req:
-                self.req.close()
-            del self.req
-
-        if hasattr(self, "thread"):
-            del self.thread
-
-        if hasattr(self, "html"):
-            del self.html
diff --git a/module/plugins/internal/Plugin.py b/module/plugins/internal/Plugin.py
index d3cc4bbc4..3d53b7d2d 100644
--- a/module/plugins/internal/Plugin.py
+++ b/module/plugins/internal/Plugin.py
@@ -142,7 +142,7 @@ def chunks(iterable, size):
 class Plugin(object):
     __name__    = "Plugin"
     __type__    = "hoster"
-    __version__ = "0.23"
+    __version__ = "0.24"
     __status__  = "testing"
 
     __pattern__ = r'^unmatchable$'
@@ -159,6 +159,7 @@ class Plugin(object):
     def __init__(self, core):
         self.pyload = core
         self.info   = {}  #: Provide information in dict here
+        self.req    = None
         self.init()
 
 
@@ -322,10 +323,7 @@ class Plugin(object):
                            *["%s=%s" % (key, val) for key, val in locals().items() if key not in ("self", "url")])
 
         if req is None:
-            if hasattr(self, "req"):
-                req = self.req
-            else:
-                req = self.pyload.requestFactory.getRequest(self.__name__)
+            req = self.req or self.pyload.requestFactory.getRequest(self.__name__)
 
         res = req.load(url, get, post, ref, cookies, just_header, multipart, decode is True)  #@TODO: Fix network multipart in 0.4.10
 
@@ -370,3 +368,17 @@ class Plugin(object):
             res = header
 
         return res
+
+
+    def clean(self):
+        """
+        Clean everything and remove references
+        """
+        for a in ("pyfile", "thread", "html"):
+            if hasattr(self, a):
+                setattr(self, a, None)
+
+        try:
+            self.req.close()
+        finally:
+            self.req = None
-- 
cgit v1.2.3