Ejemplo n.º 1
0
    def removeAccount(self, plugin, account):
        """Remove account from pyload.

        :param plugin: pluginname
        :param account: accountname
        """
        get_account_manager().removeAccount(plugin, account)
Ejemplo n.º 2
0
    def add(self, user, password=None, options={}):
        self.log_info(_("Adding user `%s`...") % (user[:3] + "*******"))

        if user in self.accounts:
            self.log_error(
                _("Error adding user `%s`") %
                user, _("User already exists"))
            return False

        d = {'login': user,
             'maxtraffic': None,
             'options': options or {'limitdl': ['0']},
             'password': password or "",
             'plugin': get_account_manager().getAccountPlugin(self.classname),
             'premium': None,
             'timestamp': 0,
             'trafficleft': None,
             'type': self.__name__,
             'valid': None,
             'validuntil': None}

        u = self.accounts[user] = d
        result = u['plugin'].choose(user)
        u['plugin'].get_info()

        return result
Ejemplo n.º 3
0
    def __init__(self, pyfile):
        Base.__init__(self, pyfile.m.core)

        self.wantReconnect = False
        #: enables simultaneous processing of multiple downloads
        self.multiDL = True
        self.limitDL = 0
        #: chunk limit
        self.chunkLimit = 1
        self.resumeDownload = False

        #: time() + wait in seconds
        self.waitUntil = 0
        self.waiting = False

        self.ocr = None  #captcha reader instance
        #: account handler instance, see :py:class:`Account`
        self.account = get_account_manager().getAccountPlugin(self.__name__)

        #: premium status
        self.premium = False
        #: username/login
        self.user = None

        if self.account and not self.account.canUse(): self.account = None
        if self.account:
            self.user, data = self.account.selectAccount()
            #: Browser instance, see `network.Browser`
            self.req = self.account.getAccountRequest(self.user)
            self.chunkLimit = -1  # chunk limit, -1 for unlimited
            #: enables resume (will be ignored if server dont accept chunks)
            self.resumeDownload = True
            self.multiDL = True  #every hoster with account should provide multiple downloads
            #: premium status
            self.premium = self.account.isPremium(self.user)
        else:
            self.req = get_request_factory().getRequest(self.__name__)

        #: associated pyfile instance, see `PyFile`
        self.pyfile = pyfile
        self.thread = None  # holds thread in future

        #: location where the last call to download was saved
        self.lastDownload = ""
        #: re match of the last call to `checkDownload`
        self.lastCheck = None
        #: js engine, see `JsEngine`
        self.js = self.core.js
        self.cTask = None  #captcha task

        self.retries = 0  # amount of retries already made
        self.html = None  # some plugins store html code here

        self.init()
Ejemplo n.º 4
0
    def load_account(self):
        if not self.account:
            self.account = get_account_manager().getAccountPlugin(
                self.classname)

        if not self.account:
            self.account = False
            self.user = None  # @TODO: Remove in 0.4.10

        else:
            self.account.choose()
            self.user = self.account.user  # @TODO: Remove in 0.4.10
            if self.account.user is None:
                self.account = False
Ejemplo n.º 5
0
    def getAccounts(self, refresh):
        """Get information about all entered accounts.

        :param refresh: reload account info
        :return: list of `AccountInfo`
        """
        accs = get_account_manager().getAccountInfos(False, refresh)
        accounts = []
        for group in accs.values():
            accounts.extend([
                AccountInfo(acc["validuntil"], acc["login"], acc["options"],
                            acc["valid"], acc["trafficleft"],
                            acc["maxtraffic"], acc["premium"], acc["type"])
                for acc in group
            ])
        return accounts
Ejemplo n.º 6
0
    def getAccountInfos(self, force=True, refresh=False):
        data = {}

        if refresh:
            self.core.scheduler.addJob(0,
                                       get_account_manager().getAccountInfos)
            force = False

        for p in self.accounts.keys():
            if self.accounts[p]:
                p = self.getAccountPlugin(p)
                data[p.__name__] = p.getAllAccounts(force)
            else:
                data[p] = []
        e = AccountUpdateEvent()
        get_pull_manager().addEvent(e)
        return data
Ejemplo n.º 7
0
    def reloadPlugins(self, type_plugins):
        """ reloads and reindexes plugins """

        def merge(dst, src, overwrite=False):
            """merge dict of dicts"""
            for name in src:
                if name in dst:
                    if overwrite is True:
                        dst[name].update(src[name])
                    else:
                        for _k in set(src[name].keys()) - set(dst[name].keys()):
                            dst[name][_k] = src[name][_k]
                else:
                    dst[name] = src[name]


        if not type_plugins: return False

        self.log.debug("Request reload of plugins: %s" % type_plugins)

        as_dict = {}
        for t,n in type_plugins:
            if t in as_dict:
                as_dict[t].append(n)
            else:
                as_dict[t] = [n]

        # we do not reload hooks or internals, would cause to much side effects
        if "hooks" in as_dict or "internal" in as_dict:
            return False

        for _type, value in six.iteritems(as_dict):
            for plugin in value:
                if plugin in self.plugins[type]:
                    if "module" in self.plugins[type][plugin]:
                        self.log.debug("Reloading %s" % plugin)
                        reload(self.plugins[type][plugin]["module"])

        #index creation
        self.crypterPlugins , config = self.parse("crypter", pattern=True)
        self.plugins["crypter"] = self.crypterPlugins
        default_config = config

        self.containerPlugins, config = self.parse("container", pattern=True)
        self.plugins["container"] = self.containerPlugins
        merge(default_config, config)

        self.hosterPlugins, config = self.parse("hoster", pattern=True)
        self.plugins["hoster"] = self.hosterPlugins
        merge(default_config, config)

        self.captchaPlugins, config = self.parse("captcha")
        self.plugins["captcha"] = self.captchaPlugins
        merge(default_config, config)

        self.accountPlugins, config = self.parse("accounts")
        self.plugins["accounts"] = self.accountPlugins
        merge(default_config, config)

        for name, config in default_config.items():
            desc = config.pop('desc', "")
            config = [[k] + list(v) for k, v in config.items()]
            try:
                self.core.config.addPluginConfig(name, config, desc)
            except:
                self.log.error("Invalid config in %s: %s" % (name, config))

        if "accounts" in as_dict: #accounts needs to be reloaded
            account_manager = get_account_manager()

            account_manager.initPlugins()
            self.core.scheduler.addJob(0, account_manager.getAccountInfos)

        return True
Ejemplo n.º 8
0
    def start(self, rpc=True, web=True):
        """ starts the fun :D """

        self.version = CURRENT_VERSION

        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)
            res = False
            try:
                res = s.start()
            except SystemExit:
                pass
            except KeyboardInterrupt:
                print("\nSetup interrupted")
            except:
                res = False
                print_exc()
                print("Setup failed")
            if not res:
                remove("pyload.conf")

            exit()

        try: signal.signal(signal.SIGQUIT, self.quit)
        except: pass

        self.config = ConfigParser()

        gettext.setpaths([join(os.sep, "usr", "share", "pyload", "locale"), None])
        translation = gettext.translation("pyLoad", self.path("locale"),
                                          languages=[self.config['general']['language'],"en"],fallback=True)
        install_translation(translation)

        self.debug = self.doDebug or self.config['general']['debug_mode']
        self.remote &= self.config['remote']['activated']

        pid = self.isAlreadyRunning()
        if pid:
            print(_("pyLoad already running with pid %s") % pid)
            exit()

        if not IS_WINDOWS and self.config["general"]["renice"]:
            os.system("renice %d %d" % (self.config["general"]["renice"], os.getpid()))

        if self.config["permission"]["change_group"]:
            if not IS_WINDOWS:
                try:
                    from grp import getgrnam

                    group = getgrnam(self.config["permission"]["group"])
                    os.setgid(group[2])
                except Exception as e:
                    print(_("Failed changing group: %s") % e)

        if self.config["permission"]["change_user"]:
            if not IS_WINDOWS:
                try:
                    from pwd import getpwnam

                    user = getpwnam(self.config["permission"]["user"])
                    os.setuid(user[2])
                except Exception as e:
                    print(_("Failed changing user: %s") % e)

        self.check_file(
            self.config['log']['log_folder'],
            _("folder for logs"),
            is_folder=True,
        )

        if self.debug:
            self.init_logger(logging.DEBUG) # logging level
        else:
            self.init_logger(logging.INFO) # logging level

        sys.excepthook = exceptHook

        self.do_kill = False
        self.do_restart = False
        self.shuttedDown = False

        self.log.info(_("Starting") + " pyLoad %s" % CURRENT_VERSION)
        self.log.info(_("Using home directory: %s") % getcwd())

        self.writePidFile()

        #@TODO refractor

        remote.activated = self.remote
        self.log.debug("Remote activated: %s" % self.remote)

        self.check_install("Crypto", _("pycrypto to decode container files"))
        #img = self.check_install("Image", _("Python Image Libary (PIL) for captcha reading"))
        #self.check_install("pycurl", _("pycurl to download any files"), True, True)
        self.check_file("tmp", _("folder for temporary files"), is_folder=True)
        #tesser = self.check_install("tesseract", _("tesseract for captcha reading"), False) if not IS_WINDOWS else True

        self.captcha = True # checks seems to fail, althoug tesseract is available

        self.check_file(
            self.config['general']['download_folder'],
            _("folder for downloads"),
            is_folder=True,
        )

        if self.config['ssl']['activated']:
            self.check_install("OpenSSL", _("OpenSSL for secure connection"))

        self.setupDB()

        if self.deleteLinks:
            self.log.info(_("All links removed"))
            self.db.purgeLinks()

        set_request_factory(RequestFactory(self))

        self.lastClientConnected = 0

        # later imported because they would trigger api import, and remote value not set correctly
        from module import Api
        from module.HookManager import HookManager
        from module.ThreadManager import ThreadManager

        if Api.activated != self.remote:
            self.log.warning("Import error: API remote status not correct.")

        self.api = Api.Api(self)

        self.scheduler = Scheduler(self)

        #hell yeah, so many important managers :D
        set_plugin_manager(PluginManager(self))
        set_pull_manager(PullManager(self))
        set_thread_manager(ThreadManager(self))
        set_account_manager(AccountManager(self))
        set_captcha_manager(CaptchaManager(self))
        # HookManager sets itself as a singleton
        HookManager(self)
        set_remote_manager(RemoteManager(self))

        thread_manager = get_thread_manager()

        self.js = JsEngine()

        self.log.info(_("Downloadtime: %s") % self.api.isTimeDownload())

        if rpc:
            get_remote_manager().startBackends()

        if web:
            self.init_webserver()

        spaceLeft = freeSpace(self.config["general"]["download_folder"])

        self.log.info(_("Free space: %s") % formatSize(spaceLeft))

        self.config.save() #save so config files gets filled

        link_file = join(pypath, "links.txt")

        if exists(link_file):
            f = open(link_file, "rb")
            if f.read().strip():
                self.api.addPackage("links.txt", [link_file], 1)
            f.close()

        link_file = "links.txt"
        if exists(link_file):
            f = open(link_file, "rb")
            if f.read().strip():
                self.api.addPackage("links.txt", [link_file], 1)
            f.close()

        #self.scheduler.addJob(0, get_account_manager().getAccountInfos)
        self.log.info(_("Activating Accounts..."))
        get_account_manager().getAccountInfos()

        thread_manager.pause = False
        self.running = True

        self.log.info(_("Activating Plugins..."))
        get_hook_manager().coreReady()

        self.log.info(_("pyLoad is up and running"))

        #test api
#        from module.common.APIExerciser import startApiExerciser
#        startApiExerciser(self, 3)

        #some memory stats
#        from guppy import hpy
#        hp=hpy()
#        import objgraph
#        objgraph.show_most_common_types(limit=20)
#        import memdebug
#        memdebug.start(8002)

        locals().clear()

        while True:
            try:
                sleep(2)
            except IOError as e:
                if e.errno != 4:  # errno.EINTR
                    raise

            if self.do_restart:
                self.log.info(_("restarting pyLoad"))
                self.restart()

            if self.do_kill:
                self.shutdown()
                self.log.info(_("pyLoad quits"))
                self.removeLogger()
                _exit(0) #@TODO thrift blocks shutdown

            thread_manager.work()
            self.scheduler.work()
Ejemplo n.º 9
0
 def updateAccount(self, plugin, account, password=None, options={}):
     """Changes pw/options for specific account."""
     get_account_manager().updateAccount(plugin, account, password, options)
Ejemplo n.º 10
0
    def getAccountTypes(self):
        """All available account types.

        :return: list
        """
        return get_account_manager().accounts.keys()