Skip to content

Instantly share code, notes, and snippets.

@biesnecker
Created December 1, 2024 14:23
Show Gist options
  • Save biesnecker/499305a943eba65910fe8a065819df03 to your computer and use it in GitHub Desktop.
Save biesnecker/499305a943eba65910fe8a065819df03 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if an argument is passed
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <date in YYYYDD format>"
exit 1
fi
# Extract year and day from the argument
INPUT_DATE="$1"
YEAR="${INPUT_DATE:0:4}" # First 4 characters (YYYY)
DAY="${INPUT_DATE:4:2}" # Last 2 characters (DD)
# Remove leading zero from day if present
DAY_NO_LEADING_ZERO=$((10#$DAY))
# Construct the filename with .py extension
FILENAME="${INPUT_DATE}.py"
# Create the file
touch "$FILENAME"
if [ $? -ne 0 ]; then
echo "Error: Unable to create file '$FILENAME'."
exit 1
fi
echo "File '$FILENAME' created successfully."
# Custom header string (modify as needed)
HEADER="Cookie: session=<your session key here>"
# Construct the URL
URL="https://adventofcode.com/${YEAR}/day/${DAY_NO_LEADING_ZERO}/input"
INPUT_FILENAME="${INPUT_DATE}.txt"
# Call wget with the custom header and output to the created file
wget --header="$HEADER" "$URL" -O "$INPUT_FILENAME"
if [ $? -ne 0 ]; then
echo "Error: wget command failed."
exit 1
fi
echo "Content from '$URL' downloaded successfully to '$INPUT_FILENAME'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment