Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modified asyncio.TaskGroup() in get_products function #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Backend/scraper/auth_example.json

This file was deleted.

27 changes: 15 additions & 12 deletions Backend/scraper/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def load_auth():
FILE = os.path.join("Scraper", "auth.json")
FILE = os.path.join("scraper", "auth.json")
with open(FILE, "r") as f:
return json.load(f)

Expand Down Expand Up @@ -54,27 +54,30 @@ async def get_products(page, search_text, selector, get_product):
valid_products = []
words = search_text.split(" ")

async with asyncio.TaskGroup() as tg:
for div in product_divs:
async def task(p_div):
product = await get_product(p_div)
tasks = []

if not product["price"] or not product["url"]:
return
for div in product_divs:
async def task(p_div):
product = await get_product(p_div)

for word in words:
if not product["name"] or word.lower() not in product["name"].lower():
break
if not product["price"] or not product["url"]:
return

for word in words:
if not product["name"] or word.lower() not in product["name"].lower():
break
else:
valid_products.append(product)
tg.create_task(task(div))
tasks.append(task(div))

await asyncio.gather(*tasks)

return valid_products


def save_results(results):
data = {"results": results}
FILE = os.path.join("Scraper", "results.json")
FILE = os.path.join("scraper", "results.json")
with open(FILE, "w") as f:
json.dump(data, f)

Expand Down