yelp_call.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:bobafetch 作者: AnliYang 项目源码 文件源码
def request_restaurants(user_latitude, user_longitude, radius=40000, limit=20):
    """Prepares OAuth authentication and sends the request to the API."""

    user_lat_lng = user_latitude + ',' + user_longitude

    url_params = {
        'll': user_lat_lng,
        'limit': limit,
        # Sort mode: 0=Best matched (default), 1=Distance, 2=Highest Rated.
        'sort': 1,
        'category_filter': 'bubbletea',
        # Search radius in meters. Max value is 40000 meters (25 miles).
        'radius_filter': radius
    }

    url = 'https://{0}{1}?'.format(API_HOST, urllib.quote(SEARCH_PATH.encode('utf8')))

    # creates a consumer object
    consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)

    # creates a token object
    token = oauth2.Token(TOKEN, TOKEN_SECRET)

    oauth_request = oauth2.Request(
        method="GET", url=url, parameters=url_params)
    # adds more entries to the request dictionary (of parameters for the query, looks like)

    # adds oauth bits to the oauth
    oauth_request.update(
        {
            'oauth_nonce': oauth2.generate_nonce(),
            'oauth_timestamp': oauth2.generate_timestamp(),
            'oauth_token': TOKEN,
            'oauth_consumer_key': CONSUMER_KEY
        }
    )
    # hashes sensitive parts of the request
    oauth_request.sign_request(
        oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)

    # generates the final url
    signed_url = oauth_request.to_url()

    # open up a connection to the signed_url
    conn = urllib2.urlopen(signed_url, None)

    try:
        response = json.loads(conn.read())
        # response = conn.read()
    finally:
        conn.close()

    yelp_location_ids = save_restaurants(response)

    return yelp_location_ids
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号