def ev_availability_by_availabilty_id(self, ids, availability_data_filename): filename = availability_data_filename if os.path.exists(filename): self.availability_data = ds.load_from_json(filename) self.get_charger_availability_by_availability_id(ids) ds.save_json(filename, self.availability_data)
def temporary_ev_availability(self, numbers, availability_data_filename): self.load_results_dict("uk_ev.json") filename = availability_data_filename if os.path.exists(filename): self.availability_data = ds.load_from_json(filename) self.get_charger_availability_by_index_number(numbers) ds.save_json(filename, self.availability_data)
def test_availability_of_each_poi_from_ids(self, poi_list, poi_dict, output_filename): if os.path.exists(output_filename): self.test_availability = ds.load_from_json(output_filename) else: self.test_availability = {} for poi_id in poi_list: if poi_id not in self.test_availability: try: charging_availability_id = poi_dict[poi_id]["dataSources"][ "chargingAvailability"]["id"] url = f"https://{self.base_url}/search/2/chargingAvailability.{self.data_type}?key={self.key}&chargingAvailability={charging_availability_id}" search_request = self.s.get(url) if search_request.status_code == 403: print( "Key not accepted, most likely out of transactions" ) ds.save_json(output_filename, self.test_availability) return self.test_availability else: self.test_availability[poi_id] = { "poi_id": poi_id, "charging_availability_id": charging_availability_id, "availability_data": search_request.json() } except KeyError: print( f"No Charging Availability id for station with id {poi_id}, moving to the next id" ) ds.save_json(output_filename, self.test_availability) return self.test_availability
def search_all_nearbies_for_petrol_station(self, radius, ids, poi_dict, output_filename): n = 0 for id in ids: self.search_nearby_for_petrol(radius, id, poi_dict) time.sleep(0.5) if n % 50 == 0: print(f"Done {n} out of {len(ids)}") ds.save_json(output_filename, self.nearby_results) n += 1
def get_total_by_brand(self, brands: list): self.failed_brand_search = [] self.total_by_brand = {} for brand in brands: ofset = 0 url = f"https://api.tomtom.com/search/2/categorySearch/{self.query}.{self.data_type}?&countrySet={self.country}&categorySet={self.poi_code}&brandSet={brand}&ofs={ofset}&limit=100&key={self.key}" search_request = self.s.get(url) total = search_request.json()["summary"]["totalResults"] self.total_by_brand[brand] = total print(brand + ": " + str(total) + "pois") for ofset in range(0, total, 100): print(ofset) url_petrol = f"https://api.tomtom.com/search/2/categorySearch/{self.query}.{self.data_type}?&countrySet={self.country}&brandSet={brand}&categorySet={self.poi_code}&ofs={ofset}&limit=100&key={self.key}" search_request = self.s.get(url_petrol) time.sleep(1) try: self.poi_results.extend(search_request.json()["results"]) except: try: print("error at: " + str(ofset) + " trying again") time.sleep(1) search_request = self.s.get(url_petrol) self.poi_results.extend( search_request.json()["results"]) except: self.failed_brand_search.append(ofset) print("request " + str(ofset) + " failed, added to failed list") self.ids = [a["id"] for a in self.poi_results] self.results_dict = ds.list_to_dict_with_key(self.poi_results, "id") if len(brands) == 1: filename = str(self.country) + "_" + str(brands[0]) + "_" + str( self.poi_code) + ".json" else: filename = str(self.country) + "_" + str(self.poi_code) + ".json" ds.save_json(filename, self.results_dict)
def check_poi_for_nearby_petrol(self, ev_dict, ids_to_test, output_filename): self.search_all_nearbies_for_petrol_station(25, ids_to_test, self.poi_results) ds.save_json(output_filename, self.nearby_results)
filename = brand + "_Detailed_Petrol.json" else: filename = json.split(".")[0] + "_Detailed_Petrol.json" return filename if __name__ == "__main__": api = PetrolTomTomApi() if mode == "Brand": if brand == None: print("No Brand selected, enter brand") brand = str(input()) api.get_total_by_brand(brand) filename = brand + ".json" ds.save_json(filename, api.results_dict) else: if json == None: if brand == None: print("No json selected, enter what brand to search first") brand = str(input()) api.get_total_by_brand([brand]) else: api.results_dict = api.load_petrol_stations(json) api.ids = [a for a in api.results_dict] if mode == "Detailed": if detailed == None: api.search_all_detailed(api.ids) filename = name_json() ds.save_json(filename, api.detailed_results)