Created
January 15, 2012 09:50
-
-
Save ajfisher/1615256 to your computer and use it in GitHub Desktop.
Send a twitter message to a serial connection on an arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Twitter stream analyser and serial activator (TSASA) | |
# | |
# This script analyses a twitter stream for keywords using the twitter | |
# streaming api - off the back of it, it then makes a serial request passing | |
# the message across to the serial device | |
# | |
# This was done fopr LCA2012 as an extension to the more code generator that | |
# Luke Weston build for the LeoStick | |
# | |
# | |
# | |
# Author: Andrew Fisher | |
# Version: 0.1 | |
# Date: 15/01/2012 | |
# Licence: BSD | |
# | |
# Dependencies: | |
# You'll need the tweetstream library - a "pip install tweetstream" should do it | |
# You'll need an arduino listening on a serial connection and with this library loaded onto it: | |
# https://github.com/lukeweston/LeoMorse | |
# Usage: | |
# Run from command line with python tsana.py [opts] | |
# options are: | |
# --twitter-username=[user] | |
# --keywords=[comma separated list of keywords] | |
# --device=[path to serial device] | |
# --baud=[Baud rate to connect to] | |
# | |
# eg: python tsasa.py --twitter-username ajfisher --keywords "#lca2012" --device /dev/ttyACM0 --baud 38400 | |
# | |
# Changelog: | |
# 0.1: Modification of tsana script to work with serial | |
import argparse | |
import getpass | |
import sys | |
import serial | |
from tweetstream import FilterStream | |
parser = argparse.ArgumentParser(description="Analyse a twitter stream and activate a URL once a keyword is found") | |
parser.add_argument('--twitter-username', '-u', dest="user", | |
required=True, help="A valid twitter user name" ) | |
parser.add_argument('--twitter-password', '-p', dest="pwd", | |
required=False, help="Password for this twitter account can be left blank and you'll be asked for it" ) | |
parser.add_argument('--keywords', '-k', dest="keywords", | |
required=True, help="A keyword list, comma separated, in quotes" ) | |
parser.add_argument('--device', '-d', dest="serial_interface", | |
required=True, help="The serial device to send to" ) | |
parser.add_argument('--baud', '-b', dest="baud_rate", | |
required=True, help="The baude rate of the serial connection" ) | |
args = parser.parse_args() | |
# put the keywords in a list and strip any leading spaces resulting from the comma splitting | |
words = [word.lstrip() for word in args.keywords.split(",")] | |
print "\n\nTSASA - Twitter Stream Analyser and Serial Activator" | |
print "Searching for %s" % words | |
print "Will pass message to %s when one is found\n\n" % args.serial_interface | |
if args.pwd is None: | |
#password hasn't been supplied | |
args.pwd = getpass.getpass("Twitter password:") | |
try: | |
ser = serial.Serial(args.serial_interface, args.baud_rate, timeout=60) | |
except: | |
print "Serial timeout - check device %s and try again" % args.serial_interface | |
exit(0) | |
stream = FilterStream(args.user, args.pwd, track=words) | |
for tweet in stream: | |
print "+++++" | |
print tweet["user"]["screen_name"] | |
print tweet["text"] | |
try: | |
ser.write("%s\n" % str(tweet["text"])) | |
except Exception, e: | |
print "Unexpected error:", sys.exc_info()[0] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment