Skip to content

Instantly share code, notes, and snippets.

@luckman212
Created November 7, 2022 18:09
Show Gist options
  • Save luckman212/e2470df9c555995e5779d3acff4002d9 to your computer and use it in GitHub Desktop.
Save luckman212/e2470df9c555995e5779d3acff4002d9 to your computer and use it in GitHub Desktop.
stream change events via syncthing REST API
#!/usr/bin/env bash
# https://docs.syncthing.net/rest/events-get.html
# https://docs.syncthing.net/dev/events.html#event-types
APIKEY=$(defaults read com.github.xor-gate.syncthing-macosx ApiKey)
[[ -n "$APIKEY" ]] || { echo "error retreiving API key"; exit 1; }
ENDPOINT='http://localhost:8384/rest/events/disk'
INTERVAL=2
_wait() {
sleep $INTERVAL
}
while true ; do
JSON=$(curl -s -H "X-API-Key: $APIKEY" "$ENDPOINT?timeout=$INTERVAL&since=$LAST" 2>/dev/null)
[[ -n "$JSON" ]] || { _wait; continue; }
LAST=$(jq -r '.[-1].id' <<<"$JSON" 2>/dev/null)
if [[ $LAST != "$LATEST" ]] && [[ $LAST != "null" ]]; then
LATEST=$LAST
jq -r '.[] | . as $r | .data | [
($r.time | sub("\\.[0-9]{3,}.*$"; "") | sub("T"; " ")),
$r.id,
([ .modifiedBy, "("+($r.type | sub("ChangeDetected"; ""))+")", .action,.type ] | join(" ")),
.path
] | @tsv' <<<"$JSON"
fi
_wait
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment