pcDuino team decided to do a weekly pcDuino3 give away sweepstakes for our tweeter account @pcDuino. pcDuino3 is chosen be to the machine to run the script to pick the winner.
We came across a nice tutorial about how to create a twitter app, and followed most part of it. In the below, we will detail what we have done to achieve the goal.
Before we can start the work on pcDuino3, we need to sign into twitter accout and create an application. The website that we need to log in is https://dev.twitter.com/ , which is different from the normal website. This is very important as I did make mistake. Once we are in, we hover over the avatar, and it will bring up a menu to let us select. We will click ‘My Applications”:
Then, we click ‘Create new app:
Fill the form:
Then, we will arrive at:
Please keep a record of these four log in credentials as we need to use them using tweepy.
Install tweepy on pcDuino3:
Install tweepy on pcDuino3 by the following commands:
$sudo apt-get update $sudo apt-get install python-dev python-pip $sudo pip install tweepy
I use the script from raspi.tv, and reproduced here:
#!/usr/bin/env python2.7 # twitterwin.py by Alex Eames http://raspi.tv/?p=5281 import tweepy import random # Consumer keys and access tokens, used for OAuth consumer_key = 'copy your consumer key here' consumer_secret = 'copy your consumer secret here' access_token = 'copy your access token here' access_token_secret = 'copy your access token secret here' # OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Creation of the actual interface, using authentication api = tweepy.API(auth) follow2 = api.followers_ids() # gives a list of followers ids print "you have %d followers" % len(follow2) show_list = str(raw_input("Do you want to list the followers array?")) if show_list == ('y' or 'yes' or 'Y' or 'Yes' or 'YES'): print follow2 def pick_winner(): random_number = random.randint(0, len(follow2)-1) winner = api.get_user(follow2[random_number]) print winner.screen_name, random_number while True: pick = raw_input("Press Enter to pick a winner, Q to quit.") if pick == ('q' or 'Q' or 'quit' or 'QUIT' or 'Quit'): break pick_winner()
Replace lines 7-10 with your actual keys, secrets and token.
Save this script as twitterwin.py, and we can run it by:
$sudo python twitterwin.py
The following is the screen shot when we run it: