summaryrefslogtreecommitdiff
path: root/ar/.local/bin/qndl-artist
blob: 9c57cc244e62fdc98524563d0f2f021a7a1e8425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/sh
# qndl-artist — 아티스트 폴더/메타데이터 통일 헬퍼
export LC_ALL="${LC_ALL:-C.UTF-8}"

MUSIC="${XDG_MUSIC_DIR:-$HOME/Music}"
ALIASES="${QNDL_ALIASES:-${XDG_DOTFILES_DIR:-$HOME/.dotfiles}/global/Music/artist-aliases.tsv}"

# 이름 → 표준명. 맵 정확일치 → 기존 폴더 대소문자무시 매칭 → 원본.
cmd_normalize() {
  _name="$1"
  if [ -f "$ALIASES" ]; then
    _c="$(awk -F'\t' -v n="$_name" '/^#/ || NF < 2 { next } $1 == n { print $2; exit }' "$ALIASES")"
    [ -n "$_c" ] && { printf '%s\n' "$_c"; return 0; }
  fi
  if [ -d "$MUSIC" ]; then
    _m="$(find "$MUSIC" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 2>/dev/null |
      awk -v n="$_name" 'tolower($0) == tolower(n) { print; exit }')"
    [ -n "$_m" ] && { printf '%s\n' "$_m"; return 0; }
  fi
  printf '%s\n' "$_name"
}

# 후크용: 파일의 현재 아티스트 폴더명을 표준명으로 해석해 apply.
cmd_apply_download() {
  _fp="$1"
  case "$_fp" in
  "$MUSIC"/*) : ;;
  *) return 0 ;;   # Music 밖이면 무시
  esac
  _rel="${_fp#"$MUSIC"/}"
  _seg="${_rel%%/*}"
  _canon="$(cmd_normalize "$_seg")"
  cmd_apply "$_fp" "$_canon"
}

# mp3 1개를 표준 폴더로 이동(필요시) + album_artist 설정.
cmd_apply() {
  _fp="$1"; _canon="$2"
  [ -f "$_fp" ] || { printf 'apply: no such file: %s\n' "$_fp" >&2; return 1; }
  case "$_fp" in
  "$MUSIC"/*) : ;;
  *) printf 'apply: not under %s: %s\n' "$MUSIC" "$_fp" >&2; return 1 ;;
  esac
  _rel="${_fp#"$MUSIC"/}"
  _artist_seg="${_rel%%/*}"
  _subpath="${_rel#*/}"          # album/.../title.mp3
  if [ "$_artist_seg" != "$_canon" ]; then
    _destdir="$MUSIC/$_canon/$(dirname "$_subpath")"
    _dest="$_destdir/$(basename "$_fp")"
    if [ -e "$_dest" ]; then
      printf 'apply: skip (exists): %s\n' "$_dest" >&2
      return 0
    fi
    mkdir -p "$_destdir"
    mv "$_fp" "$_dest" || return 1
    # 빈 원본 앨범/아티스트 폴더 정리 (MUSIC 루트는 지우지 않음)
    rmdir "$MUSIC/$_artist_seg/$(dirname "$_subpath")" 2>/dev/null || true
    rmdir "$MUSIC/$_artist_seg" 2>/dev/null || true
    _fp="$_dest"
  fi
  _tmp="$(dirname "$_fp")/.qndl-tag-$$.mp3"
  if ffmpeg -v error -y -i "$_fp" -map 0 -c copy -metadata album_artist="$_canon" "$_tmp" 2>/dev/null; then
    mv "$_tmp" "$_fp" || { rm -f "$_tmp"; return 1; }
  else
    rm -f "$_tmp"
    printf 'apply: tag failed: %s\n' "$_fp" >&2
    return 1
  fi
}

# MUSIC의 모든 아티스트 폴더를 <name>\t<mp3수>로 출력.
_artist_counts() {
  find "$MUSIC" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 2>/dev/null |
    while IFS= read -r _d; do
      _n="$(find "$MUSIC/$_d" -type f -name '*.mp3' 2>/dev/null | wc -l)"
      printf '%s\t%s\n' "$_d" "$_n"
    done
}

# stdin: <name>\t<count>. stdout: 실제 중복 그룹만 <canonical>\t<m1>\t<m2>...
_group_awk() {
  awk -F'\t' '
    function norm(x,   y){ y=tolower(x); gsub(/[^[:alnum:]가-힣]/,"",y); return y }
    function find(a){ while(parent[a]!=a){ parent[a]=parent[parent[a]]; a=parent[a] } return a }
    function union(a,b,  ra,rb){ ra=find(a); rb=find(b); if(ra!=rb) parent[rb]=ra }
    function addtok(idx,chunk,  k){ k=norm(chunk); if(k=="") return; if(k in owner) union(owner[k],idx); else owner[k]=idx }
    function caserank(s,  u,l){ u=(s ~ /[A-Z]/); l=(s ~ /[a-z]/); return (u&&l)?2:1 }
    {
      name[NR]=$1; cnt[NR]=$2+0; parent[NR]=NR
      s=$1; gsub(/(/,"(",s); gsub(/)/,")",s); rest=s
      while (match(rest,/\([^)]*\)/)) {
        addtok(NR, substr(rest,RSTART+1,RLENGTH-2))
        rest=substr(rest,1,RSTART-1) " " substr(rest,RSTART+RLENGTH)
      }
      addtok(NR, rest)
    }
    END{
      for(i=1;i<=NR;i++){ r=find(i); members[r]=members[r] i " " }
      for(r in members){
        n=split(members[r], m, " ")
        real=0; for(j=1;j<=n;j++) if(m[j]!="") real++
        if(real<2) continue
        haveEng=0
        for(j=1;j<=n;j++){ if(m[j]=="") continue; if(name[m[j]] !~ /[가-힣]/) haveEng=1 }
        best=""; brank=-1; bcnt=-1
        for(j=1;j<=n;j++){
          if(m[j]=="") continue
          nm=name[m[j]]
          if(haveEng && nm ~ /[가-힣]/) continue
          cr=caserank(nm); c=cnt[m[j]]
          if(cr>brank || (cr==brank && c>bcnt) || (cr==brank && c==bcnt && (best=="" || nm<best))){
            best=nm; brank=cr; bcnt=c
          }
        }
        line=best
        for(j=1;j<=n;j++){ if(m[j]=="") continue; line=line "\t" name[m[j]] }
        print line
      }
    }
  '
}

# stdin(그룹 라인) → 각 그룹의 비표준 멤버 이동 파일 수 계산해 dry-run 한 줄 출력.
_merge_preview() {
  while IFS="$(printf '\t')" read -r _canon _rest; do
    [ -z "$_canon" ] && continue
    _others=""; _files=0
    _oldifs="$IFS"; IFS="$(printf '\t')"
    for _mem in $_rest; do
      [ "$_mem" = "$_canon" ] && continue
      _others="${_others:+$_others, }$_mem"
      _c="$(find "$MUSIC/$_mem" -type f -name '*.mp3' 2>/dev/null | wc -l)"
      _files=$((_files + _c))
    done
    IFS="$_oldifs"
    [ -z "$_others" ] && continue
    printf '%s → %s (move %s files)\n' "$_others" "$_canon" "$_files"
  done
}

cmd_merge() {
  _apply=0
  [ "${1:-}" = "--apply" ] && _apply=1
  _groups="$(_artist_counts | _group_awk)"
  if [ -z "$_groups" ]; then
    printf 'No case/paren duplicate groups found.\n'
    return 0
  fi
  if [ "$_apply" -eq 0 ]; then
    printf '%s\n' "$_groups" | _merge_preview
    printf '\n(dry-run) 실제 병합하려면: qndl-artist merge --apply\n'
    return 0
  fi
  cmd_merge_apply "$_groups"
}

_sub="${1:-}"
[ $# -gt 0 ] && shift
case "$_sub" in
normalize) cmd_normalize "$@" ;;
apply) cmd_apply "$@" ;;
apply-download) cmd_apply_download "$@" ;;
merge) cmd_merge "$@" ;;
*) printf 'usage: qndl-artist {normalize|apply|apply-download|merge} ...\n' >&2; exit 2 ;;
esac