Exemplo n.º 1
0
        def load_project(_projectname, spider_path, models_path):
            """
            Load Project File to Database
            :param spider_path: project spider path
            :return:
            """
            try:
                with open(spider_path, 'rb') as fp:
                    spider_script = fp.read().decode('utf8')
                with open(models_path, 'rb') as fp:
                    models_script = fp.read().decode('utf8')
                project = Project.objects(name=_projectname).first()
                if project:
                    project.update(script=spider_script)
                    project.update(models=models_script)
                else:
                    project = Project(name=_projectname,
                                      info="",
                                      script=spider_script,
                                      models=models_script,
                                      generator_interval="60",
                                      downloader_interval="60",
                                      downloader_dispatch=1)
                    project.save()

            except Exception:
                reason = traceback.format_exc()
                raise CommandError('Failed to load project %s !, Reason: %s' % (spider_path, reason))
Exemplo n.º 2
0
    def _load_project(self, project_name, spider_path, models_path):
        """
        _load project
        :param project_name:
        :param spider_path:
        :param models_path:
        :return:
        """
        try:
            with open(spider_path, 'rb') as fp:
                spider_script = fp.read().decode('utf8')
            with open(models_path, 'rb') as fp:
                models_script = fp.read().decode('utf8')
            project = Project.objects(name=project_name).first()
            if project:
                project.update(script=spider_script)
                project.update(models=models_script)
            else:
                project = Project(name=project_name,
                                  info="",
                                  script=spider_script,
                                  models=models_script,
                                  generator_interval="60",
                                  downloader_interval="60",
                                  downloader_dispatch=1)
                project.save()
            message = 'Successfully load project %s !' % (project_name)
            return {"status": True, "message": message}

        except Exception:
            reason = traceback.format_exc()
            message = 'Failed to load project %s !, Reason: %s' % (spider_path,
                                                                   reason)
            return {"status": False, "message": message}