Python Workshops for Beginners/October 25th Twitter project OS X setup

From OpenHatch wiki
Jump to navigation Jump to search

Download the TwitterAPI project[edit]

  1. Right click the following file, click "Save Target as..." or "Save link as...", and save it to your Desktop directory: http://mako.cc/teaching/2014/cdsw/TwitterAPI.zip
  2. The ".zip" extension on the above file indicates that it is a compressed Zip archive. We need to "extract" its contents. To do this, open Finder and navigate to your Desktop directory. Find TwitterAPI.zip on your Desktop and double-click on it to "unzip" it. That will create a folder called TwitterAPI containing several files.

Enter your API information[edit]

  • Start your text editor (probably Smultron if you installed it following our instructions). Navigate to the directory that contains TwitterAPI (probably something of the form ~/Desktop/TwitterAPI).
  • Open up the file twitter_authentication.py in your text editor.
  • You will see four lines that include four variables in ALL CAPITALS that are being assigned, in the normal ways we learned about last session, to strings. At the moment, all of the strings say CHANGE_ME.
  • Go find the four keys, tokens, and secrets you created and wrote-down when you followed the Twitter authentication setup. Change every string that says CHANGE_ME into a string that includes the key, token, or secret you downloaded. Remember that since these are strings, we need to include quotations marks around them. Also make sure that you match up the right keys and tokens with the right variables.

Once you have done this, your example programs are set up to use the Twitter API!

Test the TwitterAPI code[edit]

Start a command prompt and navigate to the Desktop/TwitterAPI directory where the TwitterAPI code lives. For example, if the TwitterAPI project is at ~/Desktop/TwitterAPI,

cd ~/Desktop/TwitterAPI

will change you into that directory (the "~" means your home directory!), and

ls

will show you the source code files in that directory. One of the files is "twitter1.py", which has a ".py" extension indicating that it is a Python script. Type:

python twitter1.py

at the command prompt to execute the run.py Python script. Wait a little while while your computer connects to Twitter. You should see a series of tweets run by your screen. If you don't, let a staff member know.

Success![edit]

You are done downloading the TwitterAPI project!

Champagne.pngParty.png