Lesson 3: Movie Database Blueprint with OMDB API
Build a movie database application that stores movies and fetches detailed information, images, and metadata from the OMDB (Open Movie Database) API.
Lesson Objectives
- Create the movies blueprint with full CRUD operations
- Build a movies database table to store movie information
- Implement forms to add new movies by title
- Integrate with the OMDB API to fetch movie details, posters, and metadata
- Create 'Fetch Data' functionality to retrieve complete movie information
- Implement View, Edit, and Delete operations for movies
Movie Database Blueprint with OMDB API - Notes
Movie Database Feature Overview
The movies blueprint allows users to:
- Add movies by title (e.g., "Dances with Wolves")
- Fetch complete movie data from OMDB API (director, actors, plot, poster image)
- View detailed movie information with images
- Edit movie information
- Delete movies from the database
Example: Add "Dances with Wolves", click "Fetch Data", and it will populate director (Kevin Costner), year, plot, and poster image automatically.
Database Schema
Create a movies table with the following columns:
- movie_id: Integer, Primary Key, Auto-increment
- title: Text (e.g., 'Dances with Wolves')
- director: Text (e.g., 'Kevin Costner')
- year: Text or Integer (release year)
- plot: Text (movie synopsis)
- poster: Text (URL to poster image)
- actors: Text (comma-separated list of actors)
- genre: Text (movie genre)
Blueprint Routes
Your movies blueprint should have these routes:
@movies_bp.route('/movies')
def index():
# Display all movies
@movies_bp.route('/movies/add', methods=['POST'])
def add_movie():
# Add movie with just the title
@movies_bp.route('/movies/fetch/<int:movie_id>')
def fetch_data(movie_id):
# Fetch data from OMDB API and update
@movies_bp.route('/movies/view/<int:movie_id>')
def view_movie(movie_id):
# View detailed movie information
@movies_bp.route('/movies/edit/<int:movie_id>', methods=['GET', 'POST'])
def edit_movie(movie_id):
# Edit movie information
@movies_bp.route('/movies/delete/<int:movie_id>')
def delete_movie(movie_id):
# Delete movie from database OMDB API Integration
The OMDB (Open Movie Database) API provides detailed movie information:
- Free API Key: Get one at omdbapi.com
- Data Returned: Title, Year, Director, Actors, Plot, Poster, Genre, Ratings
Example API call to fetch movie data:
import requests
def get_movie_data(title):
api_key = 'YOUR_OMDB_API_KEY'
url = 'http://www.omdbapi.com/'
params = {
'apikey': api_key,
't': title # Search by title
}
response = requests.get(url, params=params)
data = response.json()
if data.get('Response') == 'True':
return {
'title': data.get('Title'),
'director': data.get('Director'),
'year': data.get('Year'),
'plot': data.get('Plot'),
'poster': data.get('Poster'),
'actors': data.get('Actors'),
'genre': data.get('Genre')
}
return None Form Implementation
Create a simple form to add movies by title:
<form action="/movies/add" method="POST">
<input type="text" name="title"
placeholder="Movie Title (e.g., Dances with Wolves)"
required>
<button type="submit">Add Movie</button>
</form>Initially, only the title is saved. Users click "Fetch Data" to populate other fields from the API.
View Movie Page
The view page should display:
- Movie poster image
- Title and year
- Director and actors
- Plot/synopsis
- Genre
Note: The demo mentions this can be "a little bit buggy" - make sure to handle cases where the API doesn't return all fields.