Skip to content

Commit

Permalink
Merge pull request #16 from TommasU/parth
Browse files Browse the repository at this point in the history
Added support for dynamic currencies. Merging PR due to limited time.
  • Loading branch information
TommasU authored Nov 30, 2021
2 parents 32bdabc + 6ce8b1b commit de89c8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

from datetime import datetime
import math
import requests
from ast import literal_eval

CURRENCY_URL = "https://api.exchangerate-api.com/v4/latest/usd"
EXCHANGES = literal_eval(requests.get(CURRENCY_URL).text)

def formatResult(website, titles, prices, links,ratings,df_flag, currency):
"""
Expand Down Expand Up @@ -54,11 +59,11 @@ def sortList(arr, sortBy, reverse):
Returns- Sorted list of the products based on the parameter requested by the user
"""
if sortBy == "pr":
return sorted(arr, key=lambda x: getNumbers(x["price"]), reverse=reverse)
return arr.sort_values(key=lambda x: x.apply(lambda y: getNumbers(y)), by=["price"], ascending=False)
# Fix Rating sort
elif sortBy == "ra":
return sorted(arr, key = lambda x:float(x["rating"]), reverse=reverse)
pass
arr["rating"] = arr["rating"].apply(lambda x: None if x == '' else float(x))
return arr.sort_values(by=["rating"], ascending=False)
return arr

def formatSearchQuery(query):
Expand Down Expand Up @@ -86,7 +91,7 @@ def getNumbers(st):
try:
ans = float(ans)
except:
ans = math.inf
ans = 0
return ans

def getCurrency(currency, price):
Expand All @@ -98,16 +103,16 @@ def getCurrency(currency, price):
converted_cur = 0.0
if len(price)>1 :
if currency == "inr":
converted_cur = 75 * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
converted_cur = EXCHANGES["rates"]["INR"] * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
elif currency == "euro":
converted_cur = 1.16 * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
converted_cur = EXCHANGES["rates"]["EUR"] * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
elif currency == "aud":
converted_cur = 1.34 * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
converted_cur = EXCHANGES["rates"]["AUD"] * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
elif currency == "yuan":
converted_cur = 6.40 * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
converted_cur = EXCHANGES["rates"]["CNY"] * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
elif currency == "yen":
converted_cur = 114.21 * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
converted_cur = EXCHANGES["rates"]["JPY"] * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
elif currency == "pound":
converted_cur = 0.74 * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
converted_cur=currency.upper()+' '+str(converted_cur)
converted_cur = EXCHANGES["rates"]["GBP"] * int(price[(price.index("$")+1):price.index(".")].replace(",",""))
converted_cur=currency.upper()+' '+str(round(converted_cur, 2))
return converted_cur
2 changes: 2 additions & 0 deletions src/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def driver(product, currency, num=None, df_flag=0,csv=False,cd=None,ui=False,sor
p["link"] = link

if sort != None:
result_condensed = pd.DataFrame(result_condensed)
if sort == "rades":
result_condensed = formatter.sortList(result_condensed, "ra", False)
elif sort == "raasc":
Expand All @@ -207,6 +208,7 @@ def driver(product, currency, num=None, df_flag=0,csv=False,cd=None,ui=False,sor
result_condensed = formatter.sortList(result_condensed, "pr", False)
else:
result_condensed = formatter.sortList(result_condensed, "pr", True)
result_condensed = result_condensed.to_dict(orient='records')

if csv:
file_name = product + "_" + datetime.now() + ".csv"
Expand Down

0 comments on commit de89c8f

Please sign in to comment.