Skip to content

Instantly share code, notes, and snippets.

@ajfisher
Created October 16, 2011 05:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajfisher/1290545 to your computer and use it in GitHub Desktop.
Save ajfisher/1290545 to your computer and use it in GitHub Desktop.
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 tsana.py [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"]
try:
response = urllib2.urlopen(args.url).read()
except Exception, e:
print "Unexpected error:", sys.exc_info()[0]
@ajfisher
Copy link
Author

To use from a command line run:

python tsana.py --twitter-username=[user] --twitter-password='user's pass' --keywords='[list, of, comma, separated keywords]' --url=[http://example.com/to/hit]

@stephenmcd
Copy link

Use the getpass module to avoid passwords ending up in your bash history: http://docs.python.org/release/2.5.2/lib/module-getpass.html

@ajfisher
Copy link
Author

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