Files
FitTrack_GarminSync/download_activity.sh

75 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# This script connects to CentralDB, lists available activities, and allows the user to download a selected activity file.
# --- Configuration ---
CENTRAL_DB_URL="http://localhost:8000" # Adjust if your CentralDB is running on a different host/port
# --- Function to display error and exit ---
function error_exit {
echo "Error: $1" >&2
exit 1
}
# --- Get list of activities ---
echo "Fetching list of activities from CentralDB..."
ACTIVITIES_JSON=$(curl -s "${CENTRAL_DB_URL}/activities")
if [ -z "${ACTIVITIES_JSON}" ]; then
error_exit "No response received from CentralDB. Is the CentralDB service running?"
fi
# Check if jq is installed
if ! command -v jq &> /dev/null; then
error_exit "jq is not installed. Please install jq to parse JSON output (e.g., sudo apt-get install jq or brew install jq)."
fi
# Parse JSON and display activities
ACTIVITIES_COUNT=$(echo "${ACTIVITIES_JSON}" | jq '. | length')
if [ "${ACTIVITIES_COUNT}" -eq 0 ]; then
echo "No activities found in CentralDB."
exit 0
fi
echo "Available Activities:"
echo "${ACTIVITIES_JSON}" | jq -c '.[] | {id: .id, file_path: .file_path, filename: .activity_metadata.filename}' | \
nl -w 2 -s ') ' | while read -r line; do
ACTIVITY_ID=$(echo "$line" | sed -E 's/^[[:space:]]*[0-9]+) {id:([0-9]+),.*$/\1/')
FILENAME=$(echo "$line" | sed -E 's/^[[:space:]]*[0-9]+) {id:[0-9]+, file_path:".*", filename:"([^"]+)".*$/\1/')
echo "$line (ID: ${ACTIVITY_ID}, Filename: ${FILENAME})"
done
# --- Prompt user for selection ---
read -p "Enter the number of the activity to download: " SELECTION_NUMBER
if ! [[ "${SELECTION_NUMBER}" =~ ^[0-9]+$ ]]; then
error_exit "Invalid input. Please enter a number."
fi
if [ "${SELECTION_NUMBER}" -lt 1 ] || [ "${SELECTION_NUMBER}" -gt "${ACTIVITIES_COUNT}" ]; then
error_exit "Invalid selection. Please enter a number between 1 and ${ACTIVITIES_COUNT}."
fi
# Get selected activity details
SELECTED_ACTIVITY=$(echo "${ACTIVITIES_JSON}" | jq ".[$((${SELECTION_NUMBER} - 1))]" -c)
SELECTED_ACTIVITY_ID=$(echo "${SELECTED_ACTIVITY}" | jq -r '.id')
SELECTED_FILENAME=$(echo "${SELECTED_ACTIVITY}" | jq -r '.activity_metadata.filename')
if [ -z "${SELECTED_ACTIVITY_ID}" ] || [ -z "${SELECTED_FILENAME}" ]; then
error_exit "Could not retrieve details for selected activity."
fi
# --- Download the selected activity file ---
echo "Downloading activity ID: ${SELECTED_ACTIVITY_ID} (Filename: ${SELECTED_FILENAME})..."
DOWNLOAD_URL="${CENTRAL_DB_URL}/activities/${SELECTED_ACTIVITY_ID}/file"
# Use curl to download the file and save it with the correct filename
curl -s -o "${SELECTED_FILENAME}" "${DOWNLOAD_URL}"
if [ $? -eq 0 ]; then
echo "Successfully downloaded ${SELECTED_FILENAME}"
else
error_exit "Failed to download activity file."
fi