summaryrefslogtreecommitdiffstats
path: root/module
diff options
context:
space:
mode:
authorGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2011-01-19 22:05:18 +0100
committerGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2011-01-19 22:05:18 +0100
commit4affc4b5f1a11e19c9df1a2e26957622730b9539 (patch)
treef90487b477bc54dd9f36b9d90e32ebb54d592053 /module
parentfixed cleanup (diff)
downloadpyload-4affc4b5f1a11e19c9df1a2e26957622730b9539.tar.xz
fixes for chunked download
Diffstat (limited to 'module')
-rw-r--r--module/PluginThread.py2
-rw-r--r--module/Utils.py36
-rw-r--r--module/network/HTTPDownload.py32
-rw-r--r--module/plugins/Plugin.py11
4 files changed, 57 insertions, 24 deletions
diff --git a/module/PluginThread.py b/module/PluginThread.py
index 67148e325..f31aa43f8 100644
--- a/module/PluginThread.py
+++ b/module/PluginThread.py
@@ -31,7 +31,7 @@ from os.path import join, exists
from pycurl import error
-from Utils import save_join
+from utils import save_join
from module.plugins.Plugin import Abort
from module.plugins.Plugin import Fail
from module.plugins.Plugin import Reconnect
diff --git a/module/Utils.py b/module/Utils.py
index 9cea1bab2..b85f48210 100644
--- a/module/Utils.py
+++ b/module/Utils.py
@@ -3,16 +3,44 @@
""" Store all usefull functions here """
import sys
+import time
from os.path import join
def save_join(*args):
""" joins a path, encoding aware """
paths = []
- for path in args:
- # remove : for win comp.
- tmp = path.replace(":", "").encode(sys.getfilesystemencoding(), "replace")
+ for i, path in enumerate(args):
+ # remove : for win comp, but not for first segment
+ if i:
+ path = path.replace(":","")
+
+ tmp = path.encode(sys.getfilesystemencoding(), "replace")
paths.append(tmp)
return join(*paths)
+def compare_time(start, end):
+ start = map(int, start)
+ end = map(int, end)
+
+ if start == end: return True
+
+ now = list(time.localtime()[3:5])
+ if start < now and end > now: return True
+ elif start > end and (now > start or now < end): return True
+ elif start < now and end < now and start > end: return True
+ else: return False
+
+def freeSpace(folder):
+ if sys.platform == 'nt':
+ import ctypes
+ free_bytes = ctypes.c_ulonglong(0)
+ ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes))
+ return free_bytes.value / 1024 / 1024 #megabyte
+ else:
+ from os import statvfs
+
+ s = statvfs(folder)
+ return s.f_bsize * s.f_bavail / 1024 / 1024 #megabyte
+
if __name__ == "__main__":
- print save_join("test","/test2") \ No newline at end of file
+ print freeSpace(".") \ No newline at end of file
diff --git a/module/network/HTTPDownload.py b/module/network/HTTPDownload.py
index 1c1f53b88..8bc2c7645 100644
--- a/module/network/HTTPDownload.py
+++ b/module/network/HTTPDownload.py
@@ -47,7 +47,6 @@ class HTTPDownload():
self.size = 0
self.chunks = []
- self.chunksDone = 0
self.infoSaved = False # needed for 1 chunk resume
@@ -83,7 +82,7 @@ class HTTPDownload():
def _copyChunks(self):
init = self.info.getChunkName(0) #initial chunk name
- if len(self.chunks) > 1:
+ if self.info.getCount() > 1:
fo = open(init, "rb+") #first chunkfile
for i in range(1, self.info.getCount()):
#input file
@@ -117,22 +116,26 @@ class HTTPDownload():
def _download(self, chunks, resume):
if not resume:
- self.info.addChunk("%s.chunk0" % self.filename, (0, 0)) #set a range so the header is not parsed
+ self.info.addChunk("%s.chunk0" % self.filename, (0, 0)) #create an initial entry
init = HTTPChunk(0, self, None, resume) #initial chunk that will load complete file (if needed)
self.chunks.append(init)
self.m.add_handle(init.getHandle())
+ chunksDone = 0
+ chunksCreated = False
+
while 1:
if (chunks == 1) and self.chunkSupport and self.size and not self.infoSaved:
+ # if chunk size is one, save info file here to achieve resume support
self.info.setSize(self.size)
self.info.createChunks(1)
self.info.save()
self.infoSaved = True
#need to create chunks
- if len(self.chunks) < chunks and self.chunkSupport and self.size: #will be set later by first chunk
+ if not chunksCreated and self.chunkSupport and self.size: #will be set later by first chunk
if not resume:
self.info.setSize(self.size)
@@ -145,9 +148,17 @@ class HTTPDownload():
for i in range(1, chunks):
c = HTTPChunk(i, self, self.info.getChunkRange(i), resume)
- self.chunks.append(c)
+
handle = c.getHandle()
- if handle: self.m.add_handle(handle)
+ if handle:
+ self.chunks.append(c)
+ self.m.add_handle(handle)
+ else:
+ #close immediatly
+ c.close()
+
+
+ chunksCreated = True
while 1:
@@ -158,7 +169,7 @@ class HTTPDownload():
while 1:
num_q, ok_list, err_list = self.m.info_read()
for c in ok_list:
- self.chunksDone += 1
+ chunksDone += 1
for c in err_list:
curl, errno, msg = c
#test if chunk was finished, otherwise raise the exception
@@ -168,11 +179,11 @@ class HTTPDownload():
#@TODO KeyBoardInterrupts are seen as finished chunks,
#but normally not handled to this process, only in the testcase
- self.chunksDone += 1
+ chunksDone += 1
if not num_q:
break
- if self.chunksDone == len(self.chunks):
+ if chunksDone == len(self.chunks):
break #all chunks loaded
# calc speed once per second
@@ -207,8 +218,7 @@ class HTTPDownload():
failed = e.code
remove(self.info.getChunkName(chunk.id))
- chunk.fp.close()
- self.m.remove_handle(chunk.c)
+ chunk.fp.close() #needs to be closed, or merging chunks will fail
if failed: raise BadHeader(failed)
diff --git a/module/plugins/Plugin.py b/module/plugins/Plugin.py
index 880b9e211..e237d52c8 100644
--- a/module/plugins/Plugin.py
+++ b/module/plugins/Plugin.py
@@ -22,8 +22,6 @@ from time import sleep
from random import randint
-import sys
-
import os
from os import remove
from os import makedirs
@@ -38,11 +36,9 @@ if os.name != "nt":
from pwd import getpwnam
from grp import getgrnam
-from mimetypes import guess_type
-
from itertools import islice
-from module.Utils import save_join
+from module.utils import save_join
def chunks(iterable, size):
it = iter(iterable)
@@ -250,9 +246,8 @@ class Plugin(object):
result = ocr.get_captcha(temp.name)
else:
captchaManager = self.core.captchaManager
- mime = guess_type(temp.name)
task = captchaManager.newTask(self)
- task.setCaptcha(content, mime[0])
+ task.setCaptcha(content, None) #@TODO mimetype really needed?
task.setWaiting()
while not task.getStatus() == "done":
if not self.core.isClientConnected():
@@ -318,7 +313,7 @@ class Plugin(object):
download_folder = self.config['general']['download_folder']
- location = save_join(download_folder, self.pyfile.package().folder) # remove : for win compability
+ location = save_join(download_folder, self.pyfile.package().folder)
if not exists(location):
makedirs(location, int(self.core.config["permission"]["folder"],8))