fcd1fb23e8716b3f22482e1ba411594815fb5975
[distro-setup] / nav2beet
1 #!/bin/bash
2
3 set -e; . /usr/local/lib/bash-bear; set +e
4
5 source /a/bin/ds/beet-data
6 source /b/bash_unpublished/source-semi-priv
7
8 declare -A genre_a
9 for g in ${all_genres[@]}; do
10 genre_a[$g]=t
11 done
12
13 # these options are mainly for debugging / developing quickly.
14 plist_only=false
15 dry_run=false
16 while (( $# )); do
17 case $1 in
18 plist)
19 plist_only=true
20 shift
21 ;;
22 dry)
23 dry_run=true
24 shift
25 ;;
26 *)
27 echo "error. unexpected arg read script"
28 exit 1
29 ;;
30 esac
31 done
32
33 m ()
34 {
35 printf "%s\n" "$*" >&2
36 if $dry_run; then
37 echo "dry run: $*"
38 else
39 "$@"
40 fi
41 }
42
43 tmpf=$(mktemp)
44
45 declare -A flacs
46
47 declare -A navirating
48 tmpdir=$(mktemp -d)
49 cd $tmpdir
50 # the code here is duplicated later for non ssh context.
51 if [[ $HOSTNAME != kd ]]; then
52
53 ssh b8.nz bash -s "$(md5sum </a/bin/ds/beet-data)" <<'EOF' | tar xz
54 if [[ $1 != "$(md5sum </a/bin/ds/beet-data)" ]]; then
55 echo error: old beet-data on kd
56 exit 1
57 fi
58 install -m 700 -d /tmp/nav2beet
59 cd /tmp/nav2beet
60 /a/bin/ds/nav2beet-local
61 tar cz -C /tmp nav2beet
62 EOF
63 cd nav2beet
64 else
65 /a/bin/ds/nav2beet-local
66 fi
67 while read -r l; do
68 flacs[$l]=t
69 done <flacs
70
71 if ! $plist_only; then
72 echo begin star rating import from navidrome to beets
73 for r in 1 2 3 4 5; do
74 while read -r path; do
75 beetpath="/i/m${path#/i/converted}"
76 flac="${beetpath%.mp3}.flac"
77 if [[ ${flacs[$flac]} ]]; then
78 beetpath="$flac"
79 fi
80 navirating[$beetpath]=$r
81 done <$r
82 done
83 declare -A beetrating
84 for r in 1 2 3 4 5; do
85 # shellcheck disable=SC2016 # expected beets arg
86 m beet ls -f '$path' rating:$r >$tmpf
87 while read -r path; do
88 beetrating[$path]=$r
89 done <$tmpf
90 done
91
92 for path in "${!navirating[@]}"; do
93 r="${navirating[$path]}"
94 if [[ ! "${beetrating[$path]}" ]]; then
95 if [[ -e $path ]]; then
96 echo "$0: ERROR: $path exists but we have no rating for it"
97 exit 1
98 else
99 echo "$0: WARNING: $path exists in navidrome but not beets"
100 continue
101 fi
102 fi
103 if [[ $r != "${beetrating[$path]}" ]]; then
104 echo "$r != ${beetrating[$path]}, beet modify -y path:$path rating=$r"
105 beet modify -y "path:$path" "rating=$r"
106 fi
107 done
108 # end star rating import from navidrome to beets:
109 fi
110
111 echo begin import navidrome playlists as flexible attribute with value t.
112
113 shopt -s nullglob
114 for path in 2genre/*/*; do
115 id="${path#*/}"
116 id="${id%/*}"
117 filename="${path##*/}"
118 tmp="${filename%%[^0-9-]*}"
119 genre="${filename#"$tmp"}"
120 if [[ ${genre_a[$genre]} ]]; then
121 is_genre=true
122 else
123 # Some playlists we create with random names to remind us to do something
124 # with these tracks later once we are at a computer.
125 is_genre=false
126 fi
127 while read -r path; do
128 flac="${path%.mp3}.flac"
129 if [[ ${flacs[$flac]} ]]; then
130 path="$flac"
131 fi
132 if $is_genre; then
133 m beet modify -y "path:$path" "genre=$genre"
134 else
135 m beet modify -y "path:$path" "$genre=t"
136 fi
137 done <"$path"
138 # how i figured this out:
139 # s tcpdump -i any -w /tmp/tcpdump port 4533
140 # then delete a test playlist in client.
141 # made some sense out of it with: http://www.subsonic.org/pages/api.jsp
142 # open file in wireshard, right click "Hypertext Transfer Protocol", copy, as printable text, put into file /tmp/headers
143 # /a/opt/h2c/h2c </tmp/headers
144 # change from https to http.
145 # then tested out removing stuff i suspected was not important,
146 # and added https and host so it would work remotely.
147 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"
148 done
149
150
151 for plist in ${nav_tags[@]}; do
152 echo "processing $plist"
153 # shellcheck disable=SC2016 # expected beets arg
154 beet ls -f '$path' $plist:t $nav_convert_query | sort | sed 's,\.flac$,.mp3,'> p
155 while read -r path; do
156 flac="${path%.mp3}.flac"
157 if [[ ${flacs[$flac]} ]]; then
158 path="$flac"
159 fi
160 m beet modify -y "path:$path" "$plist!"
161 # files unique to tmpf are in beets not navidrome
162 done < <(comm -23 p $plist)
163 while read -r path; do
164 flac="${path%.mp3}.flac"
165 if [[ ${flacs[$flac]} ]]; then
166 path="$flac"
167 fi
168 m beet modify -y "path:$path" $plist=t
169 # files unique to plist are in navidrome not beets
170 done < <(comm -13 p $plist)
171
172 done
173
174 cd
175 rm -rf $tmpdir