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

Support for Multiple Currencies and Currency Symbols #18

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
21 changes: 16 additions & 5 deletions src/google_flight_analysis/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

__all__ = ['Flight']

CURRENCY_SYMBOLS_LIST = ['₼', 'NT$', 'ج.س.', '₣', 'RD$', 'ден', '֏', '.د.ب', '₮', 'T$', 'Ξ', 'TT$', '₫', 'د.إ', 'Z$', 'ع.د', '₱', '₤', '$U', '₸', '$b', '€', '฿', 'R$', '₦', 'ƒ', 'BZ$', '£', '₨', '$', 'دج', '৳', '₽', '₾', 'Ł', 'MOP$', 'Kč', '¥', '₺', 'GH₵', '¥', 'B/.', 'zł', 'S/.', 'WS$', '₡', 'د.ت', 'Дин.', 'R₣', '₴', 'J$', '₹', '﷼', 'C$', 'Ƀ', '₭', '₪', '₩', 'лв', '₵', '؋', '៛']

class Flight:

Expand All @@ -21,6 +22,7 @@ def __init__(self, date, *args):
self._co2 = None
self._emissions = None
self._price = None
self._price_currency = None
self._times = []
self._time_leave = None
self._time_arrive = None
Expand Down Expand Up @@ -95,6 +97,10 @@ def emissions(self):
@property
def price(self):
return self._price

@property
def price_currency(self):
return self._price_currency

@property
def time_leave(self):
Expand All @@ -105,6 +111,12 @@ def time_arrive(self):
return self._time_arrive

def _classify_arg(self, arg):
if self._price_currency == None:
for symbol in CURRENCY_SYMBOLS_LIST:
if symbol in arg:
self._price_currency = symbol
break

if ('AM' in arg or 'PM' in arg) and len(self._times) < 2 and ':' in arg:
# arrival or departure time
delta = timedelta(days = 0)
Expand All @@ -121,17 +133,16 @@ def _classify_arg(self, arg):
elif 'stop' in arg and self._num_stops is None:
# num stops
self._num_stops = 0 if arg == 'Nonstop' else int(arg.split()[0])

elif arg.endswith('CO2') and self._co2 is None:
# co2
self._co2 = int(arg.split()[0])
elif arg.endswith('emissions') and self._emissions is None:
# emmision
emission_val = arg.split()[0]
self._emissions = 0 if emission_val == 'Avg' else int(emission_val[:-1])
elif '$' in arg and self._price is None:
elif self._price_currency != None and self._price_currency in arg and self._price is None:
# price
self._price = int(arg[1:].replace(',',''))
self._price = int(arg.replace(',','').replace(self._price_currency, '').replace('.', '').replace(' ', ''))
elif len(arg) == 6 and arg.isupper() and self._origin is None and self._dest is None:
# origin/dest
self._origin = arg[:3]
Expand Down Expand Up @@ -165,7 +176,7 @@ def dataframe(flights):
'Destination' : [],
'Airline(s)' : [],
'Travel Time' : [],
'Price ($)' : [],
'Price ({price_currency})'.format(price_currency=flights[0].price_currency) : [],
'Num Stops' : [],
'Layover' : [],
'Access Date' : [],
Expand All @@ -188,7 +199,7 @@ def dataframe(flights):
#data['Stop Location'] += [flight.stops]
data['CO2 Emission (kg)'] += [flight.co2]
data['Emission Diff (%)'] += [flight.emissions]
data['Price ($)'] += [flight.price]
data['Price ({price_currency})'.format(price_currency=flight.price_currency)] += [flight.price]
data['Access Date'] += [datetime.today().replace(hour = 0, minute = 0, second = 0, microsecond = 0)]

return pd.DataFrame(data)
Expand Down
3 changes: 2 additions & 1 deletion src/google_flight_analysis/scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ def _get_results(url, date, driver):

@staticmethod
def _clean_results(result, date):
res2 = [x.encode("ascii", "ignore").decode().strip() for x in result]
res2 = [x.encode("utf-8", "ignore").decode().strip() for x in result]
res2 = [x.replace('\u202f', '') for x in res2]

start = res2.index("Sort by:")+1
mid_start = res2.index("Price insights")
Expand Down