Exemple #1
0
    def from_nextbus(cls, agency_tag, route_tag):
        logging.info("from_nextbus@Vehicle...")

        if route_tag:
            etree = url.fetch_nextbus_url({ "a" : agency_tag, "command" : "vehicleLocations", "r" : route_tag })
        else:
            etree = url.fetch_nextbus_url({ "a" : agency_tag, "command" : "vehicleLocations" })

        vehicles = []
        for elem in etree.findall("vehicle"):
            vehicle_id = elem.get("id")
            route_tag = elem.get("routeTag")
            dir_tag = elem.get("dirTag")
            location = ndb.GeoPt(float(elem.get("lat")), float(elem.get("lon")))
            secs_since_report = int(elem.get("secsSinceReport"))
            predictable = bool(elem.get("predictable"))
            heading = int(elem.get("heading"))
            try:
                speedKmHr = float(elem.get("speedKmHr"))
            except:
                speedKmHr = 0
            v = Vehicle(id = vehicle_id, vehicle_id = vehicle_id, route_tag = route_tag, dir_tag = dir_tag, location = location, secs_since_report = secs_since_report, predictable = predictable, heading= heading, speedKmHr = speedKmHr)
            vehicles.append(v)

        try:
            ndb.put_multi(vehicles, max_entity_groups_per_rpc = 16)
        except CapabilityDisabledError:
            # fail gracefully here
            pass

        return vehicles
Exemple #2
0
    def from_nextbus(cls, agency_tag, route_tag, direction_tag, stop_tag):
        logging.info("from_nextbus@Estimation...")

        etree = url.fetch_nextbus_url({ "a" : agency_tag, "command" : "predictions", "s" : stop_tag, "r" : route_tag.replace("__", " ")})

        prediction_keys = [prediction.key for prediction in Prediction.from_nextbus(etree, agency_tag, route_tag, direction_tag, stop_tag)]

        estimations = []
        for elem in etree.findall("predictions"):
            route_tag = elem.get("routeTag")
            stop_tag = elem.get("stopTag")
            agency_title = elem.get("agencyTitle")
            route_title = elem.get("routeTitle")
            stop_title =  elem.get("stopTitle")
            key = "%s@%s@%s@%s" % (agency_tag, route_tag, direction_tag, stop_tag)
            e = Estimation(id = key, route_tag = route_tag, stop_tag = stop_tag, agency_title = agency_title, route_title = route_title, stop_title = stop_title, predictions = prediction_keys)
            estimations.append(e)

        try:
            ndb.put_multi(estimations)
        except CapabilityDisabledError:
            # fail gracefully here
            pass

        return estimations
Exemple #3
0
    def from_nextbus(cls, agency_tag, route_tag):
        logging.info("from_nextbus@Direction..")

        etree = url.fetch_nextbus_url({ "a" : agency_tag, "command" : "routeConfig", "r" : route_tag.replace("__", " "), "terse" : "True"})

        directions = []
        for elem in etree.findall("route/direction"):
            tag = elem.get("tag")
            # there are some directions named like 107_0_107B*uncon
            if tag.find("*") != -1:
                tag = tag.replace("*", "_")
            if tag.find("/") != -1:
                tag = tag.replace("/", "_")
            title = elem.get("title")
            name = elem.get("name")
            use_for_UI = bool(elem.get("useForUI"))
            d = Direction(id = tag, parent = ndb.Key("Route", route_tag, parent = ndb.Key("Agency", agency_tag)), tag = tag, title = title, name = name, use_for_UI = use_for_UI)
            directions.append(d)

        try:
            ndb.put_multi(directions)
        except CapabilityDisabledError:
            # fail gracefully here
            pass

        return directions
Exemple #4
0
    def from_nextbus(cls, agency_tag):
        logging.info("from_nextbus@Route...")

        routes = []
        etree = url.fetch_nextbus_url({ "a" : agency_tag, "command" : "routeList" })
        for elem in etree.findall("route"):
            tag = elem.get("tag")
            if tag.find(" ") != -1:
                tag = tag.replace(" ", "__")
            title = elem.get("title")
            r = Route(id = tag, parent = ndb.Key("Agency", agency_tag), tag = tag, title = title)
            routes.append(r)

        try:
            ndb.put_multi(routes, max_entity_groups_per_rpc = 16)
        except CapabilityDisabledError:
            # fail gracefully here
            pass

        return routes
Exemple #5
0
    def from_nextbus(cls):
        logging.info("from_nextbus@Agency...")

        agencies = []
        etree = url.fetch_nextbus_url({ "command" : "agencyList" })
        for elem in etree.findall("agency"):
            tag = elem.get("tag")
            title = elem.get("title")
            region_title = elem.get("regionTitle")
            r = Agency(id = tag, tag = tag, title = title, region_title = region_title)
            agencies.append(r)

        try:
            ndb.put_multi(agencies, max_entity_groups_per_rpc = 16)
        except CapabilityDisabledError:
            # fail gracefully here
            pass

#        cls.create_document(agencies)

        return agencies
Exemple #6
0
    def from_nextbus(cls, agency_tag, route_tag, direction_tag):
        logging.info("from_nextbus@Stop...")

        etree = url.fetch_nextbus_url({ "a" : agency_tag, "command" : "routeConfig", "r" : route_tag.replace("__", " "), "terse" : "True"})

        # create a lookup table which maps stop to directions that it belong
        #
        # stops2directions[stop_tag] -> direction_tag(s)
        stops2directions = {}
        for elem in etree.findall("route/direction"):
            direction_tag_ = elem.get("tag")
            # there are some directions named like 107_0_107B*uncon
            if direction_tag_.find("*") != -1:
                direction_tag_ = direction_tag_.replace("*", "_")
            if direction_tag_.find("/") != -1:
                direction_tag_ = direction_tag_.replace("/", "_")
            for stop_elem_ in elem.findall("stop"):
                stops2directions.setdefault(stop_elem_.get("tag"), set()).add(direction_tag_)

        # prefetch stops to solve staircase pattern caused by get calls in the loop below
        #
        # https://groups.google.com/forum/#!topic/appengine-ndb-discuss/wgNtAMwirJo
        for elem in etree.findall("route/stop"):
            tag = elem.get("tag")

            # some directions report unused stops so ignore them...
            if stops2directions.has_key(tag):
                ndb.Key("Stop", "%s@%s" % (agency_tag, tag)).get_async()

        # create/update stop entities
        #
        stops = []
        for elem in etree.findall("route/stop"):
            tag = elem.get("tag")

            # some directions report unused stops so ignore them...
            if stops2directions.has_key(tag):
                title = elem.get("title")
                stop_id = elem.get("stopId")
                location = ndb.GeoPt(float(elem.get("lat")), float(elem.get("lon")))
                directions = [ndb.Key("Direction", direction_tag_, parent = ndb.Key("Route", route_tag, parent = ndb.Key("Agency", agency_tag))) for direction_tag_ in stops2directions[tag]]
                stop_key = "%s@%s" % (agency_tag, tag)
                ####
                obj = ndb.Key("Stop", stop_key).get()
                if obj:
                    directions += obj.directions
                ####
                s = Stop(id = stop_key, tag = tag, title = title, stop_id = stop_id, location = location, directions = directions)
                stops.append(s)

        try:
            ndb.put_multi(stops)
        except CapabilityDisabledError:
            # fail gracefully here
            pass

        #cls.create_document(stops)

        # filter out other directions
        direction_key = ndb.Key("Direction", direction_tag, parent = ndb.Key("Route", route_tag, parent = ndb.Key("Agency", agency_tag)))
        return [stop for stop in stops if direction_key in stop.directions]