Skip to content

Commit

Permalink
feat: show author with definition
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
H4ppy-04 committed May 24, 2023
1 parent 6866336 commit 3b3b1bb
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/urban.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


import sys
from typing_extensions import deprecated

from bs4 import BeautifulSoup, ResultSet, Tag
import colorama
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}")

0 comments on commit 3b3b1bb

Please sign in to comment.