Skip to content

Commit

Permalink
asyncio patch
Browse files Browse the repository at this point in the history
  • Loading branch information
zdon0 committed Apr 13, 2022
1 parent e0fc0c9 commit 5ae7aef
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/service/database.json
/service/token
/venv/
23 changes: 9 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from random import shuffle

import aiohttp
import nest_asyncio
from aiogram import Bot, Dispatcher, types, executor
from aiogram.utils.exceptions import RetryAfter

Expand Down Expand Up @@ -103,9 +102,9 @@ async def show_like(message: types.Message):
if len(data[message.chat.id]["like"]) == 0:
await message.answer(text="Список пуст")
return
loop = asyncio.get_event_loop()

ans = await message.answer(text="Загрузка")
waiter = loop.create_task(functions.waiter(ans, text="Загрузка"))
waiter = asyncio.create_task(functions.waiter(ans, text="Загрузка"))

bags = list(data[message.chat.id]["like"])
sportmaster_bags = list(filter(lambda x: "sport" in x[0], bags))
Expand All @@ -114,7 +113,7 @@ async def show_like(message: types.Message):
async with aiohttp.ClientSession() as session:
tasks = [asyncio.ensure_future(sportmaster.get_image(bag[0], session)) for bag in sportmaster_bags]
tasks += [asyncio.ensure_future(shein.get_image(bag[0], session)) for bag in shein_bags]
result = loop.run_until_complete(asyncio.gather(*tasks))
result = await asyncio.gather(*tasks)

waiter.cancel()
await ans.delete()
Expand Down Expand Up @@ -206,12 +205,9 @@ async def call_handler_back(call: types.CallbackQuery):

@dp.callback_query_handler(lambda call: call.data == "parse")
async def call_handler_parse(call: types.CallbackQuery):
loop = asyncio.get_event_loop()
waiter = loop.create_task(functions.waiter(call.message))

filters = dict()
for key in data[call.from_user.id]["filters"].keys():
filters[key] = data[call.from_user.id]["filters"][key]
waiter = asyncio.create_task(functions.waiter(call.message))
filters = dict(**data[call.from_user.id]["filters"])

if not filters["colors"]:
filters["colors"] = colors
Expand All @@ -227,11 +223,11 @@ async def call_handler_parse(call: types.CallbackQuery):
if filters != data[call.from_user.id]["filters"]:
await functions.changer(data[call.from_user.id]['ans'], filters)

tasks = [asyncio.ensure_future(f) for f in [sportmaster.parser(loop, filters),
shein.parser(loop, filters)]]

result = []
for elem in loop.run_until_complete(asyncio.gather(*tasks)):
tasks = [asyncio.ensure_future(f) for f in [sportmaster.parser(filters),
shein.parser(filters)]]

for elem in await asyncio.gather(*tasks):
for item in elem:
result.append(item)
shuffle(result)
Expand Down Expand Up @@ -284,7 +280,6 @@ async def call_handler_dislike(call: types.CallbackQuery):


def main():
nest_asyncio.apply()
executor.start_polling(dp)
for key in data.keys():
data[key]['ans'] = None
Expand Down
6 changes: 3 additions & 3 deletions parse/shein.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def page(params, session):
price = connector["price"][params["price"]]

blank = f'https://ru.shein.com/Men-Backpacks-c-2126.html?attr_values={color_val}&attr_ids={color_ids}&max_price={price}&exc_attr_id={randint(20, 2000)}'
# print(blank)

async with session.get(url=blank, headers={}, timeout=5) as response:
bs = BeautifulSoup(await response.text(), 'lxml')

Expand All @@ -67,7 +67,7 @@ async def page(params, session):
return result


async def parser(loop, params):
async def parser(params):
tasks = []
if params["types"] == {"turist"}:
return []
Expand All @@ -87,7 +87,7 @@ async def parser(loop, params):
tasks += [asyncio.ensure_future(page(loop_params, session))]

result = []
for elem in loop.run_until_complete(asyncio.gather(*tasks)):
for elem in await asyncio.gather(*tasks):
for item in elem:
result += [item]
return result
9 changes: 3 additions & 6 deletions parse/sportmaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ async def get_image(url, session):


async def page(params, session):
# print("start:", params["colors"], params["types"])
volume_from = connector2["volume1"][params["volume_1"]]
volume_to = connector2["volume2"][params["volume_2"]]
price_to = connector2["price"][params["price"]]
Expand All @@ -64,16 +63,14 @@ async def page(params, session):
blank = f"https://www.sportmaster.ru/catalog/aksessuary/ryukzaki_i_sumki/ryukzaki/?f-id_ware_subgrp={type}" \
f"&f-volumefrom={volume_from}&f-volumeto={volume_to}" \
f"&f-priceto={price_to}&f-clr={color}&sortType=BY_POPULARITY"
# print(blank)

async with session.get(url=blank, headers=headers) as response:
# print("connect:", params["colors"], params["types"])
text = await response.text()

bs = BeautifulSoup(text, 'lxml')
find = bs.find('script', text=re.compile('searchResult')).text.replace(r'\u002F', r'/')
find = find[find.index('{'):find.index(";(function(){var s;(s=document.currentScript")]
find = json.loads(find)['searchResult']['products']
# print("process:", params["colors"], params["types"])

for elem in find:
name = elem['name']
Expand All @@ -88,7 +85,7 @@ async def page(params, session):
return result


async def parser(loop, params):
async def parser(params):
tasks = []
if not params['colors']:
params['colors'] = {"red", "black", "white", "green"}
Expand All @@ -112,7 +109,7 @@ async def parser(loop, params):
tasks += [asyncio.ensure_future(page(loop_params, session))]

result = []
for elem in loop.run_until_complete(asyncio.gather(*tasks)):
for elem in await asyncio.gather(*tasks):
for item in elem:
result += [item]
return result

0 comments on commit 5ae7aef

Please sign in to comment.