eagle-viewer/eagle_api_import.sh

88 lines
2.4 KiB
Bash
Raw Normal View History

#!/bin/bash
# eagle_api_import.sh
# Run this on your Mac while Eagle is open with the Masterclass library active.
# Uses Eagle's local API to properly import all files from /Volumes/steve/Courses/masterclass/
#
# Usage: bash eagle_api_import.sh
#
# Eagle API docs: https://api.eagle.cool/item/add-from-path
EAGLE_API="http://localhost:41595"
SOURCE="/Volumes/steve/Courses/masterclass"
SUPPORTED_EXTS="mp4 mov m4v mp3 m4a jpg jpeg png gif webp pdf"
is_supported() {
local ext="${1##*.}"
ext=$(echo "$ext" | tr '[:upper:]' '[:lower:]')
for e in $SUPPORTED_EXTS; do
[[ "$e" == "$ext" ]] && return 0
done
return 1
}
# Check Eagle API is up
info=$(curl -s "$EAGLE_API/api/application/info")
if [[ -z "$info" ]]; then
echo "ERROR: Eagle API not reachable at $EAGLE_API. Make sure Eagle is open."
exit 1
fi
echo "Eagle API: OK"
# Check which library is open
lib_info=$(curl -s "$EAGLE_API/api/library/info")
lib_name=$(echo "$lib_info" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d['data']['library']['name'])" 2>/dev/null)
echo "Current library: $lib_name"
echo "Make sure 'Masterclass' library is open in Eagle before continuing."
echo "Press Enter to continue or Ctrl+C to cancel..."
read
DONE=0
ERRORS=0
# Walk the source directory
find "$SOURCE" -type f | sort | while read filepath; do
is_supported "$filepath" || continue
filename=$(basename "$filepath")
ext="${filename##*.}"
name="${filename%.*}"
# Get relative path to determine folder: category/course
rel="${filepath#$SOURCE/}"
category=$(echo "$rel" | cut -d'/' -f1)
course=$(echo "$rel" | cut -d'/' -f2)
# Build folder path tag for Eagle
folder_tag="$category/$course"
# Call Eagle API to add item from path
response=$(curl -s -X POST "$EAGLE_API/api/item/addFromPath" \
-H "Content-Type: application/json" \
-d "{
\"path\": \"$filepath\",
\"name\": \"$name\",
\"tags\": [],
\"annotation\": \"\",
\"folderId\": \"\"
}")
status=$(echo "$response" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('status','unknown'))" 2>/dev/null)
if [[ "$status" == "success" ]]; then
DONE=$((DONE + 1))
else
ERRORS=$((ERRORS + 1))
echo "ERROR: $filepath -> $response"
fi
if (( (DONE + ERRORS) % 50 == 0 )); then
echo "Progress: done=$DONE errors=$ERRORS"
fi
done
echo ""
echo "=== Complete ==="
echo "Imported: $DONE"
echo "Errors: $ERRORS"