File size: 1,573 Bytes
480adc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import aiohttp
import bot_config
import uuid


async def get_suggestions(query):
    if not bot_config.SUGGESTIONS_API_KEY:
        return None
    suggestions = []
    headers = {
        'Accept-Language': 'en-US,en;q=1',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0 BingSapphire/24.1.410310303'
    }
    async with aiohttp.ClientSession(headers=headers) as session:
        async with session.get(f'https://www.bingapis.com/api/v7/suggestions?appid={bot_config.SUGGESTIONS_API_KEY}&etmkt=en-US&q={query}&lang=en-US,en;q=0.9') as response:
            if response.status != 200:
                return None
            js = await response.json()
            for suggestionGroups in js['suggestionGroups']:
                for searchSuggestion in suggestionGroups['searchSuggestions']:
                    if 'ghostText' in searchSuggestion.keys():
                        suggestions.append(
                            {'id': str(uuid.uuid4()), 'query': searchSuggestion['ghostText']})
                    else:
                        suggestion = searchSuggestion['query']
                        query_split = query.split()
                        if suggestion.startswith(query_split[-1]) and len(query_split) > 1:
                            suggestion = ' '.join(
                                query_split[:-1]) + ' ' + suggestion
                        else:
                            suggestions.append(
                                {'id': str(uuid.uuid4()), 'query': suggestion})
    return suggestions