lots o fixes, beets, shellcheck stuff
[distro-setup] / nav2beet
index cc9ca15c2a11efc4dd3da37e83daeb63cbea64a5..22322a60b4790275571605d92671beb43d62bd2f 100755 (executable)
--- a/nav2beet
+++ b/nav2beet
@@ -1,20 +1,14 @@
 #!/bin/bash
 
-f=/usr/local/lib/err;test -r $f || { echo "error: $0 no $f" >&2;exit 1;}; . $f
-
-plists=(
-  # these are useful tags
-  expl
-  gimicky
-  sad
-  # these are normal playlists
-  love
-  pump1
-  pumprap
-  rend
-  run
-)
+set -e; . /usr/local/lib/err; set +e
 
+source /a/bin/ds/beet-data
+source /b/bash_unpublished/source-semi-priv
+
+declare -A genre_a
+for g in ${all_genres[@]}; do
+  genre_a[$g]=t
+done
 
 # these options are mainly for debugging / developing quickly.
 plist_only=false
@@ -39,7 +33,7 @@ done
 m ()
 {
   printf "%s\n" "$*" >&2
-  if $dry_run && [[ $1 == beet && $2 == modify ]]; then
+  if $dry_run; then
     echo "dry run: $*"
   else
     "$@"
@@ -53,29 +47,22 @@ declare -A flacs
 declare -A navirating
 tmpdir=$(mktemp -d)
 cd $tmpdir
+# the code here is duplicated later for non ssh context.
 if [[ $HOSTNAME != kd ]]; then
-  ssh b8.nz bash -s <<EOF  | tar xz
+
+  ssh b8.nz bash -s "$(md5sum </a/bin/ds/beet-data)" <<'EOF'  | tar xz
+if [[ $1 != "$(md5sum </a/bin/ds/beet-data)" ]]; then
+  echo error: old beet-data on kd
+  exit 1
+fi
 install -m 700 -d /tmp/nav2beet
 cd /tmp/nav2beet
-for r in 1 2 3 4 5; do
- sqlite3 /i/navidrome/navidrome.db ".output \$r" "select path from annotation inner join media_file on item_id = id where rating = \$r;"
-done
-find /i/m -type f -name '*.flac' >flacs
-for plist in ${plists[@]}; do
-  sqlite3 /i/navidrome/navidrome.db "select path from media_file inner join playlist_tracks on media_file.id = media_file_id where playlist_id = (select id from playlist where name = '\$plist');" | sed 's,^/i/converted,/i/m,' | sort >\$plist
-done
+/a/bin/ds/nav2beet-local
 tar cz -C /tmp nav2beet
 EOF
-
   cd nav2beet
 else
-  for r in 1 2 3 4 5; do
-    sqlite3 /i/navidrome/navidrome.db ".output $r" "select path from annotation inner join media_file on item_id = id where rating = $r;"
-  done
-  find /i/m -type f -name '*.flac' >flacs
-  for plist in ${plists[@]}; do
-    sqlite3 /i/navidrome/navidrome.db "select path from media_file inner join playlist_tracks on media_file.id = media_file_id where playlist_id = (select id from playlist where name = '$plist');" | sed 's,^/i/converted,/i/m,' | sort >$plist
-  done
+  /a/bin/ds/nav2beet-local
 fi
 while read -r l; do
   flacs[$l]=t
@@ -83,10 +70,6 @@ done <flacs
 
 if ! $plist_only; then
   echo begin star rating import from navidrome to beets
-  # todo: consider if this is a problem: file removed/renamed in main
-  # collection, but not yet updated navidrome, we want to skip it not
-  # die.
-
   for r in 1 2 3 4 5; do
     while read -r path; do
       beetpath="/i/m${path#/i/converted}"
@@ -99,6 +82,7 @@ if ! $plist_only; then
   done
   declare -A beetrating
   for r in 1 2 3 4 5; do
+    # shellcheck disable=SC2016 # expected beets arg
     m beet ls -f '$path' rating:$r >$tmpf
     while read -r path; do
       beetrating[$path]=$r
@@ -107,8 +91,16 @@ if ! $plist_only; then
 
   for path in "${!navirating[@]}"; do
     r="${navirating[$path]}"
+    if [[ ! "${beetrating[$path]}" ]]; then
+      if [[ -e $path ]]; then
+        echo "$0: ERROR: $path exists but we have no rating for it"
+        exit 1
+      else
+        echo "$0: WARNING: $path exists in navidrome but not beets"
+        continue
+      fi
+    fi
     if [[ $r != "${beetrating[$path]}" ]]; then
-      # note: this assumes there are no cases like filea.mp3 filea.mp3.mp3, which would affect both files.
       echo "$r != ${beetrating[$path]}, beet modify -y path:$path rating=$r"
       beet modify -y "path:$path" "rating=$r"
     fi
@@ -116,17 +108,50 @@ if ! $plist_only; then
   # end star rating import from navidrome to beets:
 fi
 
-
 echo begin import navidrome playlists as flexible attribute with value t.
-# These are only the playlists listed in the beets config.yaml
-# "subsonicplaylist:" and then duplicated here:
-
 
+shopt -s nullglob
+for path in 2genre/*/*; do
+  id="${path#*/}"
+  id="${id%/*}"
+  filename="${path##*/}"
+  tmp="${filename%%[^0-9-]*}"
+  genre="${filename#"$tmp"}"
+  if [[ ${genre_a[$genre]} ]]; then
+    is_genre=true
+  else
+    # Some playlists we create with random names to remind us to do something
+    # with these tracks later once we are at a computer.
+    is_genre=false
+  fi
+  while read -r path; do
+    flac="${path%.mp3}.flac"
+    if [[ ${flacs[$flac]} ]]; then
+      path="$flac"
+    fi
+    if $is_genre; then
+      m beet modify -y "path:$path" "genre=$genre"
+    else
+      m beet modify -y "path:$path" "$genre=t"
+    fi
+  done <"$path"
+  # how i figured this out:
+  # s tcpdump -i any -w /tmp/tcpdump port 4533
+  # then delete a test playlist in client.
+  # made some sense out of it with: http://www.subsonic.org/pages/api.jsp
+  # open file in wireshard, right click "Hypertext Transfer Protocol", copy, as printable text, put into file /tmp/headers
+  # /a/opt/h2c/h2c </tmp/headers
+  # change from https to http.
+  # then tested out removing stuff i suspected was not important,
+  # and added https and host so it would work remotely.
+  m curl --http1.1 --user "iank:$navidrome_pw" "https://b8.nz/rest/deletePlaylist.view?u=iank&s=sb219dvv7egnoe4i47k75cli0m&t=1c8f5575cd0fdf03deb971187c9c88b1&v=1.2.0&c=DSub&id=$id"
+done
 
 
-for plist in ${plists[@]}; do
+for plist in ${nav_tags[@]}; do
   echo "processing $plist"
-  beet ls -f '$path' $plist:t ^genre:spoken-w ^genre:skit ^rating:1 | sort | sed 's,\.flac$,.mp3,'> p
+  # shellcheck disable=SC2016 # expected beets arg
+  beet ls -f '$path' $plist:t $nav_convert_query | sort | sed 's,\.flac$,.mp3,'> p
   while read -r path; do
     flac="${path%.mp3}.flac"
     if [[ ${flacs[$flac]} ]]; then
@@ -137,7 +162,6 @@ for plist in ${plists[@]}; do
   done < <(comm -23 p $plist)
   while read -r path; do
     flac="${path%.mp3}.flac"
-    echo "flac=$flac path=$path ${flacs[$flac]} ${flacs[flac]}"
     if [[ ${flacs[$flac]} ]]; then
       path="$flac"
     fi