jmanteau

Mon coin de toile - A piece of Web

Streamlining Google Drive Downloads With a Zsh Snippet

Posted: Nov 1, 2023
Streamlining Google Drive Downloads With a Zsh Snippet

As a terminal user, I sometime find myself in situations where I need to download files from Google Drive directly into my local machine or a remote server. While Google Drive is a fantastic tool for storing and sharing files, it doesn’t provide a straightforward way to download files via the command line. This can be particularly cumbersome when working with headless servers or when you’re trying to automate your workflow.

To address this challenge, I’ve crafted a Zsh snippet that simplifies the process of downloading files from Google Drive using only the terminal. In this blog post, I’ll share this snippet with you, explain how it works, and provide documentation for its usage.

The Challenge

Google Drive shareable links are designed for use in a web browser. They typically look something like this:

https://drive.google.com/file/d/FILEID/view?usp=sharing

While this link is perfect for sharing, it’s not directly usable in a terminal for downloading the file. The challenge lies in extracting the file ID and filename from the URL and then using them to construct a command that can fetch the file via the command line.

The Solution: A Zsh Snippet

To overcome this challenge, I created a Zsh function that automates the process. I’ve named it gdrive_dl. Here’s the snippet along with its documentation:

# Function to download files from Google Drive using the shareable link
# Documentation:
# gdrive_dl: A function to download files from Google Drive using the shareable link.
# Usage: gdrive_dl <URL>
# <URL>: The full shareable link of the Google Drive file you wish to download.
function gdrive_dl() {
  local URL="$1"

  if [[ -z "$URL" ]]; then
    echo "Usage: gdrive_dl <URL>"
    return 1
  fi

  # Extract FILEID from the URL
  local FILEID=$(echo "$URL" | grep -o 'd/[^/]*' | cut -d'/' -f2)

  # Fetch the webpage content and extract the filename using awk
  local FILENAME=$(curl -s "$URL" | awk -F'"' '/<meta itemprop="name" content="/ {print $4}')

  if [[ -z "$FILENAME" ]]; then
    echo "Could not extract the filename from the URL."
    return 1
  fi

  wget --load-cookies /tmp/cookies.txt \
    "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=${FILEID}' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=${FILEID}" \
    -O "${FILENAME}" && rm -rf /tmp/cookies.txt
}

How It Works

  1. Extracting the File ID: The function starts by extracting the file ID from the provided Google Drive URL. This is done using grep and cut commands.
  2. Extracting the Filename: Next, it uses curl to fetch the webpage content and awk to parse the HTML and extract the filename. This step is crucial because the filename is not part of the URL but is embedded within the webpage’s metadata.
  3. Downloading the File: With the file ID and filename in hand, the function constructs a wget command that handles the download. It manages cookies and session data to mimic a browser download, ensuring that Google Drive’s download confirmation is appropriately handled.
  4. Cleaning Up: After the download is complete, the function cleans up by removing the temporary cookies file.

Focus on Downloading the file

Let’s detail a bit more the core of the logic:

wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(\
    wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=${FILEID}' -O- \
    | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=${FILEID}" \a -O "${FILENAME}" \
     && rm -rf /tmp/cookies.txt

Initial Request and Cookie Handling:

wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=${FILEID}' -O-

Extracting the Confirmation Token:

sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p'

Downloading the File:

wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(...)&id=${FILEID}" -O "${FILENAME}"

Cleanup:

rm -rf /tmp/cookies.txt

In summary, this part of the function handles the interaction with Google Drive’s download mechanism, managing cookies and confirmation tokens to successfully download the requested file.

Usage

To use this function, simply source it in your .zshrc file or any Zsh configuration file you use. Then, you can download a file from Google Drive by calling the function with the shareable link:

gdrive_dl https://drive.google.com/file/d/FILEID/view?usp=sharing

Conclusion

This Zsh snippet, gdrive_dl allows me to download files from Google Drive with ease directly from the terminal. I hope you find this snippet as useful as I have, and I encourage you to integrate it into your command-line toolkit.