diff --git a/roles/photos/tasks/bambulab-fetch.yaml b/roles/photos/tasks/bambulab-fetch.yaml new file mode 100644 index 0000000..436c39e --- /dev/null +++ b/roles/photos/tasks/bambulab-fetch.yaml @@ -0,0 +1,37 @@ +--- +- name: Install dependencies + apt: + name: curl + +- name: Install fetch script + template: + src: bambulab-fetch.sh + dest: /opt/bambulab-fetch.sh + owner: root + group: root + mode: 0755 + +- name: Install service + template: + src: bambulab-fetch.service + dest: /etc/systemd/system/bambulab-fetch.service + owner: root + group: root + mode: 0644 + notify: daemon reload + +- name: Install timer + template: + src: bambulab-fetch.timer + dest: /etc/systemd/system/bambulab-fetch.timer + owner: root + group: root + mode: 0644 + notify: daemon reload + +- name: Enable timer + systemd: + name: bambulab-fetch + state: started + enabled: yes + daemon_reload: true diff --git a/roles/photos/tasks/main.yaml b/roles/photos/tasks/main.yaml index 0450d03..12af023 100644 --- a/roles/photos/tasks/main.yaml +++ b/roles/photos/tasks/main.yaml @@ -4,3 +4,6 @@ - tags: photos_mqtt import_tasks: photos2mqtt.yaml + +- tags: bambulab-fetch + import_tasks: bambulab-fetch.yaml diff --git a/roles/photos/templates/bambulab-fetch.service b/roles/photos/templates/bambulab-fetch.service new file mode 100644 index 0000000..14b372e --- /dev/null +++ b/roles/photos/templates/bambulab-fetch.service @@ -0,0 +1,10 @@ +# {{ ansible_managed }} + +[Unit] +Description=BambuLab Timelapse Fetcher + +[Service] +ExecStart=/opt/bambulab-fetch.sh +Type=oneshot +User=wip +Group=wip diff --git a/roles/photos/templates/bambulab-fetch.sh b/roles/photos/templates/bambulab-fetch.sh new file mode 100644 index 0000000..080a3fa --- /dev/null +++ b/roles/photos/templates/bambulab-fetch.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# {{ ansible_managed }} + +set -eu + +host={{ bambulab_host }} +pass={{ bambulab_key }} +dir={{ photos_path }} + +files=$(curl -ksl "ftps://bblp:$pass@$host:990/timelapse/" | grep ^video_) + +for file in $files; do + # $file is formatted like 'video_2024-06-27_22-50-12.mp4' + echo $file + + day=$(echo $file | sed -nr 's/^video_([0-9]+)-([0-9]+)-([0-9]+)_.*/\1\2\3/p') + time=$(echo $file | sed -nr 's/^video_[^_]+_([^\.]+)\.mp4/\1/p') + ofile="$dir/$day/bambu_timelapse_$time.mp4" + echo $ofile + + if [ ! -e "$ofile" ]; then + mkdir -p "$(dirname $ofile)" + curl -ks "ftps://bblp:$pass@$host:990/timelapse/$file" -o "$ofile" + fi +done diff --git a/roles/photos/templates/bambulab-fetch.timer b/roles/photos/templates/bambulab-fetch.timer new file mode 100644 index 0000000..024b44a --- /dev/null +++ b/roles/photos/templates/bambulab-fetch.timer @@ -0,0 +1,8 @@ +[Unit] +Description=Run every 15 minutes + +[Timer] +OnCalendar=*:5/10 + +[Install] +WantedBy=timers.target