Spaces:
No application file
No application file
File size: 2,351 Bytes
0c87b2b |
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import aiohttp
import asyncio
from urllib.parse import quote, parse_qs, urlparse
from bs4 import BeautifulSoup
import bot_strings
import bot_chat
TRIES = {}
MAX_TRIES = 3
async def get_images(url, aiosession):
images = []
async with aiosession.get(url) as response:
content = await response.text()
soup = BeautifulSoup(content, "html.parser")
if img_tags := soup.find_all("img"):
images = [img.get('src') for img in img_tags]
return images
async def generate_image(userID, query, cookies, request_id=None):
global TRIES
headers = {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0 BingSapphire/24.1.410310303',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'Origin': 'https://www.bing.com',
'Referer': 'https://www.bing.com/images/create?FORM=GENILP',
'Accept-Language': 'en-US,en;q=0.5'
}
data = {'q': query,
'qs': 'ds'}
params = {'q': query,
'rt': 3,
'FORM': 'GENCRE'}
images, error = [], None
if userID in TRIES.keys():
return [], bot_strings.POCESSING_ALREADY_STRING, False
TRIES[userID] = 0
canceled = False
async with aiohttp.ClientSession(headers=headers, cookie_jar=cookies) as session:
async with session.post('https://www.bing.com/images/create', params=params, data=data) as response:
if not response.history:
error = bot_strings.PROCESSING_ERROR_STRING
else:
parsed_url = urlparse(str(response.url))
id = parse_qs(parsed_url.query)['id'][0]
async with session.get(f'https://www.bing.com/images/create/async/results/{id}?q={quote(query)}') as response:
if response.status == 200:
while not images:
if not (await bot_chat.is_pending(request_id)):
canceled = True
break
images = await get_images(str(response.url), session)
if TRIES[userID] > MAX_TRIES:
break
await asyncio.sleep(5)
del TRIES[userID]
return images, error, canceled
|