Example #1
0
async def google(session: aiohttp.ClientSession,
                 query: str) -> List[GoogleResult]:
    """ Searches Google. """
    url = 'https://www.google.com/search?q={}&safe=on'.format(
        utils.urlescape(query))
    headers = {
        'User-Agent':
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrom'
        'e/57.0.2987.133 Safari/537.36'
    }
    async with session.get(url, headers=headers) as resp:
        soup = BeautifulSoup(await resp.text(), 'html.parser')

        def inflate_node(node):
            a_tag = node.select('h3.r a')
            if not a_tag:
                return None
            a_tag = a_tag[0]

            # the url to the result and the title
            link, link_text = a_tag['href'], a_tag.string

            description = node.select('.rc .s div .st')
            if not description:
                description = None
            else:
                description = description[0].text
            return GoogleResult(url=link,
                                title=link_text,
                                description=description)

        return list(map(inflate_node, soup.find_all('div', {'class': 'g'})))
Example #2
0
async def urban(session, word):
    """ Queries UrbanDictionary for a definition. """
    UD_ENDPOINT = 'http://api.urbandictionary.com/v0/define?term={}'
    async with session.get(UD_ENDPOINT.format(utils.urlescape(word))) as resp:
        json = await resp.json()
        if json['result_type'] == 'no_results':
            return None
        result = json['list'][0]
        return UrbanDefinition(**result)
Example #3
0
async def jisho(session: aiohttp.ClientSession, query: str):
    """ Searches Jisho, and returns definition data as a `dict`. """
    query_url = utils.urlescape(query)
    jisho_endpoint = 'http://jisho.org/api/v1/search/words?keyword={}'
    async with session.get(jisho_endpoint.format(query_url)) as resp:
        data = await resp.json()

        # failed, or no data
        if data['meta']['status'] != 200 or not data['data']:
            return None

        return data['data']
Example #4
0
    async def query(cls, session: ClientSession,
                    word: str) -> Union[None, 'UrbanDefinition']:
        """Queries UrbanDictionary for a definition."""
        async with session.get(UD_ENDPOINT.format(
                utils.urlescape(word))) as resp:
            json = await resp.json()

            # no results :(
            if json['result_type'] == 'no_results':
                return None

            result = json['list'][0]
            return cls(**result)
Example #5
0
async def anime_search(session: aiohttp.ClientSession,
                       query: str) -> List[Anime]:
    """ Searches for anime on MyAnimeList. Returns a list of `Anime` instances. """
    mal_auth = getattr(cfg, 'myanimelist', {})
    auth = aiohttp.BasicAuth(mal_auth['username'], mal_auth['password'])
    query_url = utils.urlescape(query)
    results = []
    try:
        async with session.get(MAL_SEARCH + query_url, auth=auth) as resp:
            tree = ET.fromstring(await resp.text())
            for anime_tag in tree.findall('entry'):
                results.append(
                    Anime(**{a.tag: a.text
                             for a in list(anime_tag)}))
    except aiohttp.ClientResponseError:
        logger.info('Found no anime for %s', query)
        return None
    return results