TMDB CLI tool is a Command Line Interface (CLI) tool for fetching movie data from The Movie Database (TMDB). You can fetch movies by type, genre, and page number, displaying results in a structured format. It supports Redis caching for faster responses and efficient API usage. Project idea gotten from roadmap.sh.
- Fetch movies by type (
popular
,now playing
,top-rated
, orupcoming
). - Filter movies by genre.
- Fetch data from specific pages of results.
- Results are displayed in a tabular format for better readability.
- Caches API responses using Redis to optimize performance.
- Supports local and Redis Cloud setups.
- Node.js (version 16 or higher).
- A Redis instance (local or cloud).
- A TMDB account and API key, which can be gotten from TMDB.
-
Clone the repository:
git clone /~https://github.com/kxng0109/tmdb-cli-tool.git cd tmdb-cli-tool
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory and add your TMDB API token and Redis credentials:For local Redis:
TMDB_TOKEN=<your_tmdb_api_token> REDIS_HOST=localhost REDIS_PORT=6379
For Redis Cloud:
TMDB_TOKEN=<your_tmdb_api_token> REDIS_HOST=<your_redis_host> REDIS_PORT=<your_redis_port> REDIS_USERNAME=<your_redis_username> REDIS_PASSWORD=<your_redis_password>
-
Make the CLI executable and link it:
chmod +x app.js npm link
Run the CLI to fetch movies:
tmdb-app -t <type>
Replace <type>
with one of the following options:
popular
(default)playing
(now playing)top
(top rated)upcoming
Option | Description | Default Value |
---|---|---|
-t, --type <type> |
Type of movies to fetch (playing , popular , top , upcoming ). |
popular |
-p, --page <number> |
Page number of the results to fetch. | 1 |
-g, --genre <type> |
Filter results by genre (e.g., Action , Comedy , Drama, e.t.c ). |
None |
-
Fetch popular movies:
tmdb-app -t popular
-
Fetch top-rated movies from page 2:
-tmdb-app -t top -p 2
-
Fetch action movies that are currently popular:
-tmdb-app -g Action
The -g, --genre
option allows you to filter movies by a specific genre. If you provide an invalid genre name, the CLI will return an error.
Results are cached in Redis for improved performance:
- Movies by type and page are cached for 10 minutes.
- Genre lists are cached for 24 hours.
You can configure the Redis connection through the .env
file as described above.
- Install Redis locally on your system.
- Ensure Redis is running on the default port (
6379
).
- Sign up for a free Redis Cloud account here.
- Obtain your Redis Cloud credentials and add them to the
.env
file.
- Fetches movie data from the TMDB API based on the type specified.
- Checks Redis for cached data before making API calls.
- Converts genre IDs to readable text using TMDB genre API.
- Displays the fetched data in the terminal.
.
├── src
│ ├── services
│ │ ├── fetchMovieData.js
│ │ ├── fetchGenres.js
│ │ ├── handleData.js
│ │ └── redisClient.js
│ ├── utils
│ │ ├── errors.js
│ │ ├── logOngoing.js
│ │ ├── logSuccess.js
│ │ ├── myParseInt.js
│ │ └── setMovieType.js
├── app.js
├── package.json
└── .env
app.js
: Entry point for the CLI application.fetchMovieData.js
: Fetches movies from TMDB and caches results in Redis.fetchGenres.js
: Fetches genre data from TMDB and caches it in Redis.errors.js
: Handles error messaging and exits the application on failures.redisClient.js
: Sets up Redis Client using configuration provided in the.env
file or the default values, and connects to the Redis server.setMovieType.js
: Maps user-friendly movie types to TMDB API endpoints.
By default the output will appear in a tabular form, but you can always make changes to handleData.js to appear in JSON format like:
{
"title": "The Shawshank Redemption",
"genre": [ "'Drama'", "'Crime'" ],
"releaseDate": "1994-09-23"
}
{
"title": "The Godfather",
"genre": [ "Drama", "Crime" ],
"releaseDate": "1972-03-14"
}
- Redis Client Error: Ensure your Redis server is running and the credentials in
.env
are correct. - No TMDB Token: Verify that the
TMDB_TOKEN
in your.env
file is accurate.
- Commander.js - CLI framework
- Redis - Caching
- Axios - HTTP requests
- dotenv - Environment variable management
- chalk - Terminal string styling
- cli-table3 - Showing results in tabular form
- Add support for Redis Cloud.
- Enhance the CLI interface with tabular outputs.
- Add more options for fetching movie details.
This project is licensed under the MIT License.
- Please ensure your Redis server is running before using the CLI tool.
- Make sure your
.env
file is correctly configured to avoid runtime errors.