TMDB API Python Library: A comprehensive Python library for interacting with TheMovieDB API, enabling easy access to movie and TV show data, including details, ratings, and more. Simplify your movie-related projects with this powerful Python tool.
- TMDB API Getting Started
- Information about TMDB API: Include meta data about the TMDB API and methods, request format.
- Contribution Bible - TMDB
- Supports both TMDB API version 3 and 4, including all methods.
- Provide all parameters in TMDB API methods.
- Includes parameter checking for methods.
- Offers credential management.
-
Install from PyPI
pip install TMDB-Py
-
Download the latest release from Release v1.1.1
pip install TMDB-Py-1.1.1.tar.gz
-
Set your credentials
The simplest way is to provide an API key. An API key authorizes access to TMDB API version 3. If an access token is given, it allows access to both version 3 and version 4.
import tmdbapi cred = tmdbapi.Credential() # Initialize a credential management object cred.set(api_key="your_api_key") # Set the API key tmdbapi.setting.use_cred(cred) # Load the credential
-
Now, You can now access API version 3 easily:
from tmdbapi import api3, pprint response = api3.movies.details(155) # Send the request pprint(response) # Pretty print the response
-
To access both version 3 and 4, you need to set access_token.
cred.set(access_token="your_access_token", account_object_id="your_account_object_id") response = tmdbapi.api4.account.favorite_movies() # Get a list of your favorite movies. pprint(response)
[!NOTE]
In version 3, accessing account catalog methods generally requires both theaccount_id
andsession_id
, with the exception ofapi3.account.details
, which does not need theaccount_id
. In version 4, you require theaccount_object_id
Thank you for considering contributing to the TMDB API Library! Your contributions are highly valued and can help make this project even more powerful.
If you discover any bugs, issues, or have suggestions for improvements, please open an issue. When reporting issues, be sure to provide detailed information about the problem, including steps to reproduce it.
Robust test coverage is crucial for maintaining a stable project. You can contribute by adding more test cases to ensure that the code behaves correctly in various scenarios. We use pytest for testing.
If you have ideas for high-level functions that can improve the library's usability or provide new features, feel free to implement them and create a pull request.
Contributions that introduce new features or enhance existing ones are always welcome.
-
The credential management provides methods to save and load credentials from files and manage credentials in system environment variables.
-
import tmdbapi cred = tmdbapi.Credential() # Initialize credential management object # You can also provide multiple pieces of information at once cred.set(api_key="your_api_key", access_token="your_access_token", session_id="your_access_token",)
All available information:
access_token, api_key, session_id, account_id, account_object_id[!Note]
To obtain youraccount_id
, use theapi3.account.details
method (requiressession_id
; if theaccess_token
is provided, thesession_id
can be optional). If neithersession_id
noraccess_token
is provided, when needed, it will automatically start the generation process. Alternatively, you can generate it usingtmdbapi.integration.auth.create_session_id()
.To acquire your
account_object_id
, you need to generate a "write"access_token
. This process will return both theaccount_object_id
and the "write"access_token
.[!Note]
The originalaccess_token
provided by the TMDB API only allows reading. If you need to perform operations such as managing lists, you must generate a "write"access_token
usingtmdbapi.integration.auth.create_session_id()
. Remember to keep it. -
# Choose one option: cred.save("path/to/file") # For unencrypted storage cred.save_encrypt("path/to/file") # For encrypted storage
-
cred = tmdbapi.Credential() # Create a new credential object cred.load("path/to/file") # Load the credential file (unencrypted) cred.load_encrypt("path/to/file") # Load the encrypted credential file
-
cred.cred.cred_to_env() # put on environment variables cred.load_env_var() # load from environment variables
-
-
-
After setting up the credential object, TMDB API still needs to be aware of your credentials. You must add them to the settings:
tmdbapi.setting.use_cred(cred) # Inform TMDB API that you are using these credentials
-
[!NOTE]
The API key is used only for version 3 of the API. An access token can be used for both versions. When using an API key, some methods in version 3 require a session ID.# If access token was given you can set use_access_token to True tmdbapi.setting.use_access_token(True)
[!NOTE]
When you provide the access token in the credential object, it will set theuse_access_token
to True by default when you load the credential in the settings usingsetting.use_cred()
-
tmdbapi.setting.timeout(5) # Set a timeout to 5 seconds
-
When you request method have language parameter and no language given, it will use the default language to the parameter.
tmdbapi.setting.language('en-US') # Set the default language to 'en-US'
-
tmdbapi.setting.region('US') # Set the default region to 'US'
-
More information: requests.Session
tmdbapi.setting.use_session(True) # Use requests.Session() tmdbapi.setting.use_session(False) # Send requests without cookies
-
tmdbapi.setting.log('directory/to/save/the/log') # Enable the log file output tmdbapi.setting.log(None) # Disable the log file output (Default)
To change the log level:
import logging tmdbapi.LOGGER.setLevel(logging.INFO) # Change the log level to INFO
[!WARNING]
Keep your logs safe and do not share them with others. Due to the TMDB API's practice of including the API key and session ID in the URL, these details may be logged. Further when you change the logger level toDEBUG
, it will also recode send headers, where the access token is.
-
-
-
TMDB API version 3 methods and endpoints. Import the
api3
sub-package to access the modules. Each module contains a category of methods, and each function in the module is an API method, with categories matching those of the TMDB API.from tmdbapi import api3 api3.catalog_name.method_name()
-
TMDB API version 4 methods and endpoints, structured and used in the same way as
api3
.from tmdbapi import api4 api4.catalog_name.method_name()
-
This section provides high-level functions and integration features to simplify interactions with TMDB.
# Example from tmdbapi.integration import auth auth.create_access_token()
-
When using the api3.lists.delete
request to delete a list on TheMovieDB, you may encounter the following error response:
response 500 - Internal Server Error
response json:
{
"success": false,
"status_code": 11,
"status_message": "Internal error: Something went wrong, contact TMDb."
}
The error is at TMDB API, please use api4 instead, or you may need to catch the exception. Issue on TMDB support
Run unit test:
python run_test.py