This blog post is a code example that I also recorded a live video of which programatically selects a winner to a recent Data Skeptic contest run in collaboration with DataSciGuide.com.


In [21]:
import os
from twython import Twython
import ConfigParser
import random
import bitlyapi
from IPython.core.display import HTML

In [4]:
propertiesFile = "my.properties"
cp = ConfigParser.ConfigParser()
cp.readfp(open(propertiesFile))

APP_KEY            = cp.get('Params', 'app_key')
APP_SECRET         = cp.get('Params', 'app_secret')
OAUTH_TOKEN        = cp.get('Params', 'oauth_token')
OAUTH_TOKEN_SECRET = cp.get('Params', 'oauth_token_secret')

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

In [5]:
# GET ALL ENTRIES

search = twitter.search(q='#WinDSBook')

In [7]:
tweets=search['statuses']

In [12]:
f = open('/dev/random', 'rb')
truerandom = f.read(1)
f.close()

In [13]:
map(bin, bytearray(truerandom))


Out[13]:
['0b11000001']

In [14]:
# TEST RUN
i = random.randint(0, len(tweets))

In [15]:
i


Out[15]:
2

In [16]:
winner = tweets[i]

In [19]:
print winner['user']['name']
print winner['user']['screen_name']
print winner['user']['location']


Adam Jay
01AdamJay
Miami Beach, FL

In [22]:
HTML(Twython.html_for_tweet(winner))


Out[22]:
I reviewed an item on @DataSciGuide to enter the @DataSkeptic book giveaway! #WinDSBook Check out my review here: datasciguide.com/content/becomi…

In [25]:
winner['entities']['urls']


Out[25]:
[{u'display_url': u'datasciguide.com/content/becomi\u2026',
  u'expanded_url': u'http://www.datasciguide.com/content/becoming-a-data-scientist/',
  u'indices': [114, 137],
  u'url': u'https://t.co/Pa1mlspYFA'}]

Congradulations to @01AdamJay for winning the Data Skeptic contest! Please DM me your mailing address so I can ship you off a copy of Cameron's excellent book in print.