Skip to content

Commit 721bb10

Browse files
Update Day-39
1 parent 51ad422 commit 721bb10

File tree

6 files changed

+170
-0
lines changed

6 files changed

+170
-0
lines changed

Day-39-FlightFinderV1/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Flight Finder V1
2+
3+
### APIs Required
4+
5+
- Google Sheet Data Management
6+
7+
- Kiwi Partners Flight Search API
8+
9+
<img src= 'https://user-images.githubusercontent.com/65078610/107887388-cab3f400-6f2b-11eb-93a6-95f296321214.PNG' width="350">

Day-39-FlightFinderV1/data_manager.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#This class is responsible for talking to the Google Sheet.
2+
import os
3+
import json
4+
import requests
5+
from dotenv import load_dotenv
6+
load_dotenv()
7+
8+
class DataManager:
9+
10+
def __init__(self):
11+
self.sheety_endpoint = "https://api.sheety.co/126f9e38bb1fc18f122cb26e242ee654/flightDeals/prices"
12+
self.headers = {
13+
'Authorization': os.getenv('AUTH_TOKEN')
14+
}
15+
16+
def get_data(self):
17+
response = requests.get(url=self.sheety_endpoint, headers=self.headers)
18+
data = json.loads(response.text)['prices']
19+
# data = [
20+
# {
21+
# "city": "Kolkata",
22+
# "iataCode": "CCU",
23+
# "lowestPrice": 3345,
24+
# "id": 12
25+
# }
26+
# ]
27+
return data
28+
29+
def update_data(self, row_id, new_price):
30+
sheety_update_endpoint = f"{self.sheety_endpoint}/{row_id}"
31+
price_config = {
32+
'price': {
33+
"lowestPrice": round(new_price)
34+
}
35+
}
36+
response = requests.put(url=sheety_update_endpoint, json=price_config , headers=self.headers)
37+
print(response.text)

Day-39-FlightFinderV1/flight_data.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#This class is responsible for structuring the flight data.
2+
class FlightData:
3+
4+
def __init__(self):
5+
pass
6+
7+
def compare(self, price_data, lowest_price):
8+
# Normalize API price hike
9+
lowest_price += lowest_price*14.8/100
10+
self.price_data = price_data
11+
self.price_array = []
12+
self.date_array = []
13+
for data in self.price_data:
14+
# print(f"{data['price']} {(data['route'][0]['local_arrival'])[:10]}")
15+
self.price_array.append(int(data['price']))
16+
self.date_array.append((data['route'][0]['local_arrival'])[:10])
17+
self.min_price = min(self.price_array)
18+
return self.min_price < lowest_price
19+
20+
def get_data(self):
21+
low_limit = self.price_array.index(self.min_price)
22+
self.price_array.reverse()
23+
high_limit = self.price_array.index(self.min_price)
24+
self.min_price -= self.min_price*14.8/100
25+
flight_details = {
26+
'price': round(self.min_price),
27+
'cityFrom': self.price_data[0]['route'][0]['cityFrom'],
28+
'cityCodeFrom': self.price_data[0]['route'][0]['cityCodeFrom'],
29+
'cityTo': self.price_data[0]['route'][0]['cityTo'],
30+
'cityCodeTo': self.price_data[0]['route'][0]['cityCodeTo'],
31+
'dateFrom': self.date_array[low_limit],
32+
'dateTo': ''
33+
}
34+
self.date_array.reverse()
35+
flight_details['dateTo'] = self.date_array[high_limit]
36+
return flight_details
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#This class is responsible for talking to the Flight Search API.
2+
from dotenv import load_dotenv
3+
from datetime import *
4+
import requests
5+
import os
6+
7+
load_dotenv()
8+
FLY_FROM = 'BOM'
9+
CURRENCY = 'INR'
10+
FLIGHT_TYPE = 'oneway'
11+
SORT_BY = 'date'
12+
13+
class FlightSearch:
14+
15+
def __init__(self):
16+
self.from_date =(datetime.now()+timedelta(days=1)).strftime('%d/%m/%Y')
17+
self.to_date =(datetime.now()+timedelta(days=180)).strftime('%d/%m/%Y')
18+
self.kiwi_endpoint = "https://tequila-api.kiwi.com/v2/search/"
19+
self.headers = {
20+
'apikey': os.getenv('API_KEY')
21+
}
22+
self.parameters = {
23+
'fly_from': FLY_FROM,
24+
'fly_to': '',
25+
'date_from': self.from_date,
26+
'date_to': self.to_date,
27+
'curr': CURRENCY,
28+
'flight_type': FLIGHT_TYPE,
29+
'one_per_date': 1,
30+
'sort': SORT_BY
31+
}
32+
33+
def get_data(self, fly_to):
34+
self.parameters['fly_to'] = fly_to
35+
response = requests.get(url=self.kiwi_endpoint, params=self.parameters, headers=self.headers)
36+
data = response.json()['data']
37+
return data

Day-39-FlightFinderV1/main.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#This file will need to use the DataManager,FlightSearch, FlightData, NotificationManager classes to achieve the program requirements.
2+
from notification_manager import NotificationManager
3+
from flight_search import FlightSearch
4+
from flight_data import FlightData
5+
from data_manager import DataManager
6+
7+
notification_manager = NotificationManager()
8+
flight_search = FlightSearch()
9+
flight_data = FlightData()
10+
data_manger = DataManager()
11+
destination_list = data_manger.get_data()
12+
13+
for city in destination_list:
14+
city_id = city['id']
15+
lowest_price = city['lowestPrice']
16+
fly_to = city['iataCode']
17+
price_data = flight_search.get_data(fly_to)
18+
is_cheap_deal = flight_data.compare(price_data, lowest_price)
19+
if is_cheap_deal:
20+
flight_details = flight_data.get_data()
21+
data_manger.update_data(city_id, flight_data.min_price)
22+
notification_manager.send_alert(flight_details)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#This class is responsible for sending notifications with the deal flight details.
2+
from dotenv import load_dotenv
3+
import smtplib
4+
import os
5+
6+
load_dotenv()
7+
my_email = os.getenv('EMAIL')
8+
to_email = os.getenv('TO_EMAIL')
9+
my_password = os.getenv('PASSWORD')
10+
11+
class NotificationManager:
12+
13+
def __init__(self):
14+
pass
15+
16+
def send_alert(self, data):
17+
with smtplib.SMTP(host="smtp.gmail.com", port=587) as connection:
18+
connection.starttls()
19+
connection.login(my_email, my_password)
20+
connection.sendmail(
21+
from_addr=my_email,
22+
to_addrs=to_email,
23+
msg=("Subject:Cheap Fare Alert!\n\n"+
24+
"Low price alert!\n"+
25+
f"Only Rs.{data['price']} "+
26+
f"to fly from {data['cityFrom']}-{data['cityCodeFrom']} "+
27+
f"to {data['cityTo']}-{data['cityCodeTo']},\n"+
28+
f"from {data['dateFrom']} to {data['dateTo']}")
29+
)

0 commit comments

Comments
 (0)