我正在尝试创建一个Python服务器,我可以从其他应用程序调用它来请求Twitter数据。我通常使用Python作为脚本语言,所以如果有任何人在我的代码中看到任何红旗,我都会耳朵!
这基本上就是我到目前为止,当我ping服务器时它运行良好,它从我的时间线获得10条推文并将它们发送回我的其他应用程序。我的主要问题是我想结合流媒体和搜索。这样我可以让流打开一个特定的哈希标签,我希望它实时发送到我的其他应用程序,但后来我会定期搜索其他不需要下载的东西。我是实时的。
我已经成功地单独使用了两个,但是如果我想要实现两者,我不知道从哪里开始,在这种情况下我想将流功能带入其中。
我正在使用Python Twitter工具1.10.2 - http://mike.verdone.ca/twitter/ 和Python 3.3
下面的代码,谢谢!
编辑:通过在if data ==“SEARCH_NOW”if语句之后添加twitter流连接,我能够更进一步。但这提出了我遇到的原始问题。一旦twitter流打开,代码似乎就在那里等待。如果我把它放在时间线查找之前,那么我永远不能调用时间线查找。更新了反映的代码。编辑2:将搜索请求放在twitter流循环中更近一点。我现在可以打开流,每次收到与搜索词匹配的推文时,我也可以提出请求。但仍然没有独立......
文件:network_settings.py
#!/usr/bin/env python
#network settings
import socket
#set server variables
TCP_IP = '127.0.0.1'
TCP_PORT = 7001
BUFFER_SIZE = 20
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
#print connection address when someone connects
print ('Connection address:', addr)
档案:twitter_settings.py
from twitter import *
import re
OAUTH_TOKEN = ''
OAUTH_SECRET = ''
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
t = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET))
auth = OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
stream = TwitterStream(auth = auth, secure = True)
文件:python_server.py
#python server
import json
from network_settings import *
from twitter_settings import *
search_term = 'test'
while 1:
tweet_iter = stream.statuses.filter(track = search_term)
for tweet in tweet_iter:
# check whether this is a valid tweet
if tweet.get('text'):
userName = tweet["user"]["screen_name"]
userTweet = tweet["text"]
# now print our tweet
print ('user: ', userName)
print ('tweet: ', userTweet)
#send data back
delivery1 = json.dumps({'type':'showdown','userName':userName,'userTweet':userTweet})
conn.send(delivery1.encode('utf-8'))
data = conn.recv(BUFFER_SIZE)
data = data.decode('utf-8')
if data == "SEARCH_NOW":
print ('request newest IDS tweets')
x = t.statuses.home_timeline(count=10)
for i in range(10):
try:
#print(x[i])
userName = x[i]['entities']['user_mentions'][0]['screen_name']
userTweet = x[i]['text']
print('username: ', userName)
print('tweet: ', userTweet)
delivery = json.dumps({'type':'display','userName':userName,'userTweet':userTweet})
conn.send(delivery.encode('utf-8'))
except:
print('not valid tweet')
conn.close()
答案 0 :(得分:1)
所以终于找到了解决方案。我最终使用线程在它自己的线程中运行流,然后我每次进行搜索时打开另一个线程。不确定我是否需要关闭每个线程,或者返回是否需要处理。如果有人有任何东西他们的东西可以改善,我都是耳朵!
以下代码:
#!/usr/bin/env python
#python server
import json
import threading
import time
import socket
from twitter import *
import re
#get thread lock ready
thread_lock = threading.Lock()
#set server variables
TCP_IP = '127.0.0.1'
TCP_PORT = 7001
BUFFER_SIZE = 20
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
#print connection address when someone connects
print ('Connection address:', addr)
#fill these in your app!
#twitter auth keys
OAUTH_TOKEN = ''
OAUTH_SECRET = ''
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
t = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET))
auth = OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
stream = TwitterStream(auth = auth, secure = True)
#twitter functions
def pythonSearch():
#lock thread to not interrupt search results
thread_lock.acquire()
print ('request newest tweets')
#get 10 things from timeline
x = t.statuses.home_timeline(count=10)
for i in range(10):
try:
#get username and tweet
userName = x[i]['entities']['user_mentions'][0]['screen_name']
userTweet = x[i]['text']
#print out values
print('username: ', userName)
print('tweet: ', userTweet)
#send json back
delivery = json.dumps({'type':'display','userName':userName,'userTweet':userTweet})
conn.send(delivery.encode('utf-8'))
except:
#not a retweet
print('not valid tweet')
#unlock thread when finished
thread_lock.release()
return
def pythonStream():
#open stream looking for search_term
search_term = 'TESTING'
tweet_iter = stream.statuses.filter(track = search_term)
for tweet in tweet_iter:
# check whether this is a valid tweet
if tweet.get('text'):
#get username and tweet
userName = tweet["user"]["screen_name"]
userTweet = tweet["text"]
# now print our tweet
print ('user: ', userName)
print ('tweet: ', userTweet)
#send json back
delivery1 = json.dumps({'type':'showdown','userName':userName,'userTweet':userTweet})
conn.send(delivery1.encode('utf-8'))
#start main loop
while 1:
#listen for calls
data = conn.recv(BUFFER_SIZE)
data = data.decode('utf-8')
#if someone calls search, do a search
if data == 'SEARCH':
threading.Thread(target = pythonSearch).start()
if data == 'STREAM':
threading.Thread(target = pythonStream).start()
conn.close()