#!/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 credentialsc_account=youraccountid
c_api_key=yourapikey
c_api_secret=yourapisecret
# Variableshome=$(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=1listing_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 presentmkdir -p $home_reportsmkdir -p $home_uploads# Get first listmax_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 fileecho"" > "$cursors_list"# If there are more files, keep on getting the listswhile ! [ -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 structurelists=$( ls "${home_reports}"| grep "${listing_prefix}")for list in $lists;doecho""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;doecho""echo"* Downloading: $pic"# Create pic's folder if it's not therepic_dir=$(echo"$pic"| grep -oE "([^/]+[/])+") mkdir -p "${home_uploads}/${pic_dir}"# Get the file curl "https://${pic}" > "${home_uploads}/${pic}"donedone
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.