2017-04-11 38 views
0

我使用树莓派和浆果剪辑创建项目。所以我编程要做的是(使用twython),代码搜索Twitter用户输入的特定关键字,当它发现它时,它会显示推文和LED闪光。我想在我的浆果剪辑上扩展这一点,并使用6个不同关键字的全部6个LED。我设法编写它来搜索6个关键字,但到目前为止,我无法找到如何根据关键字制作每个关键字。例如,如果流找到第一个关键字,则第一个LED将闪烁。如果找到第二个关键字,那么第二个LED将闪烁等。我尝试使用if语句在第一条if语句(if 'text' in data:)中加入if TERMS in 'text':但它没有起作用。这是到目前为止我的代码:Twython关键字和发光二极管

#!/usr/bin/python 
import time 
import RPi.GPIO as GPIO 
from twython import TwythonStreamer 

ledList=[4,17,22,10,9,11] 

GPIO.setmode(GPIO.BCM) 
GPIO.setup(ledList,GPIO.OUT) 

TERMS=(raw_input("Please type the first keyword")); 
TERMS2=(raw_input("Please type the second keyword")); 
TERMS3=(raw_input("Please type the third keyword")); 
TERMS4=(raw_input("Please type the fourth keyword")); 
TERMS5=(raw_input("Please type the fifth keyword")); 
TERMS6=(raw_input("Please type the sixth keyword")); 

termsList=[TERMS,TERMS1,TERMS2,TERMS3,TERMS4,TERMS5,TERMS6] 

APP_KEY='xxxxxxxxxxxx' 
APP_SECRET='xxxxxxxxx' 
OAUTH_TOKEN='xxxxxxxxx' 
OAUTH_TOKEN_SECRET='xxxxxxxxx' 

class streamer(TwythonStreamer): 
    def success(self,data): 
      if 'text' in data: 
       tweet_data=data['text'].encode('utf-8') 
       print (tweet_data) 
       GPIO.output(4,True) 
       time.sleep(1) 
       GPIO.output(4,False) 

try: 
    stream = streamer(APP_KEY,APP_SECRET,OAUTH_TOKEN,OAUTH_TOKEN_SECRET) 
    stream.statuses.filter(track=termsList) 

except KeyboardInterrupt: 
     print ('ending') 
     GPIO.cleanup() 

回答

0

它就像你说的,你需要检查哪些项是鸣叫文本。 (我对树莓派的了解不多,但我想象下面会点亮第i个词,当第i个词语在推特中找到时)

class streamer(TwythonStreamer): 
    def success(self,data): 
      if 'text' in data: 
       tweet_data=data['text'].encode('utf-8') 
       for i, term in enumerate(termList): 
       if term in tweet_data: 
        GPIO.output(i,True) 
        time.sleep(1) 
        GPIO.output(i,False)