Skip to content

Instantly share code, notes, and snippets.

@luandro
Last active August 31, 2023 15:20
Show Gist options
  • Save luandro/fbf3008ac9417a792c2c2db9aab6914a to your computer and use it in GitHub Desktop.
Save luandro/fbf3008ac9417a792c2c2db9aab6914a to your computer and use it in GitHub Desktop.
Downloads Google Drive attachments from a EML file.
#!/bin/bash
# Check if an argument has been provided
if [ $# -eq 0 ]; then
echo "No argument provided. Please enter the .eml file path:"
read file_path
else
file_path=$1
fi
# Check if the provided .eml file exists
if [ ! -f $file_path ]; then
echo "File $file_path not found!"
exit 1
fi
# Create a 'files' directory if it doesn't already exist
mkdir -p files
# Read the .eml file and extract all the Google Drive links
echo "Extracting Google Drive links from $file_path..."
links=$(cat $file_path | grep -o 'https://drive.google.com/file/d/[^/]*/')
# Loop over the links, extract the file ID from each link, and use wget to download each file into the 'files' directory
for link in $links; do
file_id=$(echo $link | awk -F'/' '{ print $(NF-1) }')
file_path="files/$file_id"
if [ -f $file_path ] && [ -s $file_path ]; then
echo "File $file_path already exists and is not empty, skipping download."
else
echo "Downloading file $file_id..."
success=0
while [ $success -eq 0 ]; do
wget --no-check-certificate "https://drive.google.com/uc?export=download&id=$file_id" -O $file_path
if [ $? -eq 0 ]; then
success=1
echo "Downloaded file $file_id successfully."
else
echo "Download failed. Retrying in 10 seconds..."
sleep 10
fi
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment