cc9ca15c2a11efc4dd3da37e83daeb63cbea64a5
[distro-setup] / nav2beet
1 #!/bin/bash
2
3 f=/usr/local/lib/err;test -r $f || { echo "error: $0 no $f" >&2;exit 1;}; . $f
4
5 plists=(
6 # these are useful tags
7 expl
8 gimicky
9 sad
10 # these are normal playlists
11 love
12 pump1
13 pumprap
14 rend
15 run
16 )
17
18
19 # these options are mainly for debugging / developing quickly.
20 plist_only=false
21 dry_run=false
22 while (( $# )); do
23 case $1 in
24 plist)
25 plist_only=true
26 shift
27 ;;
28 dry)
29 dry_run=true
30 shift
31 ;;
32 *)
33 echo "error. unexpected arg read script"
34 exit 1
35 ;;
36 esac
37 done
38
39 m ()
40 {
41 printf "%s\n" "$*" >&2
42 if $dry_run && [[ $1 == beet && $2 == modify ]]; then
43 echo "dry run: $*"
44 else
45 "$@"
46 fi
47 }
48
49 tmpf=$(mktemp)
50
51 declare -A flacs
52
53 declare -A navirating
54 tmpdir=$(mktemp -d)
55 cd $tmpdir
56 if [[ $HOSTNAME != kd ]]; then
57 ssh b8.nz bash -s <<EOF | tar xz
58 install -m 700 -d /tmp/nav2beet
59 cd /tmp/nav2beet
60 for r in 1 2 3 4 5; do
61 sqlite3 /i/navidrome/navidrome.db ".output \$r" "select path from annotation inner join media_file on item_id = id where rating = \$r;"
62 done
63 find /i/m -type f -name '*.flac' >flacs
64 for plist in ${plists[@]}; do
65 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
66 done
67 tar cz -C /tmp nav2beet
68 EOF
69
70 cd nav2beet
71 else
72 for r in 1 2 3 4 5; do
73 sqlite3 /i/navidrome/navidrome.db ".output $r" "select path from annotation inner join media_file on item_id = id where rating = $r;"
74 done
75 find /i/m -type f -name '*.flac' >flacs
76 for plist in ${plists[@]}; do
77 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
78 done
79 fi
80 while read -r l; do
81 flacs[$l]=t
82 done <flacs
83
84 if ! $plist_only; then
85 echo begin star rating import from navidrome to beets
86 # todo: consider if this is a problem: file removed/renamed in main
87 # collection, but not yet updated navidrome, we want to skip it not
88 # die.
89
90 for r in 1 2 3 4 5; do
91 while read -r path; do
92 beetpath="/i/m${path#/i/converted}"
93 flac="${beetpath%.mp3}.flac"
94 if [[ ${flacs[$flac]} ]]; then
95 beetpath="$flac"
96 fi
97 navirating[$beetpath]=$r
98 done <$r
99 done
100 declare -A beetrating
101 for r in 1 2 3 4 5; do
102 m beet ls -f '$path' rating:$r >$tmpf
103 while read -r path; do
104 beetrating[$path]=$r
105 done <$tmpf
106 done
107
108 for path in "${!navirating[@]}"; do
109 r="${navirating[$path]}"
110 if [[ $r != "${beetrating[$path]}" ]]; then
111 # note: this assumes there are no cases like filea.mp3 filea.mp3.mp3, which would affect both files.
112 echo "$r != ${beetrating[$path]}, beet modify -y path:$path rating=$r"
113 beet modify -y "path:$path" "rating=$r"
114 fi
115 done
116 # end star rating import from navidrome to beets:
117 fi
118
119
120 echo begin import navidrome playlists as flexible attribute with value t.
121 # These are only the playlists listed in the beets config.yaml
122 # "subsonicplaylist:" and then duplicated here:
123
124
125
126
127 for plist in ${plists[@]}; do
128 echo "processing $plist"
129 beet ls -f '$path' $plist:t ^genre:spoken-w ^genre:skit ^rating:1 | sort | sed 's,\.flac$,.mp3,'> p
130 while read -r path; do
131 flac="${path%.mp3}.flac"
132 if [[ ${flacs[$flac]} ]]; then
133 path="$flac"
134 fi
135 m beet modify -y "path:$path" "$plist!"
136 # files unique to tmpf are in beets not navidrome
137 done < <(comm -23 p $plist)
138 while read -r path; do
139 flac="${path%.mp3}.flac"
140 echo "flac=$flac path=$path ${flacs[$flac]} ${flacs[flac]}"
141 if [[ ${flacs[$flac]} ]]; then
142 path="$flac"
143 fi
144 m beet modify -y "path:$path" $plist=t
145 # files unique to plist are in navidrome not beets
146 done < <(comm -13 p $plist)
147
148 done
149
150 cd
151 rm -rf $tmpdir