首先让我解释一下我想要做什么:
基本概述:在一个类中从 twitter 获取数据并在另一个类中打印。
Little Deeper:我希望CustomStreamListener
能够作为单独的线程运行。并且每一个状态,我得到的on_status
方法都放在一个队列中。比Twitter()
班级将队列拉出并打印出来。
现在我不明白:
- 我应该投入什么
run()
功能?(它应该打电话on_status()
吗?) 如果一切都很好,
CustomStreamListener
我应该怎么称呼它?(为了让我在没有我的情况下调用这个类,Thread
我应该做这样的事情:l = CustomStreamListener() stream = tweepy.streaming.Stream(self.auth,l) stream.filter(follow=None, track=hashtag)
现在,如果我想打电话
threading.Thread
,我必须start()
在将队列拉出后的某个地方调用它。所以这部分对我来说有点混乱。
我也愿意接受任何其他方式来获得类似的结果。谢谢你。
import sys, os, time
import tweepy
import simplejson as json
import threading, Queue
queue = Queue.Queue()
#SETTINGS :
#output = open(os.path.join(os.path.expanduser('~/'), 'sample_file.txt'), 'a')
hashtag = ['java']
class CustomStreamListener(tweepy.StreamListener, threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.queue = queue
def on_status(self, status):
try:
# If you want to have different data from twitter just change .text to something different from tweet:
'''
status.text
status.author.screen_name
status.created_at
status.source
status.user.screen_name
status.id
status.source_url
status.place
'''
time.sleep(0.15)
data = status.text
self.queue.put(data)
#print json.loads(data)
#with open(os.path.join(os.path.expanduser('~/'), 'sample_file.txt'), 'a') as output:
# output.write("%s\n "% data)
except Exception, e:
print >> sys.stderr, 'Encountered Exception:', e
pass
'''
def on_data(self, data):
d = (json.dumps(json.loads(data), indent=4, sort_keys=True))
print d
output.write("%s "% d)
'''
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
class Twitter():
def __init__(self):
consumer_key=''
consumer_secret=''
access_key = ''
access_secret = ''
self.auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
self.auth.set_access_token(access_key, access_secret)
self.api = tweepy.API(self.auth)
def start(self):
l = CustomStreamListener()
stream = tweepy.streaming.Stream(self.auth,l)
stream.filter(follow=None, track=hashtag)
if __name__ == "__main__":
Twitter().start()