aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB. Stack <bgstack15@gmail.com>2022-05-10 15:12:12 -0400
committerB. Stack <bgstack15@gmail.com>2022-05-10 15:12:12 -0400
commit9d97bf284f27794cca2949ba97edf5f5906929a0 (patch)
tree2ceaded6f6c1eedfe6f03af13f22ad115af1fad1
downloadphotoprism-autoimport-9d97bf284f27794cca2949ba97edf5f5906929a0.tar.gz
photoprism-autoimport-9d97bf284f27794cca2949ba97edf5f5906929a0.tar.bz2
photoprism-autoimport-9d97bf284f27794cca2949ba97edf5f5906929a0.zip
initial commit
-rw-r--r--.gitignore2
-rw-r--r--README.md35
-rwxr-xr-xautoimport.sh85
-rw-r--r--photoprism-autoimport.conf.example8
4 files changed, 130 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..53468e1
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.conf
+*.log
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e5a6cc3
--- /dev/null
+++ b/README.md
@@ -0,0 +1,35 @@
+# Readme for photoprism-autoimport
+
+## Overview
+Photoprism-autoimport is a small project that uses the API of [PhotoPrism] to initiate a new import of a named directory from the command line.
+
+## Reason for existence
+This is useful when the photos to be imported are added in a manner that does not already trigger photoprism's import task. When you use webdav to upload files to an import directory for photoprism, the app already triggers an import task so many seconds afterwards. However, I use a different method ([SyncThing](https://syncthing.net)) so this autoimport project makes my life easier.
+
+## Alternatives
+
+* Use PhotoSync android app. I don't use this because it's not listed in F-droid.
+* Use some other WebDAV upload ability.
+* Manually trigger uploads in PhotoPrism interface with heading Library -> Import tab.
+
+## PhotoPrism-autoimport upstream
+[This repository](https://gitlab.com/bgstack15/photoprism-autoimport/) is the upstream source of this project.
+
+## Dependencies
+
+* curl
+* jq
+* POSIX shell (bash, ksh, dash, etc.)
+* `photoprism-autoimport.conf` configured like the example. This file contains a dot-sourceable shell script that defines variables:
+ * username
+ * password
+ * url
+ * directory (short path, not "/import/Pathname"). Case-sensitive.
+
+## Using
+Call the shell script, after configuring the config file. Because the API request is synchronous, a long import task might cause curl to fail out. The import task is still operating however. If you anticipate long import jobs, set the `max_time` value in the config file.
+
+## References
+[1]: <https://docs.photoprism.app/developer-guide/ui/rest-api/> REST API - PhotoPrism
+[2]: <https://pkg.go.dev/github.com/photoprism/photoprism/internal/api?utm_source=godoc#StartImport> api package - github.com/photoprism/photoprism/internal/api - pkg.go.dev
+[3]: <https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced/28776166#28776166> bash - How to detect if a script is being sourced - Stack Overflow
diff --git a/autoimport.sh b/autoimport.sh
new file mode 100755
index 0000000..8b09d0c
--- /dev/null
+++ b/autoimport.sh
@@ -0,0 +1,85 @@
+#!/bin/sh
+# File: autoimport.sh
+# Location: https://gitlab.com/bgstack15/photoprism-autoimport/
+# Author: bgstack15
+# SPDX-License-Identifier: GPL-3.0
+# Startdate: 2022-05-10 13:14
+# Title: Start Import Job for PhotoPrism from CLI
+# Purpose: with a single shell script, kick off an import process for a named import directory in PhotoPrism
+# History:
+# Usage:
+# You can dot-source this script to get the functions.
+# You can also just call the script by itself, with an optional env var CONFFILE.
+# Conffile must contain dot-source script that contains variables: username password url directory
+# Dependencies:
+# CONFFILE
+# req-devuan: jq
+# References:
+# LW devtools
+# https://docs.photoprism.app/developer-guide/ui/rest-api/
+# https://pkg.go.dev/github.com/photoprism/photoprism/internal/api?utm_source=godoc#StartImport
+# https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced/28776166#28776166
+
+workdir="$( dirname "$( readlink -f "${0}" 2>/dev/null )" 2>/dev/null || echo "${PWD}" )"
+#echo "workdir=${workdir}"
+test -z "${CONFFILE}" && CONFFILE="${workdir}/photoprism-autoimport.conf"
+test -e "${CONFFILE}" && . "${CONFFILE}"
+test -z "${LOGFILE}" && LOGFILE="${workdir}/autoimport.log"
+
+new_session() {
+ # dependencies: CONFFILE is dot-sourced already
+ # call: new_session
+ # get session
+ result="$( curl --silent -L "${url%%/}/api/v1/session" -X POST -H 'Content-Type: application/json' --data "{\"username\":\"${username}\",\"password\":\"${password}\"}" )"
+ export SESSION="$( echo "${result}" | jq --raw-output '.id' )"
+ #echo "inside function, session=${session}"
+}
+
+import_directory() {
+ # dependencies: SESSION
+ # call: import_directory "${DIRECTORY}"
+ ___id_dir="${1}"
+ test -z "${SESSION}" && new_session # sets env var SESSION
+ echo "curl --silent ${max_time:+--max-time ${max_time} }-L \"${url%%/}/api/v1/import\" -X POST --data \"{\\\"path\\\":\\\"${___id_dir}\\\"}\" -H \"X-Session-ID: ${SESSION}\""
+ curl --silent ${max_time:+--max-time ${max_time} }-L "${url%%/}/api/v1/import" -X POST --data "{\"path\":\"${___id_dir}\"}" -H "X-Session-ID: ${SESSION}"
+}
+
+list_directories() {
+ # dependencies: SESSION
+ # goal: list available directories for running an import task
+ test -z "${SESSION}" && new_session
+ #curl --silent "${url%%/}/api/v1/folders/import" -H "X-Session-ID: ${SESSION}" | jq '.folders[].Root+"/"+.folders[].Path'
+ curl --silent "${url%%/}/api/v1/folders/import" -H "X-Session-ID: ${SESSION}" | jq '.folders[].Path'
+}
+
+main() {
+ #echo "outside function, session=${session}"
+ import_directory "${directory}"
+}
+
+# Determine if this script was dot-sourced
+
+sourced=0
+if [ -n "$ZSH_EVAL_CONTEXT" ]; then
+ case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac
+elif [ -n "$KSH_VERSION" ]; then
+ [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] && sourced=1
+elif [ -n "$BASH_VERSION" ]; then
+ (return 0 2>/dev/null) && sourced=1
+else # All other shells: examine $0 for known shell binary filenames
+ # Detects `sh` and `dash`; add additional shell filenames as needed.
+ case ${0##*/} in sh|dash) sourced=1;; esac
+fi
+
+# So, if not dot-sourced, and this is run by cron, add logging
+if test $sourced -eq 0;
+then
+ if echo " ${@} " | grep -q cron ;
+ then
+ main 2>&1 | plecho | tee -a "${LOGFILE}"
+ printf '\n' | tee -a "${LOGFILE}"
+ else
+ main
+ fi
+fi
+:
diff --git a/photoprism-autoimport.conf.example b/photoprism-autoimport.conf.example
new file mode 100644
index 0000000..0f12717
--- /dev/null
+++ b/photoprism-autoimport.conf.example
@@ -0,0 +1,8 @@
+# Project: photoprism-autoimport
+# Dot-sourced by autoimport.sh
+username=admin
+password=1234_admin_pw_7890
+url=https://www.example.com:2342/
+directory=Stackphone1_Camera
+# Set this to safely shut down curl if the import task does not return a result to curl. The import job will still continue to run even if curl disconnects. Number is seconds.
+#max_time=8
bgstack15