diff --git a/[working-with-apis] Laura.ipynb b/[working-with-apis] Laura.ipynb new file mode 100644 index 0000000..469cad8 --- /dev/null +++ b/[working-with-apis] Laura.ipynb @@ -0,0 +1,197 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "42a5829f", + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "import time\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "1ceed0ba", + "metadata": {}, + "outputs": [], + "source": [ + "data = {'Quotes': [{'QuoteId': 1,\n", + " 'MinPrice': 92.0,\n", + " 'Direct': False,\n", + " 'OutboundLeg': {'CarrierIds': [1065],\n", + " 'OriginId': 81727,\n", + " 'DestinationId': 50290,\n", + " 'DepartureDate': '2020-12-12T00:00:00'},\n", + " 'QuoteDateTime': '2020-09-21T10:49:00'},\n", + " {'QuoteId': 2,\n", + " 'MinPrice': 133.0,\n", + " 'Direct': True,\n", + " 'OutboundLeg': {'CarrierIds': [851],\n", + " 'OriginId': 81727,\n", + " 'DestinationId': 50290,\n", + " 'DepartureDate': '2020-12-12T00:00:00'},\n", + " 'QuoteDateTime': '2020-09-21T10:49:00'}],\n", + " 'Places': [{'PlaceId': 50290,\n", + " 'IataCode': 'EWR',\n", + " 'Name': 'New York Newark',\n", + " 'Type': 'Station',\n", + " 'SkyscannerCode': 'EWR',\n", + " 'CityName': 'New York',\n", + " 'CityId': 'NYCA',\n", + " 'CountryName': 'United States'},\n", + " {'PlaceId': 60987,\n", + " 'IataCode': 'JFK',\n", + " 'Name': 'New York John F. Kennedy',\n", + " 'Type': 'Station',\n", + " 'SkyscannerCode': 'JFK',\n", + " 'CityName': 'New York',\n", + " 'CityId': 'NYCA',\n", + " 'CountryName': 'United States'},\n", + " {'PlaceId': 65633,\n", + " 'IataCode': 'LGA',\n", + " 'Name': 'New York LaGuardia',\n", + " 'Type': 'Station',\n", + " 'SkyscannerCode': 'LGA',\n", + " 'CityName': 'New York',\n", + " 'CityId': 'NYCA',\n", + " 'CountryName': 'United States'},\n", + " {'PlaceId': 81727,\n", + " 'IataCode': 'SFO',\n", + " 'Name': 'San Francisco International',\n", + " 'Type': 'Station',\n", + " 'SkyscannerCode': 'SFO',\n", + " 'CityName': 'San Francisco',\n", + " 'CityId': 'SFOA',\n", + " 'CountryName': 'United States'}],\n", + " 'Carriers': [{'CarrierId': 819, 'Name': 'Aegean Airlines'},\n", + " {'CarrierId': 851, 'Name': 'Alaska Airlines'},\n", + " {'CarrierId': 870, 'Name': 'jetBlue'},\n", + " {'CarrierId': 1065, 'Name': 'Frontier Airlines'},\n", + " {'CarrierId': 1721, 'Name': 'Sun Country Airlines'},\n", + " {'CarrierId': 1793, 'Name': 'United'},\n", + " {'CarrierId': 1902, 'Name': 'Southwest Airlines'}],\n", + " 'Currencies': [{'Code': 'USD',\n", + " 'Symbol': '$',\n", + " 'ThousandsSeparator': ',',\n", + " 'DecimalSeparator': '.',\n", + " 'SymbolOnLeft': True,\n", + " 'SpaceBetweenAmountAndSymbol': False,\n", + " 'RoundingCoefficient': 0,\n", + " 'DecimalDigits': 2}]}" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "bdac56ed", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'QuoteId': 2,\n", + " 'MinPrice': 133.0,\n", + " 'Direct': True,\n", + " 'OutboundLeg': {'CarrierIds': [851],\n", + " 'OriginId': 81727,\n", + " 'DestinationId': 50290,\n", + " 'DepartureDate': '2020-12-12T00:00:00'},\n", + " 'QuoteDateTime': '2020-09-21T10:49:00'}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data['Quotes'][1]" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "a171f12e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Date': '2020-12-12',\n", + " 'Price': 92.0,\n", + " 'Origin': 'San Francisco International',\n", + " 'Destination': 'New York Newark',\n", + " 'Company': 'Frontier Airlines'}" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def flight_search(flights, date1, date2):\n", + "\n", + " cheapest_flight = None\n", + " \n", + " for quote in data['Quotes']:\n", + " departure_date = quote['OutboundLeg']['DepartureDate'][:10]\n", + " if departure_date == date1 or departure_date == date2:\n", + " if cheapest_flight is None or quote['MinPrice'] < cheapest_flight['MinPrice']:\n", + " cheapest_flight = quote\n", + "\n", + " if cheapest_flight:\n", + " carrier_name = next((carrier['Name'] for carrier in data['Carriers'] if carrier['CarrierId'] in cheapest_flight['OutboundLeg']['CarrierIds']), \"Unknown\")\n", + " origin_name = next((place['Name'] for place in data['Places'] if place['PlaceId'] == cheapest_flight['OutboundLeg']['OriginId']), \"Unknown\")\n", + " destination_name = next((place['Name'] for place in data['Places'] if place['PlaceId'] == cheapest_flight['OutboundLeg']['DestinationId']), \"Unknown\")\n", + "\n", + "\n", + " return {\n", + " 'Date': departure_date,\n", + " 'Price': cheapest_flight['MinPrice'],\n", + " 'Origin': origin_name,\n", + " 'Destination': destination_name,\n", + " 'Company': carrier_name\n", + " }\n", + "\n", + " return \"No flights found for the given dates.\"\n", + "\n", + "cheapest_flight = flight_search(data, '2020-12-11', '2020-12-12')\n", + "cheapest_flight" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ac1c3e8c", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}