Twitter Radar

Questo è il codice contenuto in uno dei tutorial del libro Python per Raspberry edito da Amazon KDP. L’utilizzo di questo codice è permesso solo ai possessori del libro Python per Raspberry. Si declina ogni responsabilità dall’uso proprio o improprio di tale codice. Tutti i diritti riservati.

#!/usr/bin/enx python
import time
import RPi.GPIO as GPIO
from twython import TwythonStreamer
# Termini da cercare
TERMS = 'yes'
# GPIO pin LED
LED = 25
# Autenticazione Twitter. Inserire i dati salvati in precedenza
APP_KEY = 'Hk4Faw98B5vUtzMzfj3pMHPqp'
APP_SECRET = 'w0b0EnceWl268PclpBAzG1h64CcMZsoxoVA0hHeAJk1aC7JfMH'
OAUTH_TOKEN = '1964398166-7AJ1DkX2HHuRV1x8FhNR6PLFgs1pMJ8QVIWbTXa'
OAUTH_TOKEN_SECRET = '6y4ExZqs8WzpNCQ94xwgMOE25EpcIqTZ3kR6nHeBFWwTH'
# Setup chiamata Twython
class BlinkyStreamer(TwythonStreamer):
def on_success(self, data):
if 'text' in data:
print data['text'].encode('utf-8')
print
GPIO.output(LED, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(LED, GPIO.LOW)
# Setup GPIO come output
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED, GPIO.OUT)
GPIO.output(LED, GPIO.LOW)
# Creazione dello stream
try:
stream = BlinkyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.filter(track=TERMS)
except KeyboardInterrupt:
GPIO.cleanup()