#!/bin/bash

# URL of the text file
URL="https://josh47.com/dynmapspam.txt"

# Name of the output CSV file
OUTPUT_CSV="output.csv"

# Fields for the API query
API_FIELDS="status,continent,continentCode,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,offset,currency,isp,org,as,asname,reverse,mobile,proxy,hosting,query"

# Download the text file
wget -q "$URL" -O dynmapspam.txt

# Check if the download was successful
if [ $? -ne 0 ]; then
  echo "Failed to download the file."
  exit 1
fi

# Count the total number of lines in the text file
TOTAL_LINES=$(wc -l < dynmapspam.txt)

# Set the batch size to 45
BATCH_SIZE=45

# Calculate how many batches are needed
TOTAL_BATCHES=$((TOTAL_LINES / BATCH_SIZE + 1))

# Loop through each batch
for ((batch = 1; batch <= TOTAL_BATCHES; batch++)); do
  # Calculate the start and end lines for this batch
  start_line=$((BATCH_SIZE * (batch - 1) + 1))
  end_line=$((BATCH_SIZE * batch))

  # Loop through each line in the current batch
  for ((line_num = start_line; line_num <= end_line && line_num <= TOTAL_LINES; line_num++)); do
    # Get the IP address from the text file
    IP_ADDRESS=$(sed -n "${line_num}p" dynmapspam.txt)

    # Construct the dynamic URL with fields
    INFO_URL="http://ip-api.com/csv/$IP_ADDRESS?fields=$API_FIELDS"

    # Fetch additional information about the IP address from ip-api.com in CSV format using curl
    INFO_CSV=$(curl -s "$INFO_URL")

    # Check if the curl request was successful
    if [ $? -ne 0 ]; then
      echo "Failed to fetch information for IP address: $IP_ADDRESS"
      continue
    fi

    # Append the data to the output CSV file
    echo "$INFO_CSV" >> "$OUTPUT_CSV"

    # Add your custom code here for processing the fetched information.
  done

  # If this is not the last batch, wait for a minute
  if [ $batch -lt $TOTAL_BATCHES ]; then
    sleep 60
  fi
done

# Add the headers at the top of the CSV file
echo "$API_FIELDS" > headers.tmp
sed -i "1s;^;$API_FIELDS\n;" "$OUTPUT_CSV"
rm headers.tmp

# Cleanup: Remove the downloaded file
rm dynmapspam.txt
