Module 2: Consuming Data from an API
Module Overview
A powerful way to enhance a web application is by connecting it to an API. These Application Programming Interfaces accept requests and return results, similar to the servers you build with Flask. What makes APIs powerful is the breadth of data they provide—from tweets to weather to space imagery to Pokémon—enabling your applications to leverage rich, real-world information.
Learning Objectives
1. Connect to the Twitter API and query for user and tweet data
• Authenticate and establish connection with the Twitter API
• Query the Twitter API for user profile information
• Retrieve tweet data from specified users
• Filter tweets based on various parameters
• Handle API rate limits and restrictions
• Store and manage retrieved Twitter data
2. Implement a SpaCy NLP model to generate word embeddings (numeric representations) of our tweet text
• Set up and configure SpaCy for natural language processing
• Process tweet text using SpaCy's linguistic analysis
• Generate word embeddings from processed tweet text
• Understand vector representations of text data
• Store embeddings alongside tweet data
• Prepare text data for machine learning applications
Guided Project
Consuming Data from an API
Guided Project File:
guided-project.md
Follow Along Instructions
Twitter is changing their API policies and tweepy is going offline very soon. In its place, we have implemented a Twitter-API-like clone and an API interface library to bridge the gap. We named the Twitter API "NotTwitter" and the library "not_tweepy".
To install not_tweepy, please read the README.md file in the GitHub repository for detailed instructions. You'll need to copy the not_tweepy folder into your local repo at the root level of your project.
- Project
- twitoff
- not_tweepy
You may follow the instructions in this Guided Project video with the only exception:
When you see:
import tweepy
You will instead type:
import not_tweepy as tweepy
Module Assignment
Please read the assignment.md file in the GitHub repository for detailed instructions on completing the following tasks:
- Ensure you can access both the Twitter API and SpaCy package & model via Python, then:
- Write methods to pull actual Users and Tweets and replace your invented data with actual Twitter data
- Add a tweet_vect (the numbers representing the tweets) field to your Tweet model, and functions to populate it with embeddings/vectors returned from the implemented SpaCy model.
Assignment File:
assignment.md
Assignment Solution Video
Check for Understanding
Complete the following items to test your understanding:
- Connect to an API using Python
- Query an API for specific data
- Process and store API responses in your application
- Handle API authentication and rate limits
- Integrate API data with your Flask application