class FlightParser: def __init__(self, path): self._path = path self._city = {} self._airport = {} self._db = Database() def parse_and_insert(self): with open(self._path, newline='') as csv_file: self._db.open_transaction() reader = csv.reader(csv_file, delimiter=',', quotechar='"') for row in reader: try: org_airport_code = row[0] dst_airport_code = row[1] org_city_name = row[2] dst_city_name = row[3] passengers = int(row[4]) seats = int(row[5]) flights = int(row[6]) distance = int(row[7]) fly_data = row[8] org_city_population = int(row[9]) dst_city_population = int(row[10]) org_airport_lat = float(row[11]) org_airport_lng = float(row[12]) dst_airport_lat = float(row[13]) dst_airport_lng = float(row[14]) self._insert_city(org_city_name, org_city_population) self._insert_city(dst_city_name, dst_city_population) self._insert_airport(org_airport_code, org_airport_lat, org_airport_lng) self._insert_airport(dst_airport_code, dst_airport_lat, dst_airport_lng) self._db.insert_flight(self._city[org_city_name], self._airport[org_airport_code], self._city[dst_city_name], self._airport[dst_airport_code], passengers, seats, flights, distance, fly_data) except ValueError as error: # print("ValueError:", row) print(error) continue self._db.close_transaction() def _insert_city(self, name, population): idx = self._db.insert_city(name, population) if idx is not None: self._city[name] = idx def _insert_airport(self, code, lat, lng): idx = self._db.insert_airport(code, lat, lng) if idx is not None: self._airport[code] = idx
class GraphBuilder: def __init__(self, from_date, to_date): self._graph = nx.DiGraph() self._db = Database() self._from_date = from_date self._to_date = to_date def build(self): self._db.open_transaction() self._cities() self._flights() self._db.close_transaction() return self._graph def _cities(self): cities = self._db.get_all_cities() for city in cities: self._graph.add_node(city['id'], name=city['name'], population=city['population']) def _flights(self): flights = self._db.get_flights(self._from_date, self._to_date) for flight in flights: self._graph.add_edge(flight['from'], flight['to'], weight=flight['passengers'])
def _get_possible_poi_values(self): db = Database() db.open_transaction() result = {} for tag in OSMQuery.tag_keys_types: r = db.get_poi_values(tag) r = [self._ALL] + r result[tag] = r db.close_transaction() return result
class PoiUpdater: def __init__(self, radius: int): self._radius = radius self._db = Database() self._osm = OSM() def update(self): self._db.open_transaction() cities = self._db.get_all_cities() city_local_geo = self._db.get_all_cities_one_airport_geo() for city in cities: if self._db.count_poi(city['id']) != 0: continue c = city['name'].split(", ") city_name = c[0] print(city_name + ' ', end='', flush=True) pois = self._osm.get_pois(city_name, city_local_geo[city['id']], self._radius) print(len(pois)) if len(pois) == 0: print("Sth gone wrong:", city) for poi in pois: self._db.insert_poi(city['id'], poi) # just in case, try to prevent ban time.sleep(random.uniform(0.1, 1)) self._db.close_transaction() def invalid_cities(self): self._db.open_transaction() cities = self._db.get_all_cities() invalid_city = [] for city in cities: if self._db.count_poi(city['id']) == 0: invalid_city.append(city) self._db.close_transaction() return invalid_city