Raspberry Pi Twitter Monitor
Contributors:
Shawn Hymel
Code
With our packages installed, we can write our program. Open up a text editor and make a new Python script. For example:
nano TweetBlinky.py
In the new file, enter the code below.
import time import RPi.GPIO as GPIO from twython import TwythonStreamer # Search terms TERMS = '#yes' # GPIO pin number of LED LED = 22 # Twitter application authentication APP_KEY = 'erRilYZd8UzsXEFycmg' APP_SECRET = 'Yt0fGlNvCyr1sFaC6ymdNhphHchaWbz0ECdotEXIQQ' OAUTH_TOKEN = '1969690717-6a2RgVPXanSBaAjuie7EmUWZh78me8UZ6UxcM8V' OAUTH_TOKEN_SECRET = 'UIrYV2XbYZC3vHzer6ZxIDwqVa0VvynQLDJYnSQV0R3xt' # Setup callbacks from Twython Streamer 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 as output GPIO.setmode(GPIO.BOARD) GPIO.setup(LED, GPIO.OUT) GPIO.output(LED, GPIO.LOW) # Create streamer try: stream = BlinkyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) stream.statuses.filter(track=TERMS) except KeyboardInterrupt: GPIO.cleanup()
Alternatively, you can download a zip file of the script here.
IMPORTANT: You will need to change the Twitter authentication tokens! Right now, they are set to my example application. Copy the strings from the your dev.twitter.com page (the single quotes are needed):
APP_KEY = ‘<Your Consumer Key>’ APP_SECRET = ‘<Your Consumer Secret>’ OAUTH_TOKEN = ‘<Your Access Token>’ OAUTH_TOKEN_SECRET = '<Your Access Token Secret>’'
Save and exit (ctrl + X and ‘y’ if you are using nano).