from bs4 import BeautifulSoup
import re


def get_text(connection_or_html) -> str:
    """
    Uses BeautifulSoup to get text from HTML
    """
    # https://stackoverflow.com/questions/1936466/beautifulsoup-grab-visible-webpage-text/1983219#1983219
    if isinstance(connection_or_html, BeautifulSoup):
        soup = connection_or_html
    else:
        soup = BeautifulSoup(connection_or_html, "html5lib")

    [s.extract() for s in soup(['style', 'script', '[document]', 'head', 'title'])]
    [s.extract() for s in soup.find_all(attrs={"style": re.compile("display: ?none|visibility: ?hidden")})]

    split = [re.sub(r"\s+", " ", s) for s in soup.stripped_strings]
    split = [s for s in split if s]

    return "\t".join(split)