Created
March 11, 2025 03:35
-
-
Save jbranchaud/cf3d2028107a1bd8484eed7cca0fcdab to your computer and use it in GitHub Desktop.
A script to download a public Google Doc from its link, prompts for format with fzf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if fzf is installed | |
if ! command -v fzf &> /dev/null; then | |
echo "Error: fzf is not installed. Please install it first." | |
echo "You can install it with:" | |
echo " - On macOS: brew install fzf" | |
echo " - On Ubuntu/Debian: sudo apt install fzf" | |
echo " - On other systems: see https://github.com/junegunn/fzf#installation" | |
exit 1 | |
fi | |
# Check if curl is installed | |
if ! command -v curl &> /dev/null; then | |
echo "Error: curl is not installed. Please install it first." | |
exit 1 | |
fi | |
# Check arguments | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <google-doc-url> [output-filename]" | |
echo "Example: $0 https://docs.google.com/document/d/1234567890abcdefghijklmnopqrstuvwxyz/edit myfile" | |
echo "Note: If output-filename is provided, the selected format extension will be added to it" | |
exit 1 | |
fi | |
# Extract document ID from URL | |
URL="$1" | |
CUSTOM_FILENAME="$2" | |
# More robust document ID extraction that works across different grep versions | |
if [[ "$URL" =~ /document/d/([^/]+) ]]; then | |
DOC_ID="${BASH_REMATCH[1]}" | |
else | |
# Try alternative extraction method as fallback | |
DOC_ID=$(echo "$URL" | sed -E 's|.*/document/d/([^/?]+).*|\1|') | |
fi | |
if [ -z "$DOC_ID" ]; then | |
echo "Error: Could not extract document ID from URL." | |
echo "Please provide a valid Google Docs URL." | |
exit 1 | |
fi | |
# Format options for fzf (with PDF first) - using simple array instead of associative array | |
FORMAT_OPTIONS=( | |
"pdf|PDF Document (Default)" | |
"docx|Word Document" | |
"odt|OpenDocument Text" | |
"txt|Plain Text" | |
"html|HTML Document" | |
"rtf|Rich Text Format" | |
"epub|EPUB E-book" | |
) | |
# Display format selection with fzf (removed --header-first option) | |
SELECTED_FORMAT=$(printf "%s\n" "${FORMAT_OPTIONS[@]}" | fzf --height=10 --layout=reverse --prompt="Select format to download: " --header="Press Enter to select, Esc to cancel") | |
# Exit if no format was selected (user pressed Esc) | |
if [ -z "$SELECTED_FORMAT" ]; then | |
echo "Download cancelled." | |
exit 0 | |
fi | |
# Extract format code from selection | |
FORMAT=$(echo "$SELECTED_FORMAT" | cut -d'|' -f1) | |
# Define output filename | |
if [ -n "$CUSTOM_FILENAME" ]; then | |
# Use custom filename with format extension | |
FILENAME="${CUSTOM_FILENAME}.$FORMAT" | |
else | |
# Use default naming scheme | |
FILENAME="document.$FORMAT" | |
COUNT=1 | |
# Check if file already exists and create a unique filename | |
while [ -f "$FILENAME" ]; do | |
FILENAME="document-$COUNT.$FORMAT" | |
COUNT=$((COUNT+1)) | |
done | |
fi | |
echo "Downloading document as $FORMAT..." | |
echo "Document ID: $DOC_ID" | |
echo "Output file: $FILENAME" | |
# Download the file | |
curl -L "https://docs.google.com/document/d/$DOC_ID/export?format=$FORMAT" -o "$FILENAME" | |
# Check if download was successful | |
if [ $? -eq 0 ]; then | |
echo "Download complete: $FILENAME" | |
else | |
echo "Error: Download failed." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment