Skip to content

Instantly share code, notes, and snippets.

@ajfisher ajfisher/
Created Oct 16, 2011

What would you like to do?
Twitter Stream Analyser and Network Activator - watches twitter stream for keywords and hits a URL when it finds one.
# Twitter stream analyser and network activator (TSANA)
# This script analyses a twitter stream for keywords using the twitter
# streaming api - off the back of it, it then hits a user defined url
# In my case this is being used to tell an Arduino to pulse a light but
# could be used for anything arbitrary.
# Author: Andrew Fisher
# Version: 0.3
# Date: 20/10/2011
# Usage:
# Run from command line with python [opts]
# options are:
# --twitter-username=[user]
# --twitter-password=[password]
# --keywords=[comma separated list of keywords]
# --url=[complete URL to hit once a keyword has been found]
# Changelog:
# * v0.3 made password optional and uses getpass in order to not be in bash history
# * v0.2 Cleanup post WDS and released as a gist
import argparse
import getpass
import sys
import urllib2
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('--url', '-n', dest="url",
required=True, help="The url required to be hit when a match is made" )
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\nTSANA - Twitter Stream Analyser and Network Activator"
print "Searching for %s" % words
print "Will hit %s when one is found\n\n" % args.url
if args.pwd is None:
#password hasn't been supplied
args.pwd = getpass.getpass("Twitter password:")
stream = FilterStream(args.user, args.pwd, track=words)
for tweet in stream:
print "+++++"
print tweet["user"]["screen_name"]
print tweet["text"]
response = urllib2.urlopen(args.url).read()
except Exception, e:
print "Unexpected error:", sys.exc_info()[0]

This comment has been minimized.

Copy link
Owner Author

ajfisher commented Oct 16, 2011

To use from a command line run:

python --twitter-username=[user] --twitter-password='user's pass' --keywords='[list, of, comma, separated keywords]' --url=[]


This comment has been minimized.

Copy link

stephenmcd commented Oct 16, 2011

Use the getpass module to avoid passwords ending up in your bash history:


This comment has been minimized.

Copy link
Owner Author

ajfisher commented Oct 20, 2011

Thanks for that - very good point. Hadn't worried about it in my case as I was using my twitter bot with a very insecure password not one of my "real" ones. Updated now to make it optional on the command line and use the getpass module to pick it up that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.