Skip to content

Commit

Permalink
Ability to search in all the engines at once, prices are also now sorted
Browse files Browse the repository at this point in the history
properly
  • Loading branch information
hegzploit committed Aug 1, 2022
1 parent 44515e8 commit 6a63610
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions bgneh.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@
from iterfzf import iterfzf
from typing import List, Dict, Tuple, Callable
from prettytable import from_csv
import re


def all_search(query: str):
shop_engines = [
fut_search,
free_search,
ram_search,
gammal_search
]
data = []
# TODO: Make this parallel
for engine in shop_engines:
with contextlib.suppress(Exception):
data.extend(engine(query))
return data

def gammal_search(query: str):
elgammal_search_url = "http://www.elgammalelectronics.com/Home/SearchResults?TheSearchParameter="
r = requests.get(elgammal_search_url + query)
Expand All @@ -16,18 +31,13 @@ def gammal_search(query: str):
table = soup.find('table', attrs={'class': 'gridtable'})
items = table.find_all('tr')
for item in items:
with contextlib.suppress(Exception):
cols = item.find_all('td')
cols = [ele.text.strip() for ele in cols]
link = item.find(attrs={'style': 'cursor:pointer;'}).get('id')
link = link.split("-")[1:]
link = "-".join(link)
link = f"http://www.elgammalelectronics.com/Products/Details/{link}"
cols.append(link)
data.append([ele for ele in cols if ele])
cols = item.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])
data = [item for item in data if item]
data = [[f"{item[0]} {item[1]}", float(
item[2]), item[-1]] for item in data]
# Line below is very ugly and I'm too lazy to do it properly
data = [[item[0].replace(",", " ").replace('"', '').replace("'", ""), float(
item[2]), "Al Gammal"] for item in data]
data.sort(key=itemgetter(1))
return data

Expand All @@ -51,9 +61,10 @@ def fut_search(query: str):
item_name = item.findChildren("h3")[0].text
item_price = item.findChildren("h4")[0].text
item_price = item_price.strip("LE ").replace(",", "")
item_price = re.findall(r"[-+]?\d*\.?\d+|\d+", item_price)[0]
link = item.findChildren("a")[0].get('href')
link = f"https://store.fut-electronics.com{link}"
data.append([item_name, float(item_price), link])
data.append([item_name,float(item_price), "Future"])
data.sort(key=itemgetter(1))
return data

Expand Down Expand Up @@ -82,8 +93,9 @@ def ram_search(query: str):
for result in results:
title = result.find(
'h2', attrs={'class': 'woocommerce-loop-product__title'}).text
price = result.find('bdi').text
data.append([title, price])
price = result.find('bdi').text.split("EGP")[1].replace(",", "")
data.append([title, float(price), "RAM"])
data.sort(key=itemgetter(1))
return data


Expand Down Expand Up @@ -111,8 +123,9 @@ def free_search(query: str):
'h2', attrs={'class': 'woocommerce-loop-product__title'}).text
price = result.find(
'span', attrs={'class': 'woocommerce-Price-amount amount'}).text
price = price.strip(u'\xa0EGP')
data.append([title, price])
price = price.strip(u'\xa0EGP').replace(",", '')
data.append([title, float(price), "Free"])
data.sort(key=itemgetter(1))
return data


Expand All @@ -135,8 +148,8 @@ def save_to_csv(self, filename: str) -> None:
from pandas import DataFrame
data = self.run()
# make sure it's two columns
data = [[item[0], item[1]] for item in data]
df = DataFrame(data, columns=['Name', 'Price'])
data = [[item[0], item[1], item[2]] for item in data]
df = DataFrame(data, columns=['Name', 'Price', 'Shop'])
df.to_csv(filename, index=False)


Expand All @@ -148,15 +161,16 @@ def main() -> None:
"Fut Electronics": fut_search,
"Ram-E-Shop": ram_search,
"Free Electronics": free_search,
"All Shops": all_search,
}

search_engine = iterfzf(
shop_engines,
multi=True
)
for engine in search_engine:
scarper = Scraper(shop_engine=shop_engines[engine], query=query)
scarper.save_to_csv(filename=f"{engine}_{query}.csv")
scraper = Scraper(shop_engine=shop_engines[engine], query=query)
scraper.save_to_csv(filename=f"{engine}_{query}.csv")
with open(f"{engine}_{query}.csv", 'r') as f:
print(from_csv(f))

Expand Down

0 comments on commit 6a63610

Please sign in to comment.