Skip to content

Instantly share code, notes, and snippets.

@nickwebcouk
Forked from fay59/icloud-album-download.sh
Created February 2, 2020 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickwebcouk/0125ac34375409a14366647b6aed6c44 to your computer and use it in GitHub Desktop.
Save nickwebcouk/0125ac34375409a14366647b6aed6c44 to your computer and use it in GitHub Desktop.
Download entire iCloud shared albums
#!/bin/bash
# requires jq
# arg 1: iCloud web album URL
# arg 2: folder to download into (optional)
function curl_post_json {
curl -sH "Content-Type: application/json" -X POST -d "@-" "$@"
}
BASE_API_URL="https://p23-sharedstreams.icloud.com/$(echo $1 | cut -d# -f2)/sharedstreams"
pushd $2 > /dev/null
STREAM=$(echo '{"streamCtag":null}' | curl_post_json "$BASE_API_URL/webstream")
CHECKSUMS=$(echo $STREAM | jq -r '.photos[] | [(.derivatives[] | {size: .fileSize | tonumber, value: .checksum})] | max_by(.size | tonumber).value')
echo $STREAM \
| jq -c "{photoGuids: [.photos[].photoGuid]}" \
| curl_post_json "$BASE_API_URL/webasseturls" \
| jq -r '.items[] | "https://" + .url_location + .url_path' \
| while read URL; do
for CHECKSUM in $CHECKSUMS; do
if echo $URL | grep $CHECKSUM > /dev/null; then
curl -sOJ $URL &
break
fi
done
done
popd > /dev/null
wait
@nickwebcouk
Copy link
Author

#!/bin/bash

requires jq

arg 1: iCloud web album URL

arg 2: folder to download into (optional)

function curl_post_json {
curl -sH "Content-Type: application/json" -X POST -d "@-" "$@"
}

BASE_API_URL="https://p23-sharedstreams.icloud.com/$(echo $1 | cut -d# -f2)/sharedstreams"

pushd $2 > /dev/null
STREAM=$(echo '{"streamCtag":null}' | curl_post_json "$BASE_API_URL/webstream")
HOST=$(echo $STREAM | jq '.["X-Apple-MMe-Host"]' | cut -c 2- | rev | cut -c 2- | rev)

if [ "$HOST" ]; then
BASE_API_URL="https://$(echo $HOST)/$(echo $1 | cut -d# -f2)/sharedstreams"
STREAM=$(echo '{"streamCtag":null}' | curl_post_json "$BASE_API_URL/webstream")
fi

CHECKSUMS=$(echo $STREAM | jq -r '.photos[] | [(.derivatives[] | {size: .fileSize | tonumber, value: .checksum})] | max_by(.size | tonumber).value')

echo $STREAM
| jq -c "{photoGuids: [.photos[].photoGuid]}"
| curl_post_json "$BASE_API_URL/webasseturls"
| jq -r '.items | to_entries[] | "https://" + .value.url_location + .value.url_path + "&" + .key'
| while read URL; do
for CHECKSUM in $CHECKSUMS; do
if echo $URL | grep $CHECKSUM > /dev/null; then
curl -sOJ $URL &
break
fi
done
done

popd > /dev/null
wait

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment