def _get_post_urls(tags: List[str]) -> List[str]: """Retrieve the links to all of the posts that contain the tags. Args: tags: The tags that the posts must contain. Returns: A list of post urls that contain all of the specified tags. """ if len(tags) == 0: return tags _LOGGER.debug('Retrieving all URLs that contain the tags %s', str(tags)) sanitized_tags = [sanitize_tag(tag) for tag in tags] nozomi_urls = [create_tag_filepath(sanitized_tag) for sanitized_tag in sanitized_tags] tag_post_ids = [_get_post_ids(nozomi_url) for nozomi_url in nozomi_urls] tag_post_ids = set.intersection(*map(set, tag_post_ids)) # Flatten list of tuples on intersection post_urls = [create_post_filepath(post_id) for post_id in tag_post_ids] _LOGGER.debug('Got %d post urls containing the tags %s', len(tags), str(tags)) return post_urls
def get_post(url: str) -> Post: """Retrieve a single post. Args: url: The URL of the post to retrieve. Returns: A post in JSON format if it exists. """ _LOGGER.debug('Retrieving a post from URL "%s"', url) try: post_id = parse_post_id(url) post_url = create_post_filepath(post_id) post_data = requests.get(post_url).json() _LOGGER.debug(post_data) return from_dict(data_class=Post, data=post_data) except InvalidUrlFormat: raise except Exception as ex: _LOGGER.exception(ex) raise
def test_generates_valid_post_address(post_id: int, expected: str): assert create_post_filepath(post_id) == expected