Skip to content

Commit

Permalink
implement analytics logging for flight data retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
atxtechbro committed Dec 30, 2023
1 parent 53aebf9 commit fa58865
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# flightradar24 #
### Display Top 10 Watched Flights from flightradar24.com
```
$ fr.py
$ flight_data_retriever.py
```
#### Example of query on Sunday, February 20, 2022

Expand Down
Binary file not shown.
24 changes: 22 additions & 2 deletions src/flight_data_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ def __init__(self, api_endpoint, headers):

def retrieve_flight_data(self):
logging.info('Sending request to %s', self.api_endpoint)
response = requests.get(self.api_endpoint, headers=self.headers)
try:
response = requests.get(
self.api_endpoint,
headers=self.headers,
timeout=5
)
except requests.Timeout:
logging.error('Request to %s timed out', self.api_endpoint)
return None

response.raise_for_status() # Raise HTTPError for bad responses

json_data = json.loads(response.text)
Expand All @@ -25,6 +34,17 @@ def retrieve_flight_data(self):
logging.info('Successfully retrieved flight data')
return flight_data

def analyze_flight_data(self):
flight_data = self.retrieve_flight_data()

most_common_departure_city = flight_data['from_city'].mode().get(0)
most_common_arrival_city = flight_data['to_city'].mode().get(0)

logging.info("The most common departure city is: %s",
most_common_departure_city)
logging.info("The most common arrival city is: %s",
most_common_arrival_city)


# Usage
api_endpoint = "https://www.flightradar24.com/flights/most-tracked"
Expand All @@ -35,4 +55,4 @@ def retrieve_flight_data(self):
}

flight_data_retriever = FlightDataRetriever(api_endpoint, headers)
flight_data = flight_data_retriever.retrieve_flight_data()
flight_data_retriever.analyze_flight_data()
7 changes: 7 additions & 0 deletions tests/test_flight_data_retriever.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import os
import sys
import unittest
from unittest.mock import MagicMock, patch


sys.path.insert(0, os.path.abspath(
os.path.join(os.path.dirname(__file__), '..')
))

from src.flight_data_retriever import FlightDataRetriever


Expand Down

0 comments on commit fa58865

Please sign in to comment.