Пример #1
0
    def _parse_route(self, content: dict, name: str) -> typing.List[Route]:
        routes = []
        # 用于记录在请求中找到的对应的线路信息的次数
        flag = 0
        for item in content['items']:
            # 一条公交线路最多有正向和反向,当正反向同时找到时,停止遍历
            if flag == 2:
                break
            for route in item['routes']:
                origin = route['origin']
                terminal = route['terminal']
                for tmp in self.stops_dict[name]:
                    if origin == tmp[0] and terminal == tmp[-1]:
                        flag += 1
                        route_id = route['routeId']
                        # 如果获取到的线路的 id 在 route_ids 里面,则跳过,剩下的全部插入到数据库中
                        if route_id in self.route_ids:
                            continue
                        else:
                            self.route_ids.append(route_id)
                        a_route = Route()
                        a_route.route_id = route_id
                        if 'oppositeId' in route:
                            a_route.opposite_id = route['oppositeId']
                        if 'amapId' in route:
                            a_route.amap_id = route['amapId']
                        a_route.name = route['routeName']
                        a_route.raw_name = name
                        a_route.origin = route['origin']
                        a_route.terminal = route['terminal']
                        a_route.has_gps = route['hasGps']
                        routes.append(a_route)

        return routes