如何在Python中异步使用Twitter搜索API? [英] How to use twitter search API asynchronously in python?
问题描述
我阅读了龙卷风简介.它使用Twitter搜索API在应用程序中引入了龙卷风的异步功能.
I read the book Introduction to Tornado. It introduces the asynchronous feature of tornado in an application using twitter search API.
代码如下:
class IndexHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
query = self.get_argument('q')
client = tornado.httpclient.AsyncHTTPClient()
response = yield tornado.gen.Task(client.fetch,
"http://search.twitter.com/search.json?" + \
urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100}))
...
self.finish()
它使用v1 twitter API搜索关键内容.但是,新的 v1.1 twitter API 禁止使用非oauth请求.结果,我必须使用带有我的使用者密钥和访问密钥的oauth库来请求Twitter搜索API.
It uses the v1 twitter API to search a keywork. However, the new v1.1 twitter API forbids the use of non oauth request. As a result, I have to use the oauth library with my consumer key and access key to request the twitter search API.
def request_twitter(url, http_method = 'GET', post_body = '', http_headers = ''):
consumer = oauth.Consumer(key = consumer_key, secret = consumer_secret)
token = oauth.Token(key = access_token, secret = access_secret)
client = oauth.Client(consumer, token)
request = client.request(url, method = http_method, body = post_body, headers = http_headers)
return request
但是oauth客户端不提供异步请求方式.所以我想知道如何使用oauth客户端向python中的twitter API发出异步请求?谢谢.
But the oauth client doesn't provide the asynchronous way to request. So I want to know how can I make the asynchronous request using oauth client to twitter API in python? Thanks.
推荐答案
看看它的代码一点.
这篇关于如何在Python中异步使用Twitter搜索API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!