Snippets

Juan Riano Cloudinary Downloader

Created by Juan Riano last modified
#!/bin/bash

#
# By Juan Riano
#
# This script downloads all the files from a Cloudinary account,
# And saves the results in the same file structure as stored online.
# It also saves json files with the details of the downloaded files.
#
# usage:
# > ./cloudinary_downloader.sh 2>&1 | tee /home/jriano/cloudinary_uploads/_log.log
#

# Cloudinary credentials
c_account=youraccountid
c_api_key=yourapikey
c_api_secret=yourapisecret

# Variables
home=$( echo $HOME )
home_dir="$home/cloudinary_uploads"
home_uploads="$home_dir/uploads"
home_reports="$home_dir/reports"
listing_json="$home_reports/_LISTING.json"
listing_counter=1
listing_prefix="listing_"
cursors_list="$home_reports/_CURSORS.txt"
get_listing_str="https://$c_api_key:$c_api_secret@api.cloudinary.com/v1_1/$c_account/resources/image"

# Create working forlders if not present
mkdir -p $home_reports
mkdir -p $home_uploads

# Get first list
max_results="?max_results=50"
curl "$get_listing_str$max_results" > "$listing_json"
cat "$listing_json" | jq . > "${home_reports}/${listing_prefix}1.json"

next_cursor=$( cat $listing_json | jq . | grep next_cursor | awk -F': ' '{print $2}' | awk -F'"' '{print $2}')

# Initialize cursors file
echo "" > "$cursors_list"

# If there are more files, keep on getting the lists
while ! [ -z $next_cursor ]; do
    (( listing_counter ++ ))
    echo ""
    echo " Next cursor:"
    echo "$next_cursor" | tee -a "$cursors_list"
    curl "$get_listing_str$max_results&next_cursor=$next_cursor" > "$listing_json"
    # Create copy of listing
    cat "$listing_json" | jq . > "${home_reports}/${listing_prefix}${listing_counter}.json"
    next_cursor=$( cat $listing_json | jq . | grep next_cursor | awk -F': ' '{print $2}' | awk -F'"' '{print $2}')
done

# Now go throgh the lists and download the files keeping the folder structure
lists=$( ls "${home_reports}" | grep "${listing_prefix}")
for list in $lists; do
    echo ""
    echo "***** Working with list: $list"
    pics=$( cat "${home_reports}/${list}" | grep "secure_url" | awk -F'https://' '{print $2}' | awk -F'"' '{print $1}' )
    for pic in $pics; do
        echo ""
        echo "* Downloading: $pic"
        # Create pic's folder if it's not there
        pic_dir=$( echo "$pic" | grep -oE "([^/]+[/])+" )
        mkdir -p "${home_uploads}/${pic_dir}"
        # Get the file
        curl "https://${pic}" > "${home_uploads}/${pic}"
    done
done

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.