当前位置: 首页>>代码示例>>Python>>正文


Python API.get方法代码示例

本文整理汇总了Python中API.get方法的典型用法代码示例。如果您正苦于以下问题:Python API.get方法的具体用法?Python API.get怎么用?Python API.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在API的用法示例。


在下文中一共展示了API.get方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: sync_predictions

# 需要导入模块: import API [as 别名]
# 或者: from API import get [as 别名]
def sync_predictions(routes, session):
    
    Logger.log.info("Syncing predictions...")
    Logger.log.info("Input routes: %s" % routes)

    if 'Red-Ashmont' in routes or 'Red-Braintree' in routes:
        routes.remove('Red-Ashmont')
        routes.remove('Red-Braintree')
        routes.append('Red')

    Logger.log.info("Using routes: %s" % routes)
    
    to_save = []

    route_string = ",".join(routes)
    data = API.get("predictionsbyroutes", {'routes': route_string})
    mode = data['mode']
    for route in mode:
        route_sub = route['route']
        for route_sub_sub in route_sub:
            route_name = route_sub_sub['route_id']
            Logger.log.info("Processing route %s" % route_name)
            for direction in route_sub_sub['direction']:
                for trip in direction['trip']:
                    api_trip_id = trip['trip_id']
                    trip_ref = session.query(db.Trip).filter(db.Trip.api_id == api_trip_id).first()
                    if trip_ref is None:
                        Logger.log.info('No trip record for this prediction. trip_api_id: %s' % api_trip_id)
                        continue
                    for stop in trip['stop']:
                        stop_name = stop['stop_name'].split(' -')[0]
                        
                        if 'JFK/UMASS' in stop_name:
                            stop_name = stop_name.split(' ')[0]
                        
                        try:
                            station_id = session.query(db.Station).filter(db.Station.route_id == trip_ref.route_id)\
                                .filter(db.Station.name_human_readable.like('%' + stop_name + '%')).first().id
                        
                        except AttributeError as e:
                            station_id = station_with_most_similar_name(session, trip_ref.route_id, stop_name)
                            
                        try:
                            seconds = stop['pre_away']
                            new_prediction_record = db.PredictionRecord(trip_id=trip_ref.id,
                                                                        stamp=datetime.datetime.utcnow(),
                                                                        station_id=station_id,
                                                                        seconds_away_from_stop=seconds)

                            to_save.append(new_prediction_record)
                        except KeyError as e:
                            continue
                            Logger.log.info('trip %s has terminated' % api_trip_id)

    for object in to_save:
        session.merge(object)

    session.commit()

    return to_save
开发者ID:nettube,项目名称:mbtapuller,代码行数:62,代码来源:APIFunctions.py

示例2: get_stations

# 需要导入模块: import API [as 别名]
# 或者: from API import get [as 别名]
def get_stations(session):
    stations = []
    routes = session.query(db.Route).all()

    populate_red = False

    for route in routes:
        if route.name == RED_LINE_ASHMONT or route.name == RED_LINE_BRAINTREE:
            populate_red = True
            continue

        api_result = API.get("stopsbyroute", {'route': route.name})['direction'][0]['stop']
        for item in api_result:
            accessible = True

            if item['parent_station_name'] in NON_ACCESSIBLE_STATIONS:
                accessible = False

            new_station = db.Station(route_id=route.id,
                                     name_human_readable=item['parent_station_name'],
                                     name_api=item['parent_station'],
                                     location_lat=item['stop_lat'],
                                     location_lng=item['stop_lon'],
                                     accessible=accessible)
            stations.append(new_station)

    if populate_red:
        ashmont = []
        ashmont_added_jfk = False
        braintree = []
        braintree_added_jfk = False

        route_id_ashmont = session.query(db.Route).filter(db.Route.name == RED_LINE_ASHMONT).first().id
        route_id_braintree = session.query(db.Route).filter(db.Route.name == RED_LINE_BRAINTREE).first().id

        api_result = API.get("stopsbyroute", {'route': 'Red'})['direction'][0]['stop']
        for item in api_result:

            accessible = True

            if item['parent_station_name'] in NON_ACCESSIBLE_STATIONS:
                accessible = False

            if item['parent_station_name'] in RED_LINE_BRAINTREE_STATIONS:
                new_station = db.Station(route_id=route_id_braintree,
                                         name_human_readable=item['parent_station_name'],
                                         name_api=item['parent_station'],
                                         location_lat=item['stop_lat'],
                                         accessible=accessible,
                                         location_lng=item['stop_lon'])
                braintree.append(new_station)
            elif item['parent_station_name'] in RED_LINE_ASHMONT_STATIONS:
                new_station = db.Station(route_id=route_id_ashmont, name_human_readable=item['parent_station_name'],
                                      name_api=item['parent_station'], location_lat=item['stop_lat'],
                                      location_lng=item['stop_lon'], accessible=accessible)
                ashmont.append(new_station)
            else:
                new_station_ashmont = db.Station(route_id=route_id_ashmont,
                                                 name_human_readable=item['parent_station_name'],
                                                 name_api=item['parent_station'],
                                                 location_lat=item['stop_lat'],
                                                 location_lng=item['stop_lon'], accessible=accessible)
                new_station_braintree = db.Station(route_id=route_id_braintree,
                                                   name_human_readable=item['parent_station_name'],
                                                   name_api=item['parent_station'],
                                                   location_lat=item['stop_lat'],
                                                   location_lng=item['stop_lon'],
                                                   accessible=accessible)

                if new_station_ashmont.name_api == 'place-jfk':
                    if not ashmont_added_jfk:
                        ashmont_added_jfk = True
                        ashmont.append(new_station_ashmont)
                else:
                    ashmont.append(new_station_ashmont)

                if new_station_braintree.name_api == 'place-jfk':
                    if not braintree_added_jfk:
                        braintree_added_jfk = True
                        braintree.append(new_station_braintree)
                else:
                    braintree.append(new_station_braintree)

        stations.extend(ashmont)
        stations.extend(braintree)

    return stations
开发者ID:nettube,项目名称:mbtapuller,代码行数:89,代码来源:APIFunctions.py

示例3: sync_trips_and_records

# 需要导入模块: import API [as 别名]
# 或者: from API import get [as 别名]
def sync_trips_and_records(routes, session):

    Logger.log.info("Syncing trips...")
    Logger.log.info("Input routes: %s" % routes)

    if 'Red-Ashmont' in routes or 'Red-Braintree' in routes:
        routes.remove('Red-Ashmont')
        routes.remove('Red-Braintree')
        routes.append('Red')

    Logger.log.info("Using routes: %s" % routes)

    to_save = []

    route_string = ",".join(routes)
    data = API.get("vehiclesbyroutes", {'routes': route_string})
    mode = data['mode']
    for route in mode:
        route_sub = route['route']
        for route_sub_sub in route_sub:
            route_name = route_sub_sub['route_id']
            Logger.log.info("Processing route %s" % route_name)
            for direction in route_sub_sub['direction']:
                for trip in direction['trip']:
                    # The MBTA sometimes just throws us random bs
                    # Love you guys, but...really?
                    if trip['trip_name'] == u'':
                        continue

                    trip['trip_name'] = trip['trip_name'].replace('Forest Hills Orange Line', 'Forest Hills')

                    # Now we have a trip
                    trips_with_same_id = session.query(db.Trip).filter(db.Trip.api_id == trip['trip_id']).filter(
                        db.Trip.date == datetime.datetime.utcnow().date())

                    # print("trips_with_same_id is {}".format(trips_with_same_id.count()))
                    if trips_with_same_id.count() == 1:
                        # Create a trip record object

                        new_trip_record = db.TripRecord(trip_id=trips_with_same_id.first().id,
                                                        location_lat=trip['vehicle']['vehicle_lat'],
                                                        location_lng=trip['vehicle']['vehicle_lon'],
                                                        stamp=datetime.datetime.utcnow())

                        to_save.append(new_trip_record)

                        # Update the trip's last seen time
                        session.query(db.Trip).filter(db.Trip.id == trips_with_same_id.first().id)\
                            .update({'stamp_last_seen' : datetime.datetime.utcnow()})
                    elif trips_with_same_id.count() == 0:
                        # Create a trip object
                        if 'Red' in route_name:
                            if string_contains_ashmont_anything(trip['trip_name']):
                                route_name = constants.RED_LINE_ASHMONT
                            elif string_contains_braintree_anything(trip['trip_name']):
                                route_name = constants.RED_LINE_BRAINTREE
                            else:
                                route_name = constants.RED_LINE_ASHMONT

                        route_id = session.query(db.Route).filter(db.Route.name == route_name).first().id
                        station_pair = origin_and_destination_stations(session, trip, route_id)
                        origin_station_id = station_pair[0]
                        destination_station_id = station_pair[1]

                        new_trip = db.Trip(api_id=trip['trip_id'],
                                           route_id=route_id,
                                           origin_station_id=origin_station_id,
                                           destination_station_id=destination_station_id,
                                           lead=str(trip['vehicle']['vehicle_label']),
                                           date=datetime.datetime.utcnow(),
                                           stamp_first_seen=datetime.datetime.utcnow(),
                                           stamp_last_seen=datetime.datetime.utcnow())

                        session.add(new_trip)
                        session.commit()

                        new_trip_record = db.TripRecord(trip_id=new_trip.id,
                                                        location_lat=trip['vehicle']['vehicle_lat'],
                                                        location_lng=trip['vehicle']['vehicle_lon'],
                                                        stamp=datetime.datetime.utcnow())

                        to_save.append(new_trip_record)

                    else:
                        raise RuntimeError("There were multiple trip objects with the same id: %s" % trips_with_same_id)

    for object in to_save:
        session.merge(object)

    session.commit()

    return to_save
开发者ID:nettube,项目名称:mbtapuller,代码行数:94,代码来源:APIFunctions.py


注:本文中的API.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。