Пример #1
0
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'])
Пример #2
0
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