简体   繁体   中英

learning python with yelp api

I'm just beginning to learn python/django. I've been teaching myself PHP and now I would like to learn python. I'm having issues integrating yelp's API. I'm getting errors:

Values instance has no attribute 'q'

I have this code:

def search(request):
    parser = optparse.OptionParser()
    parser.add_option('-c', '--consumer_key', dest='my_consumer_key_goes_here', help='OAuth consumer key (REQUIRED)')
    parser.add_option('-s', '--consumer_secret', dest='my_consumer_secret_goes_here', help='OAuth consumer secret (REQUIRED)')
    parser.add_option('-t', '--token', dest='my_token_goes_here', help='OAuth token (REQUIRED)')
    parser.add_option('-e', '--token_secret', dest='my_token_secret_goes_here', help='OAuth token secret (REQUIRED)')
    parser.add_option('-a', '--host', dest='host', help='Host', default='api.yelp.com')
    options, args = parser.parse_args()

  # search stuff?
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        parser.add_option('-q', '--term', dest=q, help='Search term')
        url_params = {}
        if options.q:
            url_params['term'] = options.q
  # Sign the URL
        consumer = oauth2.Consumer(consumer_key, consumer_secret)
        oauth_request = oauth2.Request('GET', url, {})
        oauth_request.update({'oauth_nonce': oauth2.generate_nonce(),
                        'oauth_timestamp': oauth2.generate_timestamp(),
                        'oauth_token': token,
                        'oauth_consumer_key': consumer_key})

        token = oauth2.Token(token, token_secret)
        oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
        signed_url = oauth_request.to_url()
    else:
        message = 'You submitted an empty form.'
    #return HttpResponse(message)
    print 'Signed URL: %s\n' % (signed_url,)

  # Connect
    try:
        conn = urllib2.urlopen(signed_url, None)
        try:
            response = json.loads(conn.read())
        finally:
            conn.close()
    except urllib2.HTTPError, error:
        response = json.loads(error.read())

    return response

    response = request(options.host, '/v2/search', url_params, options.consumer_key, options.consumer_secret, options.token, options.token_secret)
    print json.dumps(response, sort_keys=True, indent=2)

Now as I'm still new to this whole python language, I wonder if I'm also adding the API consumer_key, secret, etc... in the right places? Do I even have this code set correctly? Most of it was from the original yelp api script. I guess the issue lies in the if options.q: area... not sure if I'm doing this correctly?

Thanks

I would say that calling parser.parse_args() before you declared your q option is probably your immediate problem.

But unless I am missing something about what you are trying to do, Django settings [1] are what you need to use for those first 5 options (don't use optparse at all). And then I don't know what you are trying to do with the rest of the function.

[1] https://docs.djangoproject.com/en/1.4/topics/settings/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM