This project demonstrates how to create an application that allows you to retrieve data about popular movies using the free OMDb API and save the results to a file for further analysis. In this article, we will go through the steps needed to create such a script.
Step 1: Install Required Libraries
First, set up your environment and install the necessary libraries:
pip install requests pandasThese libraries are needed for sending HTTP requests to the API (requests) and for working with tabular data (pandas).
Description:
pip install requests pandas: This command installs two libraries.requestsis used to interact with external APIs via HTTP requests, andpandasis used to work with tabular data and conveniently save it into CSV files.
Step 2: Obtain an API Key
Register on the OMDb API website to get a free API key. The free tier allows up to 1,000 requests per day, which is perfect for small projects or testing purposes.
Step 3: Writing a Script to Retrieve Movies by Keyword
Next, we will create a function that uses the OMDb API to search for movies by keyword. Dividing the code into several functions helps keep it understandable and easy to maintain.
import requests
import pandas as pd
API_KEY = "your_omdb_api_key" # Replace with your API key
BASE_URL = "http://www.omdbapi.com/"Description:
import requests: Importing the library to send HTTP requests to the API.import pandas as pd: Importing the library to work with tabular data, shortening the name for convenience.API_KEYandBASE_URL: These variables contain the API key and the base URL for interacting with the OMDb API.
Step 4: Function to Search Movies by Keyword
The following function will use the OMDb API to get a list of movies that match a specified keyword:
def fetch_movies(keyword, max_results=10):
"""
Function to retrieve a list of movies by keyword.
"""
movies = []
page = 1
while len(movies) < max_results:
response = requests.get(
BASE_URL,
params={
"apikey": API_KEY,
"s": keyword,
"type": "movie",
"page": page
}
)
if response.status_code != 200:
print("Request error:", response.status_code)
break
data = response.json()
if data.get("Response") == "False":
print("Error:", data.get("Error"))
break
movies.extend(data.get("Search", []))
if len(movies) >= max_results or len(data.get("Search", [])) == 0:
break
page += 1
return movies[:max_results]Description:
fetch_movies(keyword, max_results=10): This function takes a keyword (keyword) and the maximum number of results (max_results).requests.get(): Sends a GET request to the OMDb API to fetch the list of movies.- Request parameters (
params): apikey: Your API key for authorization.s: The keyword for searching movies.type: Specifies the type of item to search for (in this case, movies).page: The page number to navigate through results.whileloop: Keeps making requests until the maximum number of movies is reached or results run out.data.json(): Converts the response from JSON format into a Python object.
Step 5: Retrieve Detailed Information About Each Movie
After obtaining a list of movies, we need to gather more detailed information for each of them:
def get_movie_details(movie_id):
"""
Retrieve detailed information about a movie.
"""
response = requests.get(
BASE_URL,
params={
"apikey": API_KEY,
"i": movie_id
}
)
if response.status_code == 200:
return response.json()
else:
print("Error retrieving data for movie:", movie_id)
return {}Description:
get_movie_details(movie_id): This function takes a movie identifier (movie_id) and returns detailed information.requests.get(): Sends a request to the API to get information about a specific movie using its identifier (i).status_codecheck: If the request is successful (status code 200), the response is returned in JSON format. Otherwise, an error message is printed.
Step 6: Saving Data to a CSV File
Now that we have complete information about the movies, let's save it to a CSV file for further analysis:
def save_to_csv(movies, filename="movies.csv"):
"""
Save the list of movies to a CSV file.
"""
df = pd.DataFrame(movies)
df.to_csv(filename, index=False)
print(f"Data saved to {filename}")Description:
save_to_csv(movies, filename="movies.csv"): This function takes the list of movies and the file name where the data will be saved.pd.DataFrame(movies): Creates a DataFrame from the list of dictionaries containing movie data.to_csv(): Saves the DataFrame to a CSV file specified infilename.
Step 7: Main Program
Now, let's put it all together. Here's how the main program looks, connecting all our functions:
def main():
keyword = input("Enter a keyword to search for movies: ")
max_results = int(input("How many movies do you want to retrieve? "))
print(f"Searching for movies with the keyword '{keyword}'...")
movies = fetch_movies(keyword, max_results)
detailed_movies = []
for movie in movies:
print(f"Retrieving details for movie: {movie['Title']}...")
details = get_movie_details(movie["imdbID"])
if details:
detailed_movies.append({
"Title": details.get("Title"),
"Year": details.get("Year"),
"Genre": details.get("Genre"),
"Director": details.get("Director"),
"Actors": details.get("Actors"),
"IMDB Rating": details.get("imdbRating"),
"Plot": details.get("Plot")
})
save_to_csv(detailed_movies)
print("Done!")
if __name__ == "__main__":
main()Description:
main(): The main function that ties all components together.input(): Prompts the user to enter a keyword for the search and the number of movies to retrieve.fetch_movies(): Retrieves a list of movies based on the keyword.forloop: Iterates through each movie and retrieves detailed information usingget_movie_details().save_to_csv(): Saves the results to a CSV file.
Conclusion
This project demonstrates how to integrate external APIs into your applications and process data using Python. We used the OMDb API to retrieve information about movies and saved that data in CSV format for further analysis.
Potential Improvements
- Graphical User Interface (GUI): Add an interface using libraries such as
tkinterorPyQtto make the application easier to use. - Automatic Data Analysis: Use Python capabilities to analyze the saved data, for example, finding the highest-rated movies or plotting graphs using the
matplotliblibrary. - Recommendation System: Try using the saved data to create a simple recommendation system that helps users choose movies to watch.
In Plain English 🚀
Thank you for being a part of the In Plain English community! Before you go:
- Be sure to clap and follow the writer ️👏️️
- Follow us: X | LinkedIn | YouTube | Discord | Newsletter | Podcast
- Create a free AI-powered blog on Differ.
- More content at PlainEnglish.io