Dabeaz

Dave Beazley's mondo computer blog. [ homepage | archive ]

Tuesday, January 31, 2012

 

Drunk Tweeting in Chicago

Lately, I've been messing around with the requests and regex libraries for Python. They are awesome. So, without any further explanation, I present this short script that uses both in an attempt to identify people drunk-tweeting in Chicago. Enjoy.

# drunktweet.py
'''
Print out possible drunk tweets from the city of Chicago.
'''

import regex
import requests
import json

# Terms for being "wasted"
terms = { 'drunk','wasted','buzzed','hammered','plastered' }

# A fuzzy regex for people who can't type
pat = regex.compile(r"(?:\L<terms>){i,d,s,e<=2}$", regex.I, terms=terms)

# Connect to the Twitter streaming API
url   = "https://stream.twitter.com/1/statuses/filter.json"
parms = {
    'locations' : "-87.94,41.644,-87.523,42.023"    # Chicago
    }
auth  = ('username','password')
r = requests.post(url, data=parms, auth=auth)

# Print possible candidates
for line in r.iter_lines():
    if line:
        tweet = json.loads(line)
        status = tweet.get('text',u'')
        words = status.split()
        if any(pat.match(word) for word in words):
           print(tweet['user']['screen_name'], status)

It's left as an exercise to reader to filter out false-positives and have the script call a cab. By the way, you should check out some of my Python Classes in Chicago.


Comments:
too funny,
if i could figure out how to fork blogspot i would have added this version.

although it would also be interesting to add a time constraint to your chicago script and grab the pycon@night for similar twitter.drunk.very

# nerdalert.py
'''
Print out (very)possible tweets from this years PyCon.
'''

import regex
import requests
import json

# Terms for being "py.clever"
terms = { 'pypy','gil','damn!!!','elegant','magic','sweet','powerboard','beer?','tshirt','airplane commit','sprint','coffee' }

# A fuzzy regex for people who can't type
pat = regex.compile(r"(?:\L){i,d,s,e<=2}$", regex.I, terms=terms)

# Connect to the Twitter streaming API
url = "https://stream.twitter.com/1/statuses/filter.json"
params = {
'locations' : "-121.91,37.431,-122.018,37.301" # Santa Clara
}
auth = ('username','password')
r = requests.post(url, data=params, auth=auth)

# Print possible candidates
for line in r.iter_lines():
if line:
tweet = json.loads(line)
status = tweet.get('text',u'')
words = status.split()
if any(pat.match(word) for word in words):
print(tweet['user']['screen_name'], status)
 
Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

Prior Posts by Topic

08/01/2009 - 09/01/2009   09/01/2009 - 10/01/2009   10/01/2009 - 11/01/2009   11/01/2009 - 12/01/2009   12/01/2009 - 01/01/2010   01/01/2010 - 02/01/2010   02/01/2010 - 03/01/2010   04/01/2010 - 05/01/2010   05/01/2010 - 06/01/2010   07/01/2010 - 08/01/2010   08/01/2010 - 09/01/2010   09/01/2010 - 10/01/2010   12/01/2010 - 01/01/2011   01/01/2011 - 02/01/2011   02/01/2011 - 03/01/2011   03/01/2011 - 04/01/2011   04/01/2011 - 05/01/2011   05/01/2011 - 06/01/2011   08/01/2011 - 09/01/2011   09/01/2011 - 10/01/2011   12/01/2011 - 01/01/2012   01/01/2012 - 02/01/2012   02/01/2012 - 03/01/2012   03/01/2012 - 04/01/2012   07/01/2012 - 08/01/2012   01/01/2013 - 02/01/2013   03/01/2013 - 04/01/2013   06/01/2014 - 07/01/2014  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]