From 3b3b1bbd3c800e57ad6d74ff0abb8a7a4f171932 Mon Sep 17 00:00:00 2001 From: Joshua Rose Date: Wed, 24 May 2023 12:36:35 +1000 Subject: [PATCH] feat: show author with definition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit now shows the author with the definition. Attempted to add function to get likes and dislikes however they are rendered via javascript post-load so that's impossible 👽 Issue: #2 Authored-by: Joshua Rose --- src/urban.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/urban.py b/src/urban.py index d559db1..7d955f3 100755 --- a/src/urban.py +++ b/src/urban.py @@ -5,6 +5,7 @@ import sys +from typing_extensions import deprecated from bs4 import BeautifulSoup, ResultSet, Tag import colorama @@ -237,7 +238,7 @@ def derive_definition_as_tag(_soup: BeautifulSoup, _index=0) -> Tag: def derive_meaning_as_tag(_soup: BeautifulSoup) -> Tag: - """Return deriveg meaning from `_soup` object. + """Return derived meaning from `_soup` object. :param _soup: `_soup` object as `BeautifulSoup` :return: Word meaning as `Tag` @@ -306,6 +307,33 @@ def format_words_as_string_from_tag(_word_meaning: Tag, _hyperlinks_list: list[s ) return words_as_str +def get_author_from_soup(_soup: BeautifulSoup) -> str: + """Return author from soup. + + :param _soup: `_soup` object as `BeautifulSoup` object. + :return: author as a string + """ + + # get definition container + container = derive_definition_as_tag(_soup) + + return container.find_next("div", class_="contributor").find_next("a")["href"].split("=")[1] # pyright: ignore + +@deprecated("Likes and dislikes are rendered via javascript") +def get_statistics_from_soup(_soup: BeautifulSoup): + """Return likes and dislikes (both integers) as a dictionary. + + :param _soup: `_soup` object as `BeautifulSoup` object. + return: likes and dislikes (both integers) as a dictionary. + """ + + # get definition container + container = derive_definition_as_tag(_soup) + + x: Tag = container.find_next("div", class_="contributor").find_next_sibling("div") # pyright: ignore + print(x.find_all(name='span', recursive=True, attrs={'class': 'text-xs'})) + # print(type(x)) + return; # TODO get `_definition` working def fetch_word_from_remote(_word: str) -> dict[str, str] | None: @@ -348,4 +376,6 @@ def fetch_word_from_remote(_word: str) -> dict[str, str] | None: word = join_words() +get_statistics_from_soup(get_soup_object_from_word(word)) fetch_word_from_remote(word) +print(f"Defined by {colorama.Fore.BLUE}{get_author_from_soup(get_soup_object_from_word(word))}{colorama.Fore.RESET}")