summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar fedeG <federicogonzalez761@gmail.com> 2015-04-20 18:06:51 +0200
committerGravatar Walter Purcaro <vuolter@users.noreply.github.com> 2015-04-20 23:01:25 +0200
commitdf6943c37f10d98a62ca95881116b8b4debda670 (patch)
tree9b55bc9fca4202d38858098e63563da09b7a1e8c
parentMerge pull request #9 from ardi69/0.4.10 (diff)
downloadpyload-df6943c37f10d98a62ca95881116b8b4debda670.tar.xz
PEP-8, Python Zen, refactor and reduce code (part 9 in master module/network)
Conflicts: module/network/Bucket.py module/network/CookieJar.py pyload/network/Browser.py pyload/network/HTTPChunk.py pyload/network/HTTPDownload.py pyload/network/HTTPRequest.py pyload/network/RequestFactory.py pyload/network/XDCCRequest.py
-rw-r--r--pyload/network/Browser.py15
-rw-r--r--pyload/network/Bucket.py7
-rw-r--r--pyload/network/HTTPChunk.py3
-rw-r--r--pyload/network/HTTPDownload.py25
-rw-r--r--pyload/network/HTTPRequest.py8
-rw-r--r--pyload/network/RequestFactory.py9
-rw-r--r--pyload/network/XDCCRequest.py11
7 files changed, 32 insertions, 46 deletions
diff --git a/pyload/network/Browser.py b/pyload/network/Browser.py
index a499336c3..1015efd7e 100644
--- a/pyload/network/Browser.py
+++ b/pyload/network/Browser.py
@@ -24,7 +24,8 @@ class Browser(object):
def renewHTTPRequest(self):
- if hasattr(self, "http"): self.http.close()
+ if hasattr(self, "http"):
+ self.http.close()
self.http = HTTPRequest(self.cj, self.options)
@@ -61,16 +62,12 @@ class Browser(object):
@property
def arrived(self):
- if self.dl:
- return self.dl.arrived
- return 0
+ return self.dl.arrived if self.dl else 0
@property
def percent(self):
- if not self.size:
- return 0
- return (self.arrived * 100) / self.size
+ return (self.arrived * 100) / self.size if self.size else 0
def clearCookies(self):
@@ -94,8 +91,8 @@ class Browser(object):
progressNotify=None, disposition=False):
""" this can also download ftp """
self._size = 0
- self.dl = HTTPDownload(url, filename, get, post, self.lastEffectiveURL if ref else None,
- self.cj if cookies else None, self.bucket, self.options, progressNotify, disposition)
+ self.dl = HTTPDownload(url, filename, get or {}, post or {}, self.lastEffectiveURL if ref else None,
+ self.cj if cookies else None, self.bucket, self.options, progressNotify, disposition)
name = self.dl.download(chunks, resume)
self._size = self.dl.size
diff --git a/pyload/network/Bucket.py b/pyload/network/Bucket.py
index 5f8260384..2f957fcad 100644
--- a/pyload/network/Bucket.py
+++ b/pyload/network/Bucket.py
@@ -17,7 +17,7 @@ class Bucket(object):
def __nonzero__(self):
- return False if self.rate < MIN_RATE else True
+ return self.rate >= MIN_RATE
def setRate(self, rate):
@@ -35,10 +35,7 @@ class Bucket(object):
self.calc_tokens()
self.tokens -= amount
- if self.tokens < 0:
- time = -self.tokens/float(self.rate)
- else:
- time = 0
+ time = -self.tokens / float(self.rate) if self.tokens < 0 else 0
self.lock.release()
return time
diff --git a/pyload/network/HTTPChunk.py b/pyload/network/HTTPChunk.py
index 86c88fe8e..85c20d519 100644
--- a/pyload/network/HTTPChunk.py
+++ b/pyload/network/HTTPChunk.py
@@ -30,7 +30,6 @@ class ChunkInfo(object):
ret = "ChunkInfo: %s, %s\n" % (self.name, self.size)
for i, c in enumerate(self.chunks):
ret += "%s# %s\n" % (i, c[1])
-
return ret
@@ -222,7 +221,7 @@ class HTTPChunk(HTTPRequest):
def writeBody(self, buf):
- #ignore BOM, it confuses unrar
+ # ignore BOM, it confuses unrar
if not self.BOMChecked:
if [ord(b) for b in buf[:3]] == [239, 187, 191]:
buf = buf[3:]
diff --git a/pyload/network/HTTPDownload.py b/pyload/network/HTTPDownload.py
index 8bf822a15..a266548cf 100644
--- a/pyload/network/HTTPDownload.py
+++ b/pyload/network/HTTPDownload.py
@@ -3,14 +3,14 @@
from __future__ import with_statement
+import pycurl
+
from os import remove, fsync
from os.path import dirname
from time import sleep, time
from shutil import move
from logging import getLogger
-import pycurl
-
from pyload.network.HTTPChunk import ChunkInfo, HTTPChunk
from pyload.network.HTTPRequest import BadHeader
@@ -53,7 +53,7 @@ class HTTPDownload(object):
self.chunkSupport = True
self.m = pycurl.CurlMulti()
- #needed for speed calculation
+ # needed for speed calculation
self.lastArrived = []
self.speeds = []
self.lastSpeeds = [0, 0]
@@ -74,10 +74,7 @@ class HTTPDownload(object):
@property
def percent(self):
- if not self.size:
- return 0
- return (self.arrived * 100) / self.size
-
+ return (self.arrived * 100) / self.size if self.size else 0
def _copyChunks(self):
init = fs_encode(self.info.getChunkName(0)) #: initial chunk name
@@ -118,13 +115,13 @@ class HTTPDownload(object):
try:
self._download(chunks, resume)
except pycurl.error, e:
- #code 33 - no resume
+ # code 33 - no resume
code = e.args[0]
if resume is True and code == 33:
# try again without resume
self.log.debug("Errno 33 -> Restart without resume")
- #remove old handles
+ # remove old handles
for chunk in self.chunks:
self.closeChunk(chunk)
@@ -161,7 +158,7 @@ class HTTPDownload(object):
self.chunkSupport = False
while 1:
- #need to create chunks
+ # need to create chunks
if not chunksCreated and self.chunkSupport and self.size: #: will be setted later by first chunk
if not resume:
@@ -215,7 +212,7 @@ class HTTPDownload(object):
for c in err_list:
curl, errno, msg = c
chunk = self.findChunk(curl)
- #test if chunk was finished
+ # test if chunk was finished
if errno != 23 or "0 !=" not in msg:
failed.append(chunk)
ex = pycurl.error(errno, msg)
@@ -237,14 +234,14 @@ class HTTPDownload(object):
if failed and init not in failed and init.c not in chunksDone:
self.log.error(_("Download chunks failed, fallback to single connection | %s" % (str(ex))))
- #list of chunks to clean and remove
+ # list of chunks to clean and remove
to_clean = filter(lambda x: x is not init, self.chunks)
for chunk in to_clean:
self.closeChunk(chunk)
self.chunks.remove(chunk)
remove(fs_encode(self.info.getChunkName(chunk.id)))
- #let first chunk load the rest and update the info file
+ # let first chunk load the rest and update the info file
init.resetRange()
self.info.clear()
self.info.addChunk("%s.chunk0" % self.filename, (0, self.size))
@@ -257,7 +254,7 @@ class HTTPDownload(object):
if len(chunksDone) >= len(self.chunks):
if len(chunksDone) > len(self.chunks):
self.log.warning("Finished download chunks size incorrect, please report bug.")
- done = True #all chunks loaded
+ done = True # all chunks loaded
break
diff --git a/pyload/network/HTTPRequest.py b/pyload/network/HTTPRequest.py
index d87a3ee7e..1c645f898 100644
--- a/pyload/network/HTTPRequest.py
+++ b/pyload/network/HTTPRequest.py
@@ -76,7 +76,7 @@ class HTTPRequest(object):
if hasattr(pycurl, "USE_SSL"):
self.c.setopt(pycurl.USE_SSL, pycurl.CURLUSESSL_TRY)
- #self.c.setopt(pycurl.VERBOSE, 1)
+ # self.c.setopt(pycurl.VERBOSE, 1)
self.c.setopt(pycurl.USERAGENT,
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0")
@@ -254,8 +254,8 @@ class HTTPRequest(object):
for line in header:
line = line.lower().replace(" ", "")
- if not line.startswith("content-type:") or\
- ("text" not in line and "application" not in line):
+ if not line.startswith("content-type:") or \
+ ("text" not in line and "application" not in line):
continue
none, delemiter, charset = line.rpartition("charset=")
@@ -265,7 +265,7 @@ class HTTPRequest(object):
encoding = charset[0]
try:
- #self.log.debug("Decoded %s" % encoding )
+ # self.log.debug("Decoded %s" % encoding )
if lookup(encoding).name == 'utf-8' and rep.startswith(BOM_UTF8):
encoding = 'utf-8-sig'
diff --git a/pyload/network/RequestFactory.py b/pyload/network/RequestFactory.py
index 0591c5162..5e2c15f4b 100644
--- a/pyload/network/RequestFactory.py
+++ b/pyload/network/RequestFactory.py
@@ -7,7 +7,6 @@ from pyload.network.Browser import Browser
from pyload.network.Bucket import Bucket
from pyload.network.HTTPRequest import HTTPRequest
from pyload.network.CookieJar import CookieJar
-
from pyload.network.XDCCRequest import XDCCRequest
@@ -88,8 +87,10 @@ class RequestFactory(object):
else:
type = "http"
setting = self.core.config.get("proxy", "type").lower()
- if setting == "socks4": type = "socks4"
- elif setting == "socks5": type = "socks5"
+ if setting == "socks4":
+ type = "socks4"
+ elif setting == "socks5":
+ type = "socks5"
username = None
if self.core.config.get("proxy", "username") and self.core.config.get("proxy", "username").lower() != "none":
@@ -105,7 +106,7 @@ class RequestFactory(object):
"port": self.core.config.get("proxy", "port"),
"username": username,
"password": pw,
- }
+ }
def getOptions(self):
diff --git a/pyload/network/XDCCRequest.py b/pyload/network/XDCCRequest.py
index dff500749..24146ccaa 100644
--- a/pyload/network/XDCCRequest.py
+++ b/pyload/network/XDCCRequest.py
@@ -2,15 +2,12 @@
# @author: jeix
import socket
-import re
+import struct
from os import remove
from os.path import exists
-
-from time import time
-
-import struct
from select import select
+from time import time
from pyload.plugin.Plugin import Abort
@@ -145,9 +142,7 @@ class XDCCRequest(object):
@property
def percent(self):
- if not self.filesize:
- return 0
- return (self.recv * 100) / self.filesize
+ return (self.recv * 100) / self.filesize if elf.filesize else 0
def close(self):