repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
fetch-lyrics98 lines3.9 KiBraw download
#!/usr/bin/env bash
# rmpc on_song_change hook: fetch synced lyrics from LRCLIB when missing.
#
# rmpc provides (among others): $ARTIST $TITLE $ALBUM $DURATION $FILE
#                               $HAS_LRC $LRC_FILE $PID $VERSION
# Writes to $LRC_FILE (under lyrics_dir), then hot-indexes it via
# `rmpc remote indexlrc`. Never blocks rmpc: all work happens in a
# backgrounded subshell; all failures are silent (logged to cache dir).

CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/rmpc-lyrics"
MISS_DIR="$CACHE_DIR/misses"
LOG_FILE="$CACHE_DIR/fetch.log"
LRCLIB="https://lrclib.net"
MISS_TTL_DAYS=14

main() {
    mkdir -p "$MISS_DIR"

    # Nothing to do without metadata or if lyrics already resolved.
    [ "${HAS_LRC:-true}" = "false" ] || exit 0
    [ -n "${ARTIST:-}" ] && [ -n "${TITLE:-}" ] || exit 0
    [ -n "${LRC_FILE:-}" ] || exit 0
    [ -s "$LRC_FILE" ] && exit 0

    # Negative cache: don't hammer LRCLIB for known misses.
    local key miss
    key=$(printf '%s\t%s\t%s' "$ARTIST" "$TITLE" "${ALBUM:-}" | sha1sum | cut -d' ' -f1)
    miss="$MISS_DIR/$key"
    if [ -e "$miss" ] && [ -z "$(find "$miss" -mtime +"$MISS_TTL_DAYS" 2>/dev/null)" ]; then
        exit 0
    fi

    # Serialize concurrent hook invocations (fast track skipping).
    exec 9>"$CACHE_DIR/lock"
    flock -n 9 || exit 0

    # DURATION may be seconds ("312.43") or "MM:SS"; normalise to integer seconds.
    local dur_s=""
    case "${DURATION:-}" in
        *:*) dur_s=$(( $(echo "$DURATION" | awk -F: '{print $(NF-1)*60 + int($NF)}') )) ;;
        *[0-9]*) dur_s=$(printf '%.0f' "$DURATION" 2>/dev/null || true) ;;
    esac

    # Try /api/get with maximum specificity first, then progressively relax.
    local resp="" synced="" instrumental=""
    for attempt in full noalbum bare; do
        local args=(-sG --max-time 15 -H "Lrclib-Client: rmpc-${VERSION:-unknown}"
                    --data-urlencode "artist_name=$ARTIST"
                    --data-urlencode "track_name=$TITLE")
        case $attempt in
            full)
                [ -n "${ALBUM:-}" ] && args+=(--data-urlencode "album_name=$ALBUM")
                [ -n "$dur_s" ] && args+=(--data-urlencode "duration=$dur_s")
                ;;
            noalbum)
                [ -n "$dur_s" ] && args+=(--data-urlencode "duration=$dur_s")
                ;;
        esac
        resp=$(curl "${args[@]}" "$LRCLIB/api/get" 2>>"$LOG_FILE") || resp=""
        synced=$(printf '%s' "$resp" | jq -r '.syncedLyrics // empty' 2>/dev/null)
        instrumental=$(printf '%s' "$resp" | jq -r '.instrumental // false' 2>/dev/null)
        [ -n "$synced" ] && break
        [ "$instrumental" = "true" ] && break
        sleep 1   # be polite between retries
    done

    if [ -z "$synced" ]; then
        # Instrumental or genuinely not found: remember the miss, stay silent.
        touch "$miss"
        printf '%s MISS %s - %s\n' "$(date -Iseconds)" "$ARTIST" "$TITLE" >>"$LOG_FILE"
        exit 0
    fi

    # Write the .lrc with metadata tags so rmpc's index matches it to the
    # song regardless of file path. UTF-8 passes through untouched.
    mkdir -p "$(dirname "$LRC_FILE")"
    local tmp
    tmp=$(mktemp "$(dirname "$LRC_FILE")/.fetch.XXXXXX") || exit 0
    {
        printf '[ar:%s]\n' "$ARTIST"
        [ -n "${ALBUM:-}" ] && printf '[al:%s]\n' "$ALBUM"
        printf '[ti:%s]\n' "$TITLE"
        printf '%s\n' "$synced" | sed -E '/^\[(ar|al|ti|length):/d'
    } >"$tmp"
    mv -f "$tmp" "$LRC_FILE"
    rm -f "$miss"
    printf '%s HIT  %s - %s -> %s\n' "$(date -Iseconds)" "$ARTIST" "$TITLE" "$LRC_FILE" >>"$LOG_FILE"

    # Tell the running rmpc instance about the new file (hot reload).
    if [ -n "${PID:-}" ]; then
        rmpc remote --pid "$PID" indexlrc --path "$LRC_FILE" 2>>"$LOG_FILE" || true
        rmpc remote --pid "$PID" status "Lyrics downloaded: $ARTIST - $TITLE" --level info 2>>"$LOG_FILE" || true
    fi
}

# Never block rmpc's hook runner.
main >/dev/null 2>&1 &
exit 0