Skip to content

Instantly share code, notes, and snippets.

@lmaertin
Last active May 3, 2023 07:12
Show Gist options
  • Save lmaertin/58c5566b3c69a159e393389241f3db76 to your computer and use it in GitHub Desktop.
Save lmaertin/58c5566b3c69a159e393389241f3db76 to your computer and use it in GitHub Desktop.
Script to download Photos from Synology Photo
#!/usr/bin/bash
#Script to download photos from a public shared album in Synology Photos
#
#Setup:
#0) sudo apt install jq
#1) Share an album as "Anyone with link can view" in Synology Photos
#2) Copy AlbumId (end of the Link)
#3) Add host and album Id below
#4) run manually or via cronjob
#
# Example for cronjob:
# 1) run crontab -e
# 2) add this line :
# #download new photos every day at 0.00h
# 0 0 * * * /home/pi/syno-photos-downloader/syno-photos-downloader.sh
# 3) Save and close
#
# Note curl is set to silent mode, but still shows errors
#
# based on:
# https://daanmiddendorp.com/tech/2022/02/14/new-destination-for-my-broken-kindle.html
# https://github.com/landgenoot/kindle-synology-photos-photoframe/blob/main/src/refresh.sh
HOST="https://TODO:TODO"
ALBUMID="TODO"
EXPORTDIR="photos"
#variable to cache photo filenames in album
PHOTONAMES=""
#create output dir if not existing
mkdir -p ${EXPORTDIR}
link=${HOST}'/mo/sharing/'${ALBUMID}
cookie=$(curl -sS -k "${link}" -i | grep sharing_sid)
cookie="${cookie#*:}"
cookie="${cookie%;*}"
base_url="${link%/*}"
album_code="${link##*/}"
album=$(curl -sS -k "${base_url}/webapi/entry.cgi?" -X POST \
-H "x-syno-sharing: ${album_code}" \
-H "Cookie: ${cookie}" \
-d 'offset=0&limit=1000&api="SYNO.Foto.Browse.Item"&method="list"&version=1 -b cookies.txt')
#number of photos in album
length=$(($(echo "${album}" | jq -r '.data[] | length ')-1))
#get all images (if not existing)
#echo -ne "Downlading $((${length}+1)) photos: "
for ((i=0;i<=length;i++)); do
#show progress
#echo -ne "$((${i}+1)) "
id=$(echo "${album}" | jq -r ".data[] | .[${i}] | .id ")
#cache filename
PHOTONAMES="${PHOTONAMES}${id}\|"
#download photo if not yet existing in export dir
if [ ! -f "${EXPORTDIR}/${id}.jpg" ]; then
#curl -sS -k "${base_url}/webapi/entry.cgi/20210807_144336.jpg" -G
echo "Downloading new photo ${id}.jpg..."
curl -sS -k "${base_url}/webapi/entry.cgi/20210807_144336.jpg" -G \
-H "Cookie: ${cookie}" \
-d "id=${id}" -d "cache_key=\"35336_1628372812\"&type=\"unit\"&size=\"xl\"&passphrase=\"${album_code}\"&api=\"SYNO.Foto.Thumbnail\"&method=\"get\"&version=1&_sharing_id=\"${album_code}\"" \
-o "${EXPORTDIR}/${id}.jpg"
fi
done
#delete orphaned photos, i.e., no longer in album
len=${#PHOTONAMES}
if [ ${len} -gt 2 ]
then
#remove \| at the end of the string
PHOTONAMES=${PHOTONAMES::len-2}
cd ${EXPORTDIR}
#get inversed list of photos in folder but NOT in PHOTONAMES
PHOTONAMESORPHANED=$(ls | grep -v "${PHOTONAMES}")
if [ ${#PHOTONAMESORPHANED} -gt 0 ]
then
echo "Remove orphaned photos:"
echo "${PHOTONAMESORPHANED}"
#rm -r ${PHOTONAMESORPHANED}
rm -r `ls | grep -v "${PHOTONAMES}"`
fi
cd ..
fi
echo "Photo download done."
@Fredi009sg
Copy link

curl: (3) URL using bad/illegal format or missing URL

What can I do ?

@lmaertin
Copy link
Author

Add at line 42:

echo ${base_url}

And give feedback what is printed here

@lmaertin
Copy link
Author

May add further echos around each curl call to find out which of the many calls fails in the script…

@Fredi009sg
Copy link

pi@raspberrypi:~/syno-photos-downloader $ bash syno-photos-downloader.sh
syno-photos-downloader.sh: Zeile 2: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 24: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 27: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 29: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 32: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 35: $'\r': Kommando nicht gefunden.
curl: (3) URL using bad/illegal format or missing URL
syno-photos-downloader.sh: Zeile 42: $'\r': Kommando nicht gefunden.
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
syno-photos-downloader.sh: Zeile 47: -H: Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 48: -H: Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 49: -d: Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 47: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 50: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 53: Syntaxfehler beim unerwarteten Symbol »$'do\r'«
'yno-photos-downloader.sh: Zeile 53: `for ((i=0;i<=length;i++)); do

@Fredi009sg
Copy link

Those are my settings. I tried changing ALBUMID with "#/", "#" and "without nothing changed.

HOST="https://xxxxxx.myDS.me:xxxxx"
ALBUMID="xxxxxxxx#/"

EXPORTDIR="photos"

#variable to cache photo filenames in album
PHOTONAMES=""

@Fredi009sg
Copy link

Can you help ?

@lmaertin
Copy link
Author

lmaertin commented May 3, 2023

The album id has to be provided without # or /.

I‘m running this script from a raspberry so derivative, which kind of OS you are using?

Please try to access the url created in line 36 in your local browser. I think something is incompatible here!?

@Fredi009sg
Copy link

Fredi009sg commented May 3, 2023

Copying from Line 36 works. It automatically adds "#/" while its connecting to my SN.

I'm using Debian GNU/Linux 11 (bullseye)"

bash syno-photos-downloader.sh
syno-photos-downloader.sh: Zeile 2: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 24: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 27: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 29: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 32: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 35: $'\r': Kommando nicht gefunden.
curl: (3) URL using bad/illegal format or missing URL
syno-photos-downloader.sh: Zeile 42: $'\r': Kommando nicht gefunden.
/mo/sharingxxxx.xxxx.me:xxx
syno-photos-downloader.sh: Zeile 44: $'\r': Kommando nicht gefunden.
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
syno-photos-downloader.sh: Zeile 49: -H: Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 50: -H: Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 51: -d: Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 49: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 52: $'\r': Kommando nicht gefunden.
syno-photos-downloader.sh: Zeile 55: Syntaxfehler beim unerwarteten Symbol »$'do\r'«
'yno-photos-downloader.sh: Zeile 55: `for ((i=0;i<=length;i++)); do

@lmaertin
Copy link
Author

lmaertin commented May 3, 2023

Sorry, no further idea

@lmaertin
Copy link
Author

lmaertin commented May 3, 2023

Kommando nicht gefunden.
/mo/sharingxxxx.xxxx.me:xxx

This seems to be wrong

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