def get_top_link(query: str) -> str:
    if sys.platform == "darwin":
        import importlib.util  # pylint: disable=import-outside-toplevel

        spec = importlib.util.spec_from_file_location(
            "http.cookies",
            join(PY3_LIB_DIR, "http/cookies.py"),
        )
        cookies = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(cookies)
        sys.modules["http.cookies"] = cookies

        sys.path.append("/usr/local/lib/python3.7/site-packages")

    from bs4 import BeautifulSoup
    import requests  # pylint: disable=import-outside-toplevel

    try:
        html = _fetch_results(query)
    except requests.exceptions.HTTPError as e:
        return e.response.url

    soup = BeautifulSoup(html, 'html.parser')
    result_block = soup.find_all('div', attrs={'class': 'g'})
    for result in result_block:
        link = result.find('a', href=True)
        if link and link != '#' and re.match('^http[s]?://', link['href']):
            return link['href']

    return 'https://www.google.com/search?q={}'.format(utils.encode(query))
Beispiel #2
0
def get_top_link(query: str) -> str:
    from bs4 import BeautifulSoup
    import requests  # pylint: disable=import-outside-toplevel

    try:
        html = _fetch_results(query)
    except requests.exceptions.HTTPError as e:
        result: str = e.response.url
        return result

    soup = BeautifulSoup(html, "html.parser")
    a_tags = soup.find_all("a", href=True)
    for a_tag in a_tags:
        match = re.match(r"^/url\?q=(.*?)&.*$", a_tag["href"])
        if match:
            result = match.group(1)
            return result

    div_tags = soup.find_all("div", attrs={"class": "g"})
    for div_tag in div_tags:
        a_tag = div_tag.find("a", href=True)
        if a_tag and a_tag != "#" and re.match("^http[s]?://", a_tag["href"]):
            result = a_tag["href"]
            return result

    return "https://www.google.com/search?q={}".format(utils.encode(query))
Beispiel #3
0
 def make_lucky(cls, query: str, suffix: str = "") -> str:
     query = utils.encode(query)
     fmt_url = "{}{{}}{}{}".format(
         cls.start_mark,
         cls.end_mark,
         re.sub(r"\{(\d*)\}", r"{{\1}}", suffix),
     )
     return fmt_url.format(query)
Beispiel #4
0
def _validate_prefix(prefix: str = None) -> str:
    """Validates and Beautifies @prefix Argument"""
    if prefix is None:
        return ""
    elif prefix[-1] != " ":
        prefix = prefix + " "

    return utils.encode(prefix)
Beispiel #5
0
 def make_lucky(cls, query: str, suffix: str = '') -> str:
     query = utils.encode(query)
     fmt_url = '{}{{}}{}{}'.format(
         cls.start_mark,
         cls.end_mark,
         re.sub(r'\{(\d*)\}', r'{{\1}}', suffix),
     )
     return fmt_url.format(query)
Beispiel #6
0
        def filter(  # pylint: disable=method-hidden
                cls, query: str) -> Sequence:
            nums: List = re.split(utils.encode(" "), query, maxsplit=n)

            result = nums[:]
            for i in range(n):
                result[i] = int(nums[i])

            return result
Beispiel #7
0
def google(query: str, *, max_years_old: int = None) -> SE.SearchEngine:
    encoded_query = utils.encode(query)
    if max_years_old is None:
        return SE.SearchEngine(
            'https://google.com/search?q={}'.format(encoded_query))
    else:
        D = _n_years_ago(max_years_old)
        return SE.SearchEngine(
            f'https://google.com/search?q={encoded_query}&source=lnt&tbs=cdr%3A1%2Ccd_min%3A{D.month}%2F{D.day}%2F{D.year}%2Ccd_max%3A&tbm='
        )
Beispiel #8
0
def _fetch_results(query: str) -> str:
    # dynamic import needed to work around weird qutebrowser bug with
    # 'cryptography' module
    import requests  # pylint: disable=import-outside-toplevel

    assert isinstance(query, str), "Search term must be a string"

    encoded_query = utils.encode(query)

    google_url = "https://www.google.com/search?q={}".format(encoded_query)
    response = requests.get(google_url, headers=USER_AGENT)
    response.raise_for_status()

    return response.text
Beispiel #9
0
    def __init__(self,
                 url: str,
                 pattern: str = None,
                 filter_: FilterType = None):
        self.url = url

        if pattern is None:
            self.pattern = ".*"
        else:
            self.pattern = utils.encode(pattern)

        if filter_ is None:
            self.filter = lambda x: x
        else:
            self.filter = filter_
Beispiel #10
0
def google(query: str, *, max_years_old: int = None) -> SE.SearchEngine:
    encoded_query = utils.encode(query)
    if max_years_old is None:
        return SE.SearchEngine(
            "https://google.com/search?q={}".format(encoded_query))
    else:
        D = _n_years_ago(max_years_old)
        month = D.month
        day = D.day
        year = D.year

        google_search_url = (
            "https://google.com/search"
            f"?q={encoded_query}"
            "&source=lnt"
            f"&tbs=cdr%3A1%2Ccd_min%3A{month}%2F{day}%2F{year}%2Ccd_max%3A"
            "&tbm=")
        return SE.SearchEngine(google_search_url)
Beispiel #11
0
def duckduckgo(query: str) -> "SE.SearchEngine":
    encoded_query = utils.encode(query)
    return SE.SearchEngine(
        "https://duckduckgo.com/?q={}".format(encoded_query))
Beispiel #12
0
def duckduckgo(query: str) -> 'SE.SearchEngine':
    encoded_query = utils.encode(query)
    return SE.SearchEngine(
        'https://duckduckgo.com/?q={}'.format(encoded_query))