diff options
| -rw-r--r-- | module/HookManager.py | 2 | ||||
| -rw-r--r-- | module/PluginThread.py | 1 | ||||
| -rw-r--r-- | module/SpeedManager.py | 71 | ||||
| -rw-r--r-- | module/config/default.conf | 1 | ||||
| -rwxr-xr-x | pyLoadCore.py | 115 | 
5 files changed, 101 insertions, 89 deletions
| diff --git a/module/HookManager.py b/module/HookManager.py index 8a6193d87..8bb8d1d5b 100644 --- a/module/HookManager.py +++ b/module/HookManager.py @@ -48,6 +48,7 @@ class HookManager():                  return func(*args)              except Exception, e:                  args[0].log.error(_("Error executing hooks: %s") % str(e)) +                traceback.print_exc()          return new      def createIndex(self): @@ -58,7 +59,6 @@ class HookManager():                  #hookClass = getattr(plugin, plugin.__name__)                  if self.core.config.getPlugin(pluginClass.__name__, "load"): -                    #@TODO handle in pluginmanager                      plugin = pluginClass(self.core)                      plugins.append(plugin)                      self.log.info(_("%(name)s loaded, activated %(value)s") % {"name": pluginClass.__name__, "value": plugin.isActivated() }) diff --git a/module/PluginThread.py b/module/PluginThread.py index 9d16155ef..32426766a 100644 --- a/module/PluginThread.py +++ b/module/PluginThread.py @@ -147,7 +147,6 @@ class DownloadThread(PluginThread):              except Reconnect:                  self.queue.put(pyfile) -                #@TODO                  #pyfile.req.clearCookies()                  while self.m.reconnecting.isSet(): diff --git a/module/SpeedManager.py b/module/SpeedManager.py index e69c641fc..e0ce05586 100644 --- a/module/SpeedManager.py +++ b/module/SpeedManager.py @@ -23,44 +23,49 @@ from threading import Thread  from time import sleep, time  class SpeedManager(Thread): -    def __init__(self, parent): +    def __init__(self, core, hook):          Thread.__init__(self) -        self.parent = parent +        self.setDaemon(True) + +        self.hook = hook +        self.core = core          self.running = True          self.lastSlowCheck = 0.0 -         +          stat = {}          stat["slow_downloads"] = None          stat["each_speed"] = None          stat["each_speed_optimized"] = None          self.stat = stat -         +          self.slowCheckInterval = 60          self.slowCheckTestTime = 25 -         -        self.logger = self.parent.parent.logger + +        self.log = self.core.log          self.start() -     +      def run(self):          while self.running:              sleep(1)              self.manageSpeed() -     +      def getMaxSpeed(self): -        return self.parent.parent.getMaxSpeed() -     +        return int(self.hook.getConfig("speed")) +      def manageSpeed(self):          maxSpeed = self.getMaxSpeed() -        if maxSpeed <= 0: -            for thread in self.parent.py_downloading: -                thread.plugin.req.speedLimitActive = False +        if maxSpeed <= 0 or self.core.compare_time(self.hook.getConfig("start").split(":"), +                                                   self.hook.getConfig("end").split(":")): +            for pyfile in [x.active for x in self.core.threadManager.threads if x.active and x.active != "quit"]: +                pyfile.plugin.req.speedLimitActive = False              return -        threads = self.parent.py_downloading + +        threads = [x.active for x in self.core.threadManager.threads if x.active and x.active != "quit"]          threadCount = len(threads) -        if threadCount <= 0: +        if not threads:              return -        eachSpeed = maxSpeed/threadCount -         +        eachSpeed = maxSpeed / threadCount +          currentOverallSpeed = 0          restSpeed = maxSpeed - currentOverallSpeed          speeds = [] @@ -68,39 +73,39 @@ class SpeedManager(Thread):              currentOverallSpeed += thread.plugin.req.dl_speed              speeds.append((thread.plugin.req.dl_speed, thread.plugin.req.averageSpeed, thread))              thread.plugin.req.speedLimitActive = True -         -        if currentOverallSpeed+50 < maxSpeed: -            for thread in self.parent.py_downloading: + +        if currentOverallSpeed + 50 < maxSpeed: +            for thread in threads:                  thread.plugin.req.speedLimitActive = False              return -         +          slowCount = 0          slowSpeed = 0 -        if self.lastSlowCheck + self.slowCheckInterval + self.slowCheckTestTime < time.time(): -            self.lastSlowCheck = time.time() -        if self.lastSlowCheck + self.slowCheckInterval < time.time() < self.lastSlowCheck + self.slowCheckInterval + self.slowCheckTestTime: +        if self.lastSlowCheck + self.slowCheckInterval + self.slowCheckTestTime < time(): +            self.lastSlowCheck = time() +        if self.lastSlowCheck + self.slowCheckInterval < time( +        ) < self.lastSlowCheck + self.slowCheckInterval + self.slowCheckTestTime:              for speed in speeds:                  speed[2].plugin.req.isSlow = False          else:              for speed in speeds: -                if speed[0] <= eachSpeed-7: -                    if speed[1] < eachSpeed-15: -                        if speed[2].plugin.req.dl_time > 0 and speed[2].plugin.req.dl_time+30 < time.time(): +                if speed[0] <= eachSpeed - 7: +                    if speed[1] < eachSpeed - 15: +                        if speed[2].plugin.req.dl_time > 0 and speed[2].plugin.req.dl_time + 30 < time():                              speed[2].plugin.req.isSlow = True -                            if not speed[1]-5 < speed[2].plugin.req.maxSpeed/1024 < speed[1]+5: -                                speed[2].plugin.req.maxSpeed = (speed[1]+10)*1024 +                            if not speed[1] - 5 < speed[2].plugin.req.maxSpeed / 1024 < speed[1] + 5: +                                speed[2].plugin.req.maxSpeed = (speed[1] + 10) * 1024                  if speed[2].plugin.req.isSlow:                      slowCount += 1 -                    slowSpeed += speed[2].plugin.req.maxSpeed/1024 +                    slowSpeed += speed[2].plugin.req.maxSpeed / 1024          stat = {}          stat["slow_downloads"] = slowCount          stat["each_speed"] = eachSpeed          eachSpeed = (maxSpeed - slowSpeed) / (threadCount - slowCount)          stat["each_speed_optimized"] = eachSpeed          self.stat = stat -         +          for speed in speeds:              if speed[2].plugin.req.isSlow:                  continue -            speed[2].plugin.req.maxSpeed = eachSpeed*1024 -            print "max", speed[2].plugin.req.maxSpeed, "current", speed[2].plugin.req.dl_speed +            speed[2].plugin.req.maxSpeed = eachSpeed * 1024 diff --git a/module/config/default.conf b/module/config/default.conf index df8682804..915604084 100644 --- a/module/config/default.conf +++ b/module/config/default.conf @@ -26,7 +26,6 @@ general - "General":  	int max_downloads : "Max Parallel Downloads" = 3
  	bool debug_mode : "Debug Mode" = False
  	int max_download_time : "Max Download Time" = 5
 -	int download_speed_limit : "Download Speed Limit" = 0
  	bool checksum : "Use Checksum" = False
  	int min_free_space : "Min Free Space (MB)" = 200
  	bool folder_per_package : "Create folder for each package" = True
 diff --git a/pyLoadCore.py b/pyLoadCore.py index dd0c1f20f..f33cf3a1f 100755 --- a/pyLoadCore.py +++ b/pyLoadCore.py @@ -50,7 +50,6 @@ from time import sleep  from traceback import print_exc  from xmlrpclib import Binary -  from module import InitHomeDir  from module.AccountManager import AccountManager  from module.CaptchaManager import CaptchaManager @@ -75,21 +74,22 @@ class Core(object):          if len(argv) > 1:              try: -                options, args = getopt(argv[1:], 'vca:hdus', ["version", "clear", "add=", "help", "debug", "user", "setup", "configdir"]) +                options, args = getopt(argv[1:], 'vca:hdus', +                                       ["version", "clear", "add=", "help", "debug", "user", "setup", "configdir"])                  for option, argument in options:                      if option in ("-v", "--version"):                          print "pyLoad", CURRENT_VERSION                          exit()                      elif option in ("-c", "--clear"): -                        try:  +                        try:                              remove("files.db")                              print "Removed Linklist"                          except:                              print "No Linklist found"                      elif option in ("-a", "--add"): -                        #self.arg_links.append(argument) -                        #@TODO +                    #self.arg_links.append(argument) +                    #@TODO                          print "Added %s" % argument                      elif option in ("-h", "--help"):                          self.print_help() @@ -98,18 +98,21 @@ class Core(object):                          self.doDebug = True                      elif option in ("-u", "--user"):                          from module.setup import Setup +                          self.config = ConfigParser()                          s = Setup(pypath, self.config)                          s.set_user()                          exit()                      elif option in ("-s", "--setup"):                          from module.setup import Setup +                          self.config = ConfigParser()                          s = Setup(pypath, self.config)                          s.start()                          exit()                      elif option == "--configdir":                          from module.setup import Setup +                          self.config = ConfigParser()                          s = Setup(pypath, self.config)                          s.conf_path(True) @@ -154,6 +157,7 @@ class Core(object):          if not exists("pyload.conf"):              from module.setup import Setup +              print "This is your first start, running configuration assistent now."              self.config = ConfigParser()              s = Setup(pypath, self.config) @@ -165,33 +169,34 @@ class Core(object):                  print "Setup failed"              if not res:                  remove("pyload.conf") -             +              exit() -         +          try: signal.signal(signal.SIGQUIT, self.quit)          except: pass -             +          self.config = ConfigParser() -         -        translation = gettext.translation("pyLoad", self.path("locale"), languages=["en", self.config['general']['language']]) + +        translation = gettext.translation("pyLoad", self.path("locale"), +                                          languages=["en", self.config['general']['language']])          translation.install(unicode=(True if sys.getfilesystemencoding().lower().startswith("utf") else False)) -         +          self.debug = self.doDebug or self.config['general']['debug_mode']          self.check_file(self.config['log']['log_folder'], _("folder for logs"), True) -         +          if self.debug:              self.init_logger(logging.DEBUG) # logging level          else:              self.init_logger(logging.INFO) # logging level -         +          self.do_kill = False          self.do_restart = False -         +          self.log.info(_("Using home directory: %s") % getcwd()) -         +          #@TODO refractor -         +          self.check_install("Crypto", _("pycrypto to decode container files"))          self.check_install("Image", _("Python Image Libary (PIL) for captcha reading"))          self.check_install("pycurl", _("pycurl to download any files"), True, True) @@ -205,7 +210,6 @@ class Core(object):          if self.config['ssl']['activated']:              self.check_install("OpenSSL", _("OpenSSL for secure connection"), True) -          self.downloadSpeedLimit = int(self.config.get("general", "download_speed_limit"))          self.requestFactory = RequestFactory(self) @@ -249,7 +253,7 @@ class Core(object):          if exists(link_file):              f = open(link_file, "rb") -            links =  [x.strip() for x in f.readlines() if x.strip()] +            links = [x.strip() for x in f.readlines() if x.strip()]              if links:                  self.server_methods.add_package("links.txt", links)                  f.close() @@ -262,15 +266,14 @@ class Core(object):          link_file = "links.txt"          if exists(link_file):              f = open(link_file, "rb") -            links =  [x.strip() for x in f.readlines() if x.strip()] +            links = [x.strip() for x in f.readlines() if x.strip()]              if links:                  self.server_methods.add_package("links.txt", links)                  f.close()                  f = open(link_file, "wb") -                f.close()         +                f.close()          while True: -                                      sleep(2)              if self.do_restart:                  self.log.info(_("restarting pyLoad")) @@ -279,7 +282,7 @@ class Core(object):                  self.shutdown()                  self.log.info(_("pyLoad quits"))                  exit() -                 +              self.threadManager.work()              self.hookManager.periodical() @@ -289,7 +292,8 @@ class Core(object):              usermap = {self.config.username: self.config.password}              if self.config['ssl']['activated']:                  if exists(self.config['ssl']['cert']) and exists(self.config['ssl']['key']): -                    self.server = Server.SecureXMLRPCServer(server_addr, self.config['ssl']['cert'], self.config['ssl']['key'], usermap) +                    self.server = Server.SecureXMLRPCServer(server_addr, self.config['ssl']['cert'], +                                                            self.config['ssl']['key'], usermap)                      self.log.info(_("Secure XMLRPC Server Started"))                  else:                      self.log.warning(_("SSL Certificates not found, fallback to auth XMLRPC server")) @@ -319,7 +323,10 @@ class Core(object):          self.log = logging.getLogger("log") # settable in config          if self.config['log']['file_log']: -            file_handler = logging.handlers.RotatingFileHandler(join(self.config['log']['log_folder'], 'log.txt'), maxBytes=102400, backupCount=int(self.config['log']['log_count'])) #100 kib each +            file_handler = logging.handlers.RotatingFileHandler(join(self.config['log']['log_folder'], 'log.txt'), +                                                                maxBytes=102400, +                                                                backupCount=int(self.config['log']['log_count']) +                                                                ) #100 kib each              file_handler.setFormatter(frm)              self.log.addHandler(file_handler) @@ -363,13 +370,13 @@ class Core(object):                      file_created = False          if not file_exists and not quiet:              if file_created: -                #self.log.info( _("%s created") % description ) +            #self.log.info( _("%s created") % description )                  pass              else:                  if not empty: -                    self.log.warning(_("could not find %(desc)s: %(name)s") % { "desc" : description, "name": tmp_name}) +                    self.log.warning(_("could not find %(desc)s: %(name)s") % {"desc": description, "name": tmp_name})                  else: -                    print _("could not create %(desc)s: %(name)s") % { "desc" : description, "name": tmp_name} +                    print _("could not create %(desc)s: %(name)s") % {"desc": description, "name": tmp_name}                  if essential:                      exit() @@ -381,13 +388,12 @@ class Core(object):          execv(executable, [executable, "pyLoadCore.py"])      def compare_time(self, start, end): -          start = map(int, start)          end = map(int, end)          if start == end: return True -        now  = list(time.localtime()[3:5]) +        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 @@ -399,11 +405,11 @@ class Core(object):          try:              if self.config['webinterface']['activated']:                  self.webserver.quit() -                #self.webserver.join() +            #self.webserver.join()              for thread in self.threadManager.threads:                  thread.put("quit")              pyfiles = self.files.cache.values() -             +              for pyfile in pyfiles:                  pyfile.abortDownload() @@ -423,28 +429,32 @@ class Core(object):          folder = self.config['general']['download_folder']          if 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 -#################################### -    ########## XMLRPC Methods ########## -    #################################### +        #################################### +        ########## XMLRPC Methods ########## +        ####################################  class ServerMethods():      """ methods that can be used by clients with xmlrpc connection""" +      def __init__(self, core):          self.core = core      def status_downloads(self):          """ gives status about all files currently processed """          downloads = [] -        for pyfile in [x.active for x in self.core.threadManager.threads + self.core.threadManager.localThreads if x.active]: +        for pyfile in [x.active for x in self.core.threadManager.threads + self.core.threadManager.localThreads if +                       x.active and x.active != "quit"]:              if not isinstance(pyfile, PyFile):                  continue              download = {} @@ -459,7 +469,7 @@ class ServerMethods():              download['format_size'] = pyfile.formatSize()              download['percent'] = pyfile.getPercent()              download['status'] = pyfile.status -            download['statusmsg'] = pyfile.m.statusMsg[pyfile.status]  +            download['statusmsg'] = pyfile.m.statusMsg[pyfile.status]              download['format_wait'] = pyfile.formatWait()              download['wait_until'] = pyfile.waitUntil              download['package'] = pyfile.package().name @@ -479,10 +489,10 @@ class ServerMethods():              self.core.config[str(cat)][str(opt)] = val          elif sec == "plugin":              self.core.config.setPlugin(cat, opt, val) -         +      def get_config(self):          """ gets complete config """ -        return self.core.config.config  +        return self.core.config.config      def get_plugin_config(self):          """ gets complete plugin config """ @@ -522,20 +532,20 @@ class ServerMethods():          return CURRENT_VERSION      def add_package(self, name, links, queue=0): -        #0 is collector +    #0 is collector          if self.core.config['general']['folder_per_package']:              folder = name          else:              folder = "" -             +          pid = self.core.files.addPackage(name, folder, queue)          self.core.files.addLinks(links, pid) -         +          self.core.log.info(_("Added package %(name)s containing %(count)d links") % {"name": name, "count": len(links)}) -         +          self.core.files.save() -         +          return pid @@ -552,14 +562,13 @@ class ServerMethods():      def del_links(self, ids):          for id in ids:              self.core.files.deleteLink(int(id)) -         +          self.core.files.save()      def del_packages(self, ids): -                  for id in ids:              self.core.files.deletePackage(int(id)) -         +          self.core.files.save()      def kill(self): @@ -576,7 +585,7 @@ class ServerMethods():          return self.core.files.getCompleteData(0)      def add_files_to_package(self, pid, urls): -        #@TODO implement +    #@TODO implement          pass      def push_package_to_queue(self, id): @@ -609,13 +618,13 @@ class ServerMethods():      def stop_downloads(self):          pyfiles = self.core.files.cache.values() -             +          for pyfile in pyfiles:              pyfile.abortDownload()      def abort_files(self, fids):          pyfiles = self.core.files.cache.values() -             +          for pyfile in pyfiles:              if pyfile.id in fids:                  pyfile.abortDownload() @@ -672,18 +681,18 @@ class ServerMethods():          for p in plugins:              data[p.__name__] = p.getAllAccounts()          return data -     +      def update_account(self, plugin, account, password, options=[]):          """ create and update account """          self.core.accountManager.updateAccount(plugin, account, password, options) -     +      def remove_account(self, plugin, account):          self.core.accountManager.removeAccount(plugin, account) -     +      def set_priority(self, id, priority):          p = self.core.files.getPackage(id)          p.setPriority(priority) -     +      def is_time_download(self):          start = self.core.config['downloadTime']['start'].split(":")          end = self.core.config['downloadTime']['end'].split(":") | 
