def export_whole_month(depart=None, destination=None, date=None): """ Fetch data from SkyScanner.com for destination's airport from departure's airport of the selected month :param depart: The airport that the flight depart :type depart: Airport :param destination: The destination of the flight :type destination: Airport :param date: Date of the required month for fetch :type date: datetime :return list of flights of this month :rtype: list[Flight] """ selected_month = date.strftime('%Y-%m') Path( os.path.dirname(__file__) + '/../../Data/Flights/Whole Month/' + selected_month).mkdir(parents=True, exist_ok=True) Path( os.path.dirname(__file__) + '/../../Data/Flights/Whole Month/' + selected_month + '/' + destination.name).mkdir(parents=True, exist_ok=True) request_whole_month_url = DataManager.SkyScanner_whole_month_request.format( depart=depart.code, destination=destination.code, selected_month=selected_month) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } request = requests.get(url=request_whole_month_url, headers=headers) data_hash = request.json() data = data_hash["PriceGrids"]["Grid"] flights = [] i = 0 for list_i in data: j = 0 for dict_j in list_i: if "Direct" in dict_j.keys(): price = dict_j["Direct"]["Price"] day_depart = ("0" if (j + 1) < 10 else "") + str(j + 1) day_return = ("0" if (i + 1) < 10 else "") + str(i + 1) depart_date = selected_month + '-' + day_depart return_date = selected_month + '-' + day_return flight = Flight(flying_out=depart, flying_back=destination, flying_out_date=depart_date, flying_back_date=return_date, price_per_adult=price, source_site="SkyScanner") flights.append(flight) j = j + 1 i = i + 1 general.update_json_files(flights=flights, year_month_date_depart=selected_month, destination=destination) return flights
def load_next_label_file(): with open(os.path.dirname(__file__) + '/../Data/Flights/json_files.json') as f: json_files = json.load(f) for json_file in json_files: flights_data = [] with open(os.path.dirname(__file__) + "/../" + json_file) as f: data = json.load(f) for temp in data: flights_data.append(Flight(**temp)) for flight in flights_data: if flight.label == (-1): return flights_data, json_file return None, None
def get_flights_data_from_json_file(json_file): """ get the data of flights from this json as list of flights :param json_file:the path of json file that contains flights :type json_file: str :return:flights data from this json :rtype list[Flight] """ flights_data = [] with open(os.path.dirname(__file__) + "/../" + json_file) as f: data = json.load(f) for temp in data: flights_data.append(Flight(**temp)) return flights_data
def get_data_by_name(name, add_json_file=False): """ :param add_json_file: decide if return only flight or append him his json path,by default is just return flight list :type add_json_file: bool :param name: The shortcut of the airport :type name: str :return: list of all flights of destination if add_json_file is true return list of [...,(flight,json_path),....] else(by default) return list of [....,flight,....] :rtype: list[Flight] """ files_lst = get_files_list_of_location(name) flights_data = [] for json_file in files_lst: with open(os.path.dirname(__file__) + "/../" + json_file) as f: data = json.load(f) for temp in data: if not add_json_file: flights_data.append(Flight(**temp)) else: flights_data.append((Flight(**temp), json_file)) return flights_data
def get_updated_data_from_json_file(): """ get all the flights of all destinations in that that fetched today-the most updated flights the data load from most_updated_flights , its alternative for get_all_updated_data :return: list of updated flights of destination :rtype: list[Flight] """ with open(os.path.dirname(__file__) + "\\..\\Data\\Flights\\most_updated_flights.json", 'r', encoding='utf-8') as f: flights_data_json = json.load(f) flights_data = [] for flight_json in flights_data_json: flights_data.append(Flight(**flight_json)) return flights_data
def export_whole_months(depart=None, destination=None): """ Fetch data from Easyjet.com for destination's airport from departure's airport :param depart: The airport that the flight depart :type depart: Airport :param destination: The destination of the flight :type destination: Airport :return return list of flights from departure airport to destination airport :rtype list[Flight] """ whole_month_url = DataManager.Easyjet_whole_month_request date_str = datetime.today().strftime('%Y-%m-%d') request_whole_month_url = whole_month_url.format( depart=depart.code, destination=destination.code, depart_date=date_str) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/80.0.3987.149 Safari/537.36' } request = requests.get(url=request_whole_month_url, headers=headers) data_depart_hash = request.json() request_whole_month_url = whole_month_url.format(depart=destination.code, destination=depart.code, depart_date=date_str) request = requests.get(url=request_whole_month_url, headers=headers) data_return_hash = request.json() data_depart = data_depart_hash["months"] data_return = data_return_hash["months"] try: year_month_day_date_return_str = str( data_return[len(data_return) - 1]["year"]) + '-' + DataManager.month_string_to_number( data_return[len(data_return) - 1]["monthDisplayName"]) + '-' \ + str(len(data_return[len(data_return) - 1]['days'])) except Exception: #print(f'EasyJet missing flight to -{destination}') return list() return_max_date_datetime = datetime.strptime( year_month_day_date_return_str, '%Y-%m-%d') flights = [] flights_data = [] for month in data_depart: year_month_date_depart = str( month["year"]) + '-' + DataManager.month_string_to_number( month["monthDisplayName"]) days = month['days'] day_index = 0 for day in days: if not day['lowestFare'] is None: price_depart = day['lowestFare'] day_depart = ("0" if (day_index + 1) < 10 else "") + str(day_index + 1) selected_date_depart_str = year_month_date_depart + '-' + day_depart potential_days_return_list = [] selected_date_depart_datetime = datetime.strptime( selected_date_depart_str, '%Y-%m-%d') for j in range(3, 7): potential_days_return_list.append( selected_date_depart_datetime + relativedelta(days=j)) for potential_day_return in potential_days_return_list: day_return = int(potential_day_return.strftime("%d")) - 1 month_return_str = DataManager.month_dict[ potential_day_return.strftime("%m")] month_return = 0 for return_temp in data_return: if return_temp['monthDisplayName'] == month_return_str: break month_return = month_return + 1 selected_date_return_str = datetime.strftime( potential_day_return, '%Y-%m-%d') if potential_day_return < return_max_date_datetime: if not data_return[month_return]['days'][day_return][ 'lowestFare'] is None: price_return = data_return[month_return]['days'][ day_return]['lowestFare'] total_price = exchange_rate * (price_depart + price_return) flight = Flight( flying_out=depart, flying_back=destination, flying_out_date=selected_date_depart_str, flying_back_date=selected_date_return_str, price_per_adult=total_price, source_site='Easyjet') flights.append(flight) day_index = day_index + 1 if len(flights) != 0: general.update_json_files( flights=flights, year_month_date_depart=year_month_date_depart, destination=destination) flights_data.extend(flights) flights = [] return flights_data
def export_whole_month_all_dest(): """ Fetch data from Wizzair.com for all the detentions from TLV, parse him to Flight format and save the data as json in Data\\Flights folder. """ flights_data = fetch_data() destinations = DataManager.destination_list_wizzair departs = DataManager.depart_list flights_data_most_updated = [] for depart in departs: depart_flight = Airport(depart) t_progress_bar_destination = tqdm(destinations, leave=True) for destination in t_progress_bar_destination: destination_flight = Airport(destination) depart_destination_list = [] destination_depart_list = [] t_progress_bar_destination.set_description("Wizzair processing " + destination_flight.name) t_progress_bar_destination.refresh() for flight_data in flights_data: if flight_data["departureStation"] == depart_flight.code and \ flight_data["arrivalStation"] == destination_flight.code: depart_destination_list.append(flight_data) elif flight_data["departureStation"] == destination_flight.code \ and flight_data["arrivalStation"] == depart_flight.code: destination_depart_list.append(flight_data) for flight in depart_destination_list: flight["departureDate"] = flight["departureDate"][0:10] for flight in destination_depart_list: flight["departureDate"] = flight["departureDate"][0:10] combination_flights = [] for item_depart in depart_destination_list: for item_return in destination_depart_list: combination_flights.append( Flight(flying_out=depart_flight, flying_back=destination_flight, flying_out_date=item_depart["departureDate"], flying_back_date=item_return["departureDate"], price_per_adult=item_depart["price"]["amount"] + item_return["price"]["amount"], source_site="Wizzair")) flights_filter = [ flight for flight in combination_flights if 3 <= flight.days < 7 ] flights_per_year_month_dict = {} if len(flights_filter) != 0: flights_data_most_updated.extend(flights_filter) for flight_item in flights_filter: depart_date_dt = datetime.strptime(flight_item.depart_date, '%Y-%m-%d') if datetime.strftime(depart_date_dt, "%Y-%m") in flights_per_year_month_dict: flights_per_year_month_dict[datetime.strftime( depart_date_dt, "%Y-%m")].append(flight_item) else: flights_per_year_month_dict[datetime.strftime( depart_date_dt, "%Y-%m")] = [] flights_per_year_month_dict[datetime.strftime( depart_date_dt, "%Y-%m")].append(flight_item) # update json files for key in flights_per_year_month_dict: general.update_json_files( flights=flights_per_year_month_dict[key], year_month_date_depart=key, destination=destination_flight) general.update_most_updated_flights(flights_data_most_updated)