|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# ==================================================== |
| 4 | +# Script to Fetch TCGDex Card Information and Populate cards.js |
| 5 | +# ==================================================== |
| 6 | +# |
| 7 | +# This script retrieves card information from specified sets |
| 8 | +# using the TCGDex API and appends the selected data to a |
| 9 | +# JavaScript file (`cards.js`). |
| 10 | +# |
| 11 | +# Requirements: |
| 12 | +# - curl |
| 13 | +# - jq |
| 14 | +# |
| 15 | +# Usage: |
| 16 | +# ./fetch_cards.sh |
| 17 | +# |
| 18 | +# ==================================================== |
| 19 | + |
| 20 | +# -------------------- |
| 21 | +# Configuration Variables |
| 22 | +# -------------------- |
| 23 | + |
| 24 | +# API hostname (modifiable) |
| 25 | +API_HOST="https://api.tcgdex.net/v2/en" |
| 26 | + |
| 27 | +# List of set codes to process (add/remove sets here) |
| 28 | +SETS=("A1" "A1a" "P-A") |
| 29 | + |
| 30 | +# Output file path |
| 31 | +OUTPUT_FILE="js/cards.js" |
| 32 | + |
| 33 | +# Number of cards to fetch per set (useful for testing; set to empty for all) |
| 34 | +CARD_LIMIT="" |
| 35 | + |
| 36 | +# -------------------- |
| 37 | +# Function Definitions |
| 38 | +# -------------------- |
| 39 | + |
| 40 | +# Function to fetch and process card data for a given set |
| 41 | +fetch_set_cards() { |
| 42 | + local set_code="$1" |
| 43 | + echo "Processing set: $set_code" |
| 44 | + |
| 45 | + # Construct the URL for the set |
| 46 | + local set_url="$API_HOST/sets/$set_code" |
| 47 | + |
| 48 | + # Fetch set data |
| 49 | + echo "Fetching set data from $set_url..." |
| 50 | + local set_data |
| 51 | + set_data=$(curl -s "$set_url") |
| 52 | + if [[ $? -ne 0 || -z "$set_data" ]]; then |
| 53 | + echo "Error: Failed to fetch data for set $set_code" |
| 54 | + return 1 |
| 55 | + fi |
| 56 | + |
| 57 | + # Extract card IDs using jq |
| 58 | + local card_ids |
| 59 | + card_ids=$(echo "$set_data" | jq -r '.cards[].id') |
| 60 | + if [[ -z "$card_ids" ]]; then |
| 61 | + echo "Warning: No cards found for set $set_code" |
| 62 | + return 0 |
| 63 | + fi |
| 64 | + |
| 65 | + # Apply card limit if specified |
| 66 | + if [[ -n "$CARD_LIMIT" ]]; then |
| 67 | + card_ids=$(echo "$card_ids" | head -n "$CARD_LIMIT") |
| 68 | + fi |
| 69 | + |
| 70 | + # Iterate over each card ID and fetch card details |
| 71 | + for card_id in $card_ids; do |
| 72 | + echo " Fetching card data for ID: $card_id" |
| 73 | + |
| 74 | + # Construct the URL for the card |
| 75 | + local card_url="$API_HOST/cards/$card_id" |
| 76 | + |
| 77 | + # Fetch card data |
| 78 | + local card_data |
| 79 | + card_data=$(curl -s "$card_url") |
| 80 | + if [[ $? -ne 0 || -z "$card_data" ]]; then |
| 81 | + echo " Error: Failed to fetch data for card ID $card_id" |
| 82 | + continue |
| 83 | + fi |
| 84 | + |
| 85 | + # Extract required fields using jq |
| 86 | + local extracted |
| 87 | + extracted=$(echo "$card_data" | jq '{category, id, image, localId, name, hp, types, stage, attacks, retreat, evolveFrom}') |
| 88 | + |
| 89 | + # Append the extracted data to the output file |
| 90 | + echo "$extracted," >> "$OUTPUT_FILE" |
| 91 | + done |
| 92 | +} |
| 93 | + |
| 94 | +# -------------------- |
| 95 | +# Main Script Execution |
| 96 | +# -------------------- |
| 97 | + |
| 98 | +# Ensure the output directory exists |
| 99 | +mkdir -p "$(dirname "$OUTPUT_FILE")" |
| 100 | + |
| 101 | +# Initialize/Empty the output file |
| 102 | +echo "// Auto-generated cards data" > "$OUTPUT_FILE" |
| 103 | +echo "const cards = [" >> "$OUTPUT_FILE" |
| 104 | + |
| 105 | +# Iterate over each set and fetch card data |
| 106 | +for set_code in "${SETS[@]}"; do |
| 107 | + fetch_set_cards "$set_code" |
| 108 | +done |
| 109 | + |
| 110 | +# Close the JavaScript array in the output file |
| 111 | +echo "];" >> "$OUTPUT_FILE" |
| 112 | + |
| 113 | +echo "All sets processed. Data saved to $OUTPUT_FILE" |
0 commit comments