Magic: The Gathering Card Downloader is a Node.js project designed to download images of Magic: The Gathering cards listed in a CSV file. The CSV file contains card names as the first field, and the application processes each row, downloads the card art, and saves it to a local directory. The project utilizes TypeScript and various Node.js packages for handling CSV, logging, rate limiting, and HTTP requests.
- Make sure you have Node.js installed on your machine.
- Clone or download the repository from GitHub.
- Open a terminal and navigate to the project's root directory.
- Run the following command to install the project dependencies:
npm install
- Prepare a CSV file containing card names. The first column of the CSV file should contain the card names.
- Place the CSV file(s) inside the
./card_lists
directory. - To start the card downloading process, execute the following command:
The application will read the CSV file(s) in the
npm run start
./card_lists
directory, process each card name, and download the card art. The downloaded images will be saved in the./card_art_downloads
directory.
dev
: Starts the development server usingvitest
.test
: Executes tests usingvitest
.build
: Builds the TypeScript code usingtsup
, generating CommonJS and ES modules, as well as TypeScript declaration files.start
: Similar tobuild
, but it then runs the built application usingnode
.lint
: Lints the TypeScript code using the TypeScript compiler (tsc
) without emitting files.ci
: Executes linting, tests, and builds the project for Continuous Integration (CI) purposes.
The code is divided into three main functions:
Fetches card details from an external API using the provided card name.
Parameters:
name
(string): The name of the card to fetch.
Returns: A promise that resolves with the fetched card details as an object.
Asynchronously downloads card art from the provided URL and saves it to a file with the given card name.
Parameters:
url
(string): The URL of the card art to download.cardName
(string): The name of the card used for saving the file.
Makes an asynchronous HTTP request and returns the response data as a specified type.
Type Parameters:
TResponse
: The type of the response data to be returned.
Parameters:
url
(string): The URL of the API endpoint to make the request to.config
(RequestInit): Optional configuration for the fetch request. Default is an empty object.
Returns: A promise that resolves with the response data as the specified type.
- The application reads CSV files from the
./card_lists
directory. - For each CSV file, it processes the card names, standardizes them, and stores them in the
cardList
array. - The application creates a rate limiter to control the number of requests made per second. The limiter allows 10 requests per second.
- The application iterates over the
cardList
array and fetches card details from an external API using each card name. - If the card has an 'image_uris' property, it is treated as a single face card, and its card art is downloaded and saved.
- If the card is a double face card, both faces' card art is downloaded and saved with appropriate naming.
The project can be extended in the future to include more download options or additional features, such as:
- Downloading card arts in different sizes or formats.
- Enhancing the search options for card names (e.g., fuzzy search).
- Adding support for other external APIs or data sources for card details.
- Improving error handling and logging for a more robust application.
Note: The package.json
scripts mentioned above assume you have pnpm installed. If you prefer using npm or yarn, you can replace pnpm
with npm
or yarn
in the commands accordingly.