summarylogtreecommitdiffstats
path: root/sqlchknob
blob: 8844965d679ad54316dc79f0b47c03733a2e57ea (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
#!/usr/bin/env bash

# 🎛 sqlchknob — Interactive tuner

set -euo pipefail
IFS=$'\n\t'

CONFIG_DIR="$HOME/.config/sqlch"
STATION_FILE="$CONFIG_DIR/stations"
LAST_PLAYED="$CONFIG_DIR/last"
DEFAULT_STATION="$CONFIG_DIR/default"
SOCKET="/tmp/mpvsocket"

mkdir -p "$CONFIG_DIR"
touch "$STATION_FILE" "$LAST_PLAYED"

ensure_gum() {
  if ! command -v gum >/dev/null; then
    echo "❌ 'gum' required but not found in PATH."
    exit 1
  fi
}

splash() {
  gum style --foreground 2 --border double --padding "1 2" --margin "1 1" <<EOF
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓   sqlchKNOB v3   ▓▓
▓▓────────────────────▓▓
▓▓   â€ĸ Tune In        ▓▓
▓▓   â€ĸ Drop Out       ▓▓
▓▓   â€ĸ Mono Loud      ▓▓
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
EOF
}

choose_play() {
  local choices name
  choices=$(cut -d= -f1 "$STATION_FILE")
  [[ -z "$choices" ]] && echo "âš ī¸ No stations available." && return
  name=$(echo "$choices" | gum choose)
  [[ -n "$name" ]] && sqlchctl play "$name"
}

search_add() {
  local query
  query=$(gum input --placeholder "Search RadioBrowser")
  [[ -z "$query" ]] && return

  local fetched
  fetched=$(curl -s "https://de1.api.radio-browser.info/json/stations/search?name=$query" | jq -r '.[] | "\(.name)=\(.url_resolved)"')
  if [[ -z "$fetched" ]]; then
    gum style --foreground 1 --border double "❌ No results found for '$query'"
    return
  fi

  local results
  results=$(echo "$fetched" | gum choose --no-limit)
  [[ -z "$results" ]] && return

  # Ensure the stations file exists
  mkdir -p "$(dirname "$STATION_FILE")"
  touch "$STATION_FILE"

  # Normalize and save to stations file
  echo "$results" | tr -d '\r' | while IFS= read -r line; do
    [[ -n "$line" ]] && grep -qxF "$line" "$STATION_FILE" || echo "$line" >> "$STATION_FILE"
  done

  gum style --foreground 3 "✅ Saved new station(s) to $STATION_FILE"
  echo "DEBUG: Writing to $STATION_FILE" >&2
  ls -l "$STATION_FILE" >&2
}

browse_genre() {
  local genre
  genre=$(curl -s https://de1.api.radio-browser.info/json/genres | jq -r '.[].name' | gum choose --placeholder "Pick a genre")
  [[ -z "$genre" ]] && return

  local fetched
  fetched=$(curl -s "https://de1.api.radio-browser.info/json/stations/bytag/$genre" | jq -r '.[] | "\(.name)=\(.url_resolved)"')
  if [[ -z "$fetched" ]]; then
    gum style --foreground 1 --border double "❌ No stations found for '$genre'"
    return
  fi

  local results
  results=$(echo "$fetched" | tr '\r' '
' | gum choose --no-limit)

  if [[ -z "$results" ]]; then
    return
  fi

  while IFS= read -r line; do
    [[ -n "$line" ]] && grep -qxF "$line" "$STATION_FILE" || echo "$line" >> "$STATION_FILE"
  done <<< "$results"
}

edit_station() {
  local tmpfile backup
  tmpfile=$(mktemp)
  backup="${STATION_FILE}.bak.$(date +%s)"
  cp "$STATION_FILE" "$backup"
  cp "$STATION_FILE" "$tmpfile"
  gum write --placeholder "Edit your stations..." < "$tmpfile" > "$tmpfile" && mv "$tmpfile" "$STATION_FILE"
}

delete_station() {
  local name
  name=$(cut -d= -f1 "$STATION_FILE" | gum choose --placeholder "Select station to delete")
  [[ -n "$name" ]] && sed -i "/^\*\?$name=/d" "$STATION_FILE"
}

show_status() {
  sqlchctl status
  sleep 2
}

stop_playback() {
  sqlchctl stop
}

main_menu() {
  splash
  local choice
  choice=$(gum choose "🎧 Play Station" "🔍 Search & Add" "🎚 Browse by Genre" "âœī¸ Edit Station" "🗑 Delete Station" "â„šī¸ Now Playing" "⛔ Stop" "đŸšĒ Quit")
  case "$choice" in
    "🎧 Play Station") choose_play ;;
    "🔍 Search & Add") search_add ;;
    "🎚 Browse by Genre") browse_genre ;;
    "âœī¸ Edit Station") edit_station ;;
    "🗑 Delete Station") delete_station ;;
    "â„šī¸ Now Playing") show_status ;;
    "⛔ Stop") stop_playback ;;
    "đŸšĒ Quit") exit 0 ;;
  esac
}

ensure_gum
while true; do main_menu; done