def groupby_category(results: List[Json], key: str, groups: List[Json], default: Optional[str] = None, orderby: Optional[str] = None, reverse: Optional[bool] = False, max_results_per_group: Optional[int] = None) -> Json: rgroups = {g['value']: g['name'] for g in groups} output = OrderedDict({g['name']: [] for g in groups}) if default: output[default] = [] fget = itemgetter(key) for result in results: value = fget(result) if value in rgroups: output[rgroups[value]].append(result) elif default: output[default].append(result) if orderby: sortkey = itemgetter(orderby) for unsorted in output.values(): unsorted.sort(key=sortkey, reverse=reverse) if max_results_per_group: for group in output: output[group] = output[group][:max_results_per_group] logger.warn(", ".join([f"{k}: {len(v)}" for k, v in output.items()])) return output
def get_source(self, **identifiers:Union[str,List[str]]) -> RssFeed: source = self.find_all(**identifiers) if len(source) > 1: logger.warn(f"Parameters {identifiers} not unique") elif len(source) < 1: logger.error(f"No source found with parameters {identifiers}") raise KeyError(identifiers) return source[0]
def get_url(self, category_name:str) -> str: avail = self.available_categories if category_name not in avail: raise KeyError(category_name) filter_func = lambda el: el["name"] == category_name selected = list(filter(filter_func, self.categories)) if len(selected) > 1: logger.warn(f"More than one categories with name {category_name}") return urljoin(self.url, selected[0]["url"])
def get_category_feed(self, category_name:str) -> None: if category_name not in self.available_categories: return url = self.get_url(category_name) logger.info(f"Request feed from {url}") result = feedparser.parse(url) if hasattr(result, "status"): if result.status // 400 > 0: raise requests.HTTPError(result.status) else: logger.warn(result) with Lock(): self._results[category_name] = result
def get_category(self, category_name:str) -> Category: logger.warn(DeprecationWarning("Replace self.get_category(name) to self[name]")) for category in self.categories: if category["name"] == category_name: return category raise KeyError(category_name)