summaryrefslogtreecommitdiff
path: root/ar/.config/rmpc/scripts/on_song_change
blob: 2899ab86fa0e0c42a98190543292af3368b795b6 (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
#!/usr/bin/env sh

# ── dwmblocks signal ─────────────────────────────────────────────────────────
pkill -RTMIN+21 dwmblocks

# ── Desktop notification ──────────────────────────────────────────────────────
TMP_DIR="/tmp/rmpc"
mkdir -p "$TMP_DIR"
ALBUM_ART_PATH="$TMP_DIR/notification_cover"

if ! rmpc albumart --output "$ALBUM_ART_PATH"; then
  ALBUM_ART_PATH=""
fi

if [ -n "$ALBUM_ART_PATH" ]; then
  notify-send -i "$ALBUM_ART_PATH" " Now Playing" "$ARTIST - $TITLE"
else
  notify-send " Now Playing" "$ARTIST - $TITLE"
fi

# ── Play count ────────────────────────────────────────────────────────────────
sticker=$(rmpc sticker get "$FILE" "playCount" 2>/dev/null | jq -r '.value')
if [ -z "$sticker" ]; then
  rmpc sticker set "$FILE" "playCount" "1"
else
  rmpc sticker set "$FILE" "playCount" "$((sticker + 1))"
fi

# ── Auto album tag ───────────────────────────────────────────────────────────
if [ -z "$ALBUM" ] && [ -n "$ARTIST" ] && [ -n "$TITLE" ]; then
  MUSIC_DIR="$HOME/Music"
  SONG_PATH="$MUSIC_DIR/$FILE"

  result=$(curl -sG \
    --data-urlencode "term=$ARTIST $TITLE" \
    --data-urlencode "entity=song" \
    --data-urlencode "limit=1" \
    "https://itunes.apple.com/search")

  found_album=$(echo "$result" | jq -r '.results[0].collectionName')
  found_artist=$(echo "$result" | jq -r '.results[0].artistName')

  if [ -n "$found_album" ] && [ "$found_album" != "null" ]; then
    eyeD3 -A "$found_album" -a "$found_artist" "$SONG_PATH" 2>/dev/null
    rmpc remote --pid "$PID" status "Set album: $found_album" --level info
  else
    rmpc remote --pid "$PID" status "Album not found for $ARTIST - $TITLE" --level warn
  fi
fi

# ── Auto album art download ──────────────────────────────────────────────────
if [ -z "$ALBUM_ART_PATH" ] && [ -n "$ALBUM" ] && [ -n "$ARTIST" ]; then
  MUSIC_DIR="$HOME/Music"
  SONG_PATH="$MUSIC_DIR/$FILE"
  COVER_PATH="$TMP_DIR/cover.jpg"

  ARTWORK_URL=$(curl -sG \
    --data-urlencode "term=$ARTIST $ALBUM" \
    --data-urlencode "entity=album" \
    --data-urlencode "limit=1" \
    "https://itunes.apple.com/search" | jq -r '.results[0].artworkUrl100' | sed 's/100x100bb/600x600bb/')

  if [ -n "$ARTWORK_URL" ] && [ "$ARTWORK_URL" != "null" ]; then
    curl -s -o "$COVER_PATH" "$ARTWORK_URL"
    eyeD3 --add-image="$COVER_PATH:FRONT_COVER" "$SONG_PATH" 2>/dev/null
    rmpc remote --pid "$PID" status "Downloaded album art for $ARTIST - $ALBUM" --level info
  else
    rmpc remote --pid "$PID" status "Album art not found for $ARTIST - $ALBUM" --level warn
  fi
fi

# ── Auto lyrics download ──────────────────────────────────────────────────────
LRCLIB_INSTANCE="https://lrclib.net"

if [ "$HAS_LRC" = "false" ]; then
  mkdir -p "$(dirname "$LRC_FILE")"

  # 1) /api/get with artist + title + album (exact match)
  LYRICS="$(curl -X GET -sG \
    -H "Lrclib-Client: rmpc-$VERSION" \
    --data-urlencode "artist_name=$ARTIST" \
    --data-urlencode "track_name=$TITLE" \
    --data-urlencode "album_name=$ALBUM" \
    "$LRCLIB_INSTANCE/api/get" | jq -r '.syncedLyrics // empty')"

  # 2) /api/get without album
  if [ -z "$LYRICS" ]; then
    LYRICS="$(curl -X GET -sG \
      -H "Lrclib-Client: rmpc-$VERSION" \
      --data-urlencode "artist_name=$ARTIST" \
      --data-urlencode "track_name=$TITLE" \
      "$LRCLIB_INSTANCE/api/get" | jq -r '.syncedLyrics // empty')"
  fi

  # 3) /api/search (fuzzy fallback)
  if [ -z "$LYRICS" ]; then
    LYRICS="$(curl -X GET -sG \
      -H "Lrclib-Client: rmpc-$VERSION" \
      --data-urlencode "artist_name=$ARTIST" \
      --data-urlencode "track_name=$TITLE" \
      "$LRCLIB_INSTANCE/api/search" | jq -r '.[0].syncedLyrics // empty')"
  fi

  if [ -z "$LYRICS" ]; then
    rmpc remote --pid "$PID" status "Lyrics for $ARTIST - $TITLE not found" --level warn
    exit
  fi

  echo "[ar:$ARTIST]" >"$LRC_FILE"
  {
    echo "[al:$ALBUM]"
    echo "[ti:$TITLE]"
  } >>"$LRC_FILE"
  echo "$LYRICS" | sed -E '/^\[(ar|al|ti):/d' >>"$LRC_FILE"

  rmpc remote --pid "$PID" indexlrc --path "$LRC_FILE"
  rmpc remote --pid "$PID" status "Downloaded lyrics for $ARTIST - $TITLE" --level info
fi