Recipes: scripts for common jobs

Ready-to-copy scripts for the jobs people ask for most, with the API (bash with curl and jq, or PowerShell) and with the trackhop command. On your own server, Tools › API › Recipes shows the same scripts with your server’s address already in them.

Every call and command here is checked against the real API and command by Trackhop’s tests, so a recipe cannot name something that does not exist.

Before you start

Make a token in Tools › API (it is shown once). Paste it, and your server’s address, at the top of the script:

Bash

export TRACKHOP_TOKEN="th_…"      # the token made in Tools › API
TH="https://your-server/api/v1"
H="Authorization: Bearer $TRACKHOP_TOKEN"

PowerShell

$env:TRACKHOP_TOKEN = "th_…"      # the token made in Tools › API
$TH = "https://your-server/api/v1"
$H = @{ Authorization = "Bearer $env:TRACKHOP_TOKEN" }

The token is as good as your password for Trackhop: keep it out of files you share. The trackhop command needs no token on the same computer or inside the container (docker exec trackhop trackhop …).

Run every sync

Starts every sync that is turned on, as the Sync all button does.

Bash

curl -s -X POST -H "$H" "$TH/syncs/run-all"

PowerShell

Invoke-RestMethod -Method Post -Headers $H "$TH/syncs/run-all"

The trackhop command

trackhop run --all --wait

Run a group of syncs

The syncs wearing a label, with a tag or starred (only those turned on). The lines after “or:” are the other ways to choose the group.

Bash

curl -s -X POST -H "$H" "$TH/syncs/run?label=Gym"
# or: curl -s -X POST -H "$H" "$TH/syncs/run?tag=genre:metal"
# or: curl -s -X POST -H "$H" "$TH/syncs/run?favorite=true"

PowerShell

Invoke-RestMethod -Method Post -Headers $H "$TH/syncs/run?label=Gym"
# or: Invoke-RestMethod -Method Post -Headers $H "$TH/syncs/run?tag=genre:metal"
# or: Invoke-RestMethod -Method Post -Headers $H "$TH/syncs/run?favorite=true"

The trackhop command

trackhop run --label Gym --wait
# or: trackhop run --tag genre=metal --wait
# or: trackhop run --favorite --wait

Copy the star ratings on many syncs

Turns “Copy star ratings” on for every sync that goes to one service (here Plex: its id is in GET /services). ratings is off, own (the ratings set by hand) or all (those and the ones iTunes derived from the album). The command takes the syncs by name.

Bash

ids=$(curl -s -H "$H" "$TH/syncs" | jq -c '[.[] | select(.target.service == "plex") | .id]')
curl -s -X POST -H "$H" -H "Content-Type: application/json" -d '{"syncs": '"$ids"', "ratings": "own"}' "$TH/syncs/bulk"

PowerShell

$ids = (Invoke-RestMethod -Headers $H "$TH/syncs" | Where-Object { $_.target.service -eq "plex" }).id
$body = @{ syncs = @($ids); ratings = "own" } | ConvertTo-Json
Invoke-RestMethod -Method Post -Headers $H -ContentType "application/json" -Body $body "$TH/syncs/bulk"

The trackhop command

trackhop edit-many "Road trip" "Gym" --ratings own

Turn off the syncs of a service

Turns the schedule off for every sync that goes to one service (a holiday, a service that is down); “enabled”: true turns them on again.

Bash

ids=$(curl -s -H "$H" "$TH/syncs" | jq -c '[.[] | select(.target.service == "tidal") | .id]')
curl -s -X POST -H "$H" -H "Content-Type: application/json" -d '{"syncs": '"$ids"', "enabled": false}' "$TH/syncs/bulk"

PowerShell

$ids = (Invoke-RestMethod -Headers $H "$TH/syncs" | Where-Object { $_.target.service -eq "tidal" }).id
$body = @{ syncs = @($ids); enabled = $false } | ConvertTo-Json
Invoke-RestMethod -Method Post -Headers $H -ContentType "application/json" -Body $body "$TH/syncs/bulk"

The trackhop command

trackhop edit-many "Road trip" "Gym" --off
# or: trackhop edit-many "Road trip" "Gym" --on

Search again the songs not found

A song not found is not searched again for a month. After your server rescanned its library, this forgets them for every sync that goes to it, so the next run searches them again.

Bash

for id in $(curl -s -H "$H" "$TH/syncs" | jq -r '.[] | select(.target.service == "navidrome") | .id'); do
  curl -s -X POST -H "$H" "$TH/syncs/$id/run?retryNotFound=true"
done

PowerShell

$syncs = Invoke-RestMethod -Headers $H "$TH/syncs" | Where-Object { $_.target.service -eq "navidrome" }
foreach ($s in $syncs) { Invoke-RestMethod -Method Post -Headers $H "$TH/syncs/$($s.id)/run?retryNotFound=true" }

The trackhop command

trackhop run "Road trip" "Gym" --retry-not-found --wait

Export the saved matches

The saved matches (or only the songs not found) as a CSV file, one row per song and target service. kind is all, chosen, automatic, not_found or ignored; to narrows it to one target service.

Bash

curl -s -H "$H" -o not-found.csv "$TH/matches/export?kind=not_found&to=tidal"

PowerShell

Invoke-RestMethod -Headers $H -OutFile not-found.csv "$TH/matches/export?kind=not_found&to=tidal"

The trackhop command

trackhop export-matches not-found.csv --kind not_found --to tidal

The Wanted list for Lidarr (and others)

What your syncs could not find on your own server (Missing on my server). format=lidarr is Lidarr’s Custom List: the artists with a MusicBrainz id. Lidarr itself fetches it with the key shown on Tools › Missing on my server, with no token. The same list as CSV, only the last week’s.

Bash

curl -s -H "$H" -o lidarr-list.json "$TH/wanted?format=lidarr"
curl -s -H "$H" -o wanted-week.csv "$TH/wanted?format=csv&since=7d"

PowerShell

Invoke-RestMethod -Headers $H -OutFile lidarr-list.json "$TH/wanted?format=lidarr"
Invoke-RestMethod -Headers $H -OutFile wanted-week.csv "$TH/wanted?format=csv&since=7d"

The trackhop command

trackhop wanted --format lidarr > lidarr-list.json
trackhop wanted --format csv --since 7d > wanted-week.csv

Songs found nowhere, as a playlist

The songs not found by any of your syncs, those missing on one service, saved as a File playlist to sync on to any service.

Bash

curl -s -X POST -H "$H" -H "Content-Type: application/json" -d '{"name": "Not found anywhere", "scope": "all", "on": "spotify"}' "$TH/wanted/file-playlist"

PowerShell

$body = @{ name = "Not found anywhere"; scope = "all"; on = "spotify" } | ConvertTo-Json
Invoke-RestMethod -Method Post -Headers $H -ContentType "application/json" -Body $body "$TH/wanted/file-playlist"

The trackhop command

trackhop wanted --everywhere --missing-on spotify --save-as "Not found anywhere"

A nightly backup

Backs up every playlist as dated files (a background task) and saves the set-up (syncs, settings, chosen matches) as one .trackhop file. Put it in cron or Task Scheduler; with Docker the command runs as shown in the comment.

Bash

curl -s -X POST -H "$H" "$TH/backup"
curl -s -H "$H" -o "trackhop-$(date +%F).trackhop" "$TH/config"

PowerShell

Invoke-RestMethod -Method Post -Headers $H "$TH/backup"
Invoke-RestMethod -Headers $H -OutFile "trackhop-$(Get-Date -Format yyyy-MM-dd).trackhop" "$TH/config"

The trackhop command

# cron, every night at 03:00, with Docker: 0 3 * * * docker exec trackhop trackhop backup --wait
trackhop backup --wait
trackhop config set-up.trackhop

Copies before big syncs

Before a sync that adds or removes many songs, Trackhop saves the destination’s playlists. This lists the copies, shows what restoring the newest would do (nothing changes), and, after “or:”, restores it (only additions) or keeps 5 copies of each service.

Bash

curl -s -H "$H" "$TH/copies" | jq '.copies[] | {id, serviceName, takenAt, reason}'
id=$(curl -s -H "$H" "$TH/copies" | jq -r '.copies[0].id')
curl -s -H "$H" "$TH/copies/$id"
# or: curl -s -X POST -H "$H" "$TH/copies/$id/restore"
# or: curl -s -X PATCH -H "$H" -H "Content-Type: application/json" -d '{"keep": 5}' "$TH/copies"

PowerShell

(Invoke-RestMethod -Headers $H "$TH/copies").copies | Select-Object id, serviceName, takenAt, reason
$id = (Invoke-RestMethod -Headers $H "$TH/copies").copies[0].id
Invoke-RestMethod -Headers $H "$TH/copies/$id"
# or: Invoke-RestMethod -Method Post -Headers $H "$TH/copies/$id/restore"
# or: Invoke-RestMethod -Method Patch -Headers $H -ContentType "application/json" -Body '{"keep": 5}' "$TH/copies"

The trackhop command

trackhop copies
trackhop copies show <copy>
# or: trackhop copies restore <copy> --wait
# or: trackhop copies keep 5

Start from scratch

Saves the set-up, then deletes everything in Trackhop’s data folder (syncs, settings, sign-ins, files, log) and stops Trackhop. The call is accepted only from the same computer (127.0.0.1, not through a proxy), on a Trackhop for one person.

Careful. This cannot be undone. Keep the saved file: it brings your syncs back (Tools › Backup & restore), but not your sign-ins.

Bash

curl -s -H "$H" -o before-reset.trackhop "$TH/config"
curl -s -X POST -H "$H" -H "Content-Type: application/json" -d '{"confirm": "RESET"}' "$TH/reset"

PowerShell

Invoke-RestMethod -Headers $H -OutFile before-reset.trackhop "$TH/config"
Invoke-RestMethod -Method Post -Headers $H -ContentType "application/json" -Body '{"confirm": "RESET"}' "$TH/reset"

The trackhop command

trackhop reset --backup before-reset.trackhop --yes