Here is my wrapper on TwythonStreamer:
from twython import TwythonStreamer
class Twitter_talker(TwythonStreamer):
def __init__(self):
super(Twitter_talker, self).__init__(<my credentials here...>)
def on_success(self, data):
self.disconnect()
print data
def on_error(self, status_code, data):
print status_code
self.disconnect()
Here is my call to get statuses from twitter:
class Tweet_Handler(tornado.web.RequestHandler):
def get(self):
stream = Twitter_talker()
stream.statuses.filter(track='badminton')
I create this stream looking for "badminton" on GET REST API call to my web server. And though its not coded, but it will be a async call, meaning i will return a response like "1" indicating stream was created successfully.
How do I self.disconnect
when i receive a DELETE REST API call with value "badminton" ?
您的DELETE
处理程序将具有与GET
处理程序不同的self
(始终为每个请求实例化一个新的RequestHandler
。您需要将GET
处理程序存储在某个位置,也许是将关键字映射到一组处理程序的全局字典(不要忘记)删除on_finish
),请看Tornado 聊天演示以及它如何管理服务员集合。
Use a class attribute to hold a dictionary of Twitter Talkers.
class Tweet_Handler(tornado.web.RequestHandler):
requests = {}
def get(self):
stream = Twitter_talker()
stream.statuses.filter(track='badminton')
Tweet_Handler.requests['badminton'] = stream
def delete(self):
value = self.get_argument("value")
if value in Tweet_Handler.requests:
Tweet_Handler.requests[value].disconnect()
del Tweet_Handler.requests[value]
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.