Exemplo n.º 1
0
class WGSelect(object):
    """ Select which modules to run """
    def __init__(self, pargs):
        self.pargs = pargs
        self.modules = list()
        self.targets = list()
        self.items = dict()
        self.results_type = None
        self.results = None
        self.filter = None
        self.parse_args()
        self.sort_type = None

    def parse_args(self):
        for arg in self.pargs:
            if arg == "--filter" or arg == "--quality":
                self.filter = self.pargs[arg][0]
            elif arg == "--results":
                self.results = int(self.pargs[arg][0])
            elif arg == "--target":
                self.targets = self.pargs[arg][0].split(',')
            elif arg == '--links':
                self.results_type = 'L'
            elif arg == '--json':
                self.results_type = 'J'
            elif arg == "--sort":
                self.sort_type = self.pargs[arg][0]

    def cut_items(self, items, results):
        """cut_items: show N items.
        """
        nitems = dict()
        for key in sorted(items)[:results]:
            nitems.update({key: items[key]})
        return nitems

    def filter_items(self, fx):
        """filter_items: match text or regex in the torrent name.
        """
        nitems = dict()
        for item in self.items:
            if re.findall(fx, item):
                nitems.update({item: self.items[item]})
        return nitems

    def add_items_label(self, target, items):
        """ add_items_label - add label of the target to the torrent name.
          @target
          @items
        """
        nitems = dict()

        for item in items:
            items[item].update({"target": target})
            nitems.update({item: items[item]})
        return nitems

    def sort_items_by_seeds(self, items):
        """sort_items_by_seeds - sort items by number of seeds.
        """
        nitems = OrderedDict()

        # Sort by number of seeds
        i = sorted(items, key=lambda x: int(items[x]['seeds']), reverse=True)
        for item in i:
            nitems.update({item: items[item]})
        return nitems

    """sort_items_by_name"""

    def sort_items_by_name(self, items):
        return collections.OrderedDict(sorted(items.items()))

    def run(self, api_mode=False):
        items = dict()

        if self.targets[0] == "all":
            self.targets.pop()
            self.targets = list_wg_modules()

        for target in self.targets:
            if not self.results_type and not api_mode:
                msg_fetching(target)
            path = "we_get.modules.%s" % (target)
            try:
                run = import_module(path)
            except ImportError:
                msg_error("Cannot find target \'%s\'." % (target), True)
            except Exception:
                msg_info("Module: \'%s.py\' stopped!" % (target))
                msg_err_trace(True)
            items = run.main(self.pargs)
            items = self.add_items_label(target, items)
            if items:
                self.items.update(items)
            else:
                msg_error(" \'%s\' - no results" % (target), False)
                continue
        """Sort self.items"""
        if self.filter:
            self.items = self.filter_items(self.filter)
        if self.results:
            self.items = self.cut_items(self.items, self.results)
        if self.sort_type == "name":
            self.items = self.sort_items_by_name(self.items)
        else:
            self.items = self.sort_items_by_seeds(self.items)

        if api_mode:
            return self.items
        elif self.results_type == 'J':
            print(dumps(self.items, indent=2, sort_keys=True))
        elif self.results_type == 'L':
            [print(self.items[item]['link']) for item in self.items]
        else:
            # XXX: import we_get.core.shell is here for optimization.
            # we-get will load 50% faster!
            from we_get.core.shell import Shell
            self.shell = Shell()
            self.shell.shell(self.items, self.pargs)
Exemplo n.º 2
0
class WGSelect(object):
    """ Select which modules to run """
    def __init__(self, pargs):
        self.pargs = pargs
        self.modules = list()
        self.targets = list()
        self.items = dict()
        self.results_type = None
        self.results = None
        self.filter = None
        self.parse_args()
        self.sort_type = None

    def parse_args(self):
        for arg in self.pargs:
            if arg == "--filter" or arg == "--quality":
                self.filter = self.pargs[arg][0]
            elif arg == "--results":
                self.results = int(self.pargs[arg][0])
            elif arg == "--target":
                self.targets = self.pargs[arg][0].split(',')
            elif arg == '--links':
                self.results_type = 'L'
            elif arg == '--json':
                self.results_type = 'J'
            elif arg == "--sort":
                self.sort_type = self.pargs[arg][0]
            elif arg == "--config":
                self.config = configparser.ConfigParser()
                config_file = self.pargs[arg][0]
                with open(config_file) as f:
                    self.config.read_file(f)

    def cut_items(self, items, results):
        """cut_items: show N items.

        for cutting OrderedDict use method as on link below
        https://stackoverflow.com/a/12988463/1766261

        Args:
            items (OrderedDict): Items to cut.
            results (int): Number of item to cut.

        """
        nitems = dict()
        # cutting have to follow the sorted rule
        if self.sort_type == "name":
            sorted_items = self.sort_items_by_name(items)
        else:
            sorted_items = self.sort_items_by_seeds(items)
        sorted_items = iter(sorted_items.items())
        try:
            nitems = OrderedDict(itertools.islice(sorted_items, results))
        except ValueError:
            logging.error('Error when cut items: n={}'.format(results))
            nitems = items
        return nitems

    def filter_items(self, fx):
        """filter_items: match text or regex in the torrent name.
        """
        nitems = dict()
        for item in self.items:
            if re.findall(fx, item):
                nitems.update({item: self.items[item]})
        return nitems

    def add_items_label(self, target, items):
        """ add_items_label - add label of the target to the torrent name.
          @target
          @items
        """
        nitems = dict()

        for item in items:
            items[item].update({"target": target})
            nitems.update({item: items[item]})
        return nitems

    def sort_items_by_seeds(self, items):
        """sort_items_by_seeds - sort items by number of seeds.
        """
        nitems = OrderedDict()

        # Sort by number of seeds
        i = sorted(items, key=lambda x: int(items[x]['seeds']), reverse=True)
        for item in i:
            nitems.update({item: items[item]})
        return nitems

    """sort_items_by_name"""

    def sort_items_by_name(self, items):
        return collections.OrderedDict(sorted(items.items()))

    def run(self, api_mode=False):
        items = dict()

        if self.targets[0] == "all":
            self.targets.pop()
            self.targets = list_wg_modules()

        for target in self.targets:
            if not self.results_type and not api_mode:
                msg_fetching(target)
            path = "we_get.modules.%s" % (target)
            try:
                run = import_module(path)
            except ImportError:
                msg_error("Cannot find target \'%s\'." % (target), True)
            except Exception:
                msg_info("Module: \'%s.py\' stopped!" % (target))
                msg_err_trace(True)
            items = run.main(self.pargs)
            items = self.add_items_label(target, items)
            if items:
                self.items.update(items)
            else:
                msg_error(" \'%s\' - no results" % (target), False)
                continue
        """Sort self.items"""
        if self.filter:
            self.items = self.filter_items(self.filter)
        if self.sort_type == "name":
            self.items = self.sort_items_by_name(self.items)
        else:
            self.items = self.sort_items_by_seeds(self.items)
        # items cut must at the end of item processing.
        if self.results:
            self.items = self.cut_items(self.items, self.results)

        if api_mode:
            return self.items
        elif self.results_type == 'J':
            print(dumps(self.items, indent=2, sort_keys=True))
        elif self.results_type == 'L':
            [print(self.items[item]['link']) for item in self.items]
        else:
            # XXX: import we_get.core.shell is here for optimization.
            # we-get will load 50% faster!
            from we_get.core.shell import Shell
            self.shell = Shell()
            if hasattr(self, 'config') and self.config['item_color']:
                self.shell.item_color.update(self.config['item_color'])
            self.shell.shell(self.items, self.pargs)
Exemplo n.º 3
0
def main():
    s = Shell()
    s.shell(x, {'--search': ['test']})