Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

PhotoprismPull library for python

In my efforts to de-google my life, I have established a Photoprism instance. It has a great third-party tool that helps build the same albums on Photoprism that I had in Google Photos.

I used to use images from Google Photos album as screensaver on Linux but now I am using PhotoPrism so I had to develop a new solution.

My goals include showing only the most recent photos (60 days in my case), and thankfully the Photoprism API endpoint for searching for images allows you to filter with "after {date}"! In fact, the logic for pulling the time range from Photoprism is easier than from Google Photos!

I wrote a whole python library and simple front end to accomplish my shell script which pulls the named albums' recent images.

README for photoprismpull

This is a basic python lib for interacting with the API of a PhotoPrism instance.

Upstream

This project's upstream is at https://bgstack15.ddns.net/cgit/photoprismpull and https://gitlab.com/bgstack15/photoprismpull.

Alternatives

A library written in Go is advertised by Photoprism.

Related

For a shell interaction with the API to trigger imports, see https://bgstack15.ddns.net/cgit/photoprism-autoimport/.

For a script to create albums from a Google Photos Takeout in PhotoPrism see https://github.com/inthreedee/photoprism-transfer-album.

Reason for existing

I do not know Go, but I am comfortable in python. My two original goals for this library were:

  • Download album to directory (with optional limitation such as "only the last 60 days"
  • Add photos to album based on sha1sums

Using

There is a small front-end, pp.py. Run its --help option.

I tend to use this library in an interactive python shell. One task included fetching a Google Photos album as a zip, extracting, and generating a sha1sums file. I had used that related photoprism-transfer-album tool, but my Takeout failed to include a few albums for no reason. Since I already had all the photos in PhotoPrism, I just needed to find them and add them to the correct album.

sha1sum -- * > input.album5

And then in a python shell:

import importlib, pplib
# as needed after changes:
importlib.reload(pplib)
a = pplib.get_session("https://pp.example.com","admin","password")
c = add_hashes_to_album(a, "Pretty Album Name", "/mnt/public/Images/photoprism/Named albums/Pretty Album Name/input.album5",apply=True)

The simpler example is downloading an album to a subdirectory.

import pplib
a = pplib.get_session("https://pp.example.com","admin","password")
c = download_album_to_directory("Pretty Album Name",directory="/mnt/public/Images/photoprism",extraparams="&after=2020-02-08",session=a)

This will make a new directory, /mnt/public/Images/photoprism/Pretty Album Name/ and populate it with the images from this named album in PhotoPrism. Note that the get_album_by_title() function explicitly excludes "albums" autogenerated from existing directories in your originals directory. It only searches through actual "album" albums. This is trivial to change for yourself as needed.

file get-albums.sh

#!/bin/sh
# File: get-albums.sh
# Location: extra/
# Author: bgstack15
# Startdate: 2022-07-07 13:50
# Title: Demo for getting albums
# Purpose: Download albums easily, but only keep the past so many days
# History:
# Usage:
#    adjust variables at top, and album names looped at the bottom.
# Reference:
# Improve:
#    switch to bash, and put the list of album names in the top part with the other variables?
# Dependencies:
# Documentation: see README.md for project
OUTDIR=/mnt/public/pictures
SCRIPT=./pp.py
USERNAME=admin
PWFILE=pwfile
URL=http://server4:2342
get_album(){
   # Goal: get photos from this named album, and keep only ones from under $DAYS days ago.
   # call: get_album "${TOPDIR}" "${NAME}" "${DAYS}"
   _dir="${1}"
   _name="${2}"
   _days="${3}"
   when="$( date -d "-${_days} days" "+%F" )"
   test -d "${_dir}/${_name}" && find "${_dir}/${_name}" -mindepth 1 ! -type d -mtime "+${_days}" -delete
   "${SCRIPT}" --url "${URL}" --password "${PWFILE}" --username "${USERNAME}" -a "${_name}" --extra "&after=${when}" --directory "${_dir}"
}
for album in "Pretty Name 1" "Family Memories 2020-2025" ;
do
   get_album "${OUTDIR}" "${album}" "60"
done

The file is that simple! There are so many more parameters you can pass to SearchPhotos. I only needed "after."

Comments