Use of procedures, parameter passing, block structures, data types, arrays, abstract data structures, conditional control, iterative and recursive processes, and input/output in programming solutions to a variety of problems. Prerequisite: None.

Lab 7 - Introduction to APIs - (50 points)



Due date: 4/16/2019

Objectives



  • Gain practice on using APIs in Python
  • Familiarize yourself with importing external modules
  • Practice with dictionaries and lists

Introduction



For this lab you will create a python program that uses the Twitter API. You will search the twitter stream for keywords using the API and store the most recent 100 tweets in a dictionary associated with that keyword. If the user asks for a keyword that has already been searched for before, then you look it up in the dictionary rather than in the twitter stream.


Part 1: API Keys (5 points)



You will need an a consumer key, a consumer secret, an acess token key, and an access token secret from Twitter to access their API. To obtain these, follow these steps:

  1. Log into the Twitter Developers section. If you don't already have a developers account, you can login with your normal Twitter credentials. Note:You need a twitter account for this lab.
  2. Go to "Create an app"
  3. Fill in the details of the application you'll be using to connect with the API. Your application name must be unique. If someone else is already using it, you won't be able to register your application until you can think of something that isn't being used.
  4. Click on "Create your Twitter application"
  5. Details of your new app will be shown along with your consumer key and consumer secret.
  6. If you need access tokens, scroll down and click "Create my access token". The page will then refresh on the "Details" tab with your new access tokens. You can recreate these at any time if you need to.
Note: By default your apps will be granted for read-only access. To change this, go to the Settings tab and change the access level required in the "Application Type" section.

Part 2: Install and Test python-twitter (5 points)



  • Type pip3 install python-twitter on the terminal to install the python-twitter wrapper around the Twitter API on your computer.
  • Create a test program that imports python-twitter to check whether it works well.

    import twitter
    api = twitter.Api(consumer_key='consumer_key',
    consumer_secret='consumer_secret',
    access_token_key='access_token',
    access_token_secret='access_token_secret')
    
    print(api.VerifyCredentials())
    

    This should print your credentials which will be in this format but with a lot more detail. {"id": 16133, "location": "Philadelphia", "name": "bear"}

Part 3: Store tweets (25 points)



For this lab, you will write a program that asks the user to enter a keyword. Your program will then get the 100 most recent tweets and store them in a dictionary with the keyword as the key and the returned tweets as the value. If that keyword already exists in the dictionary, you will NOT search the twitter stream for the most recent tweets and will instead show the tweets in the dictionary. The program should work as follows:

  • Ask the user for a keyword and check if that keyword is in the dictionary. If it is found in the dictionary, then print the contents of the value for that key. (10 points)
  • If the keyword is not found then search for the keyword using the API as below. For example, the command below will search for the most recent 100 tweets from January 1st, 2019. (10 points)
  • results = api.GetSearch(raw_query="q=usfca%20&result_type=recent&since=2019-01-01&count=100")

  • The GetSearch() method above returns a list of tweets. Store them in the dictionary as mentioned above (key = word being searched for, value = the most recent tweets)
  • Now display a list of all the keys in the dictionary to the user, so they know what is in the dictionary.(5 points)
  • Ask the user again for another keyword and repeat until the user chooses to terminate the program.

Part 4: Personal Website (15 points)



Create your personal website with professional content that contains your name, your contact information, a professional picture of yourself, and some information related to your education, technical skills, and anything else that a recruiter at a company may want to know. This webpage can be hosted on the CS servers or on GitHub Pages for free. Your webpage must have a list, a few links, and a table.

Make sure to include a link to your website in the README file.

Submitting the lab



Please turn in the following for the lab.

  • README file - This file must contain a list of all the resources you have used for the lab. It must also contain the URL for your website.
  • python-twitter-search.py - The source code for your program