论坛里有些人无脑吹 ai,挺反感的。 最近用 ai 写个收音机小工具,花了点时间修改才满意。 请大家挑战一下,谁能用更少的提示词实现以下功能(请注明大模式和提示词): 需求:用户打开终端,输入 fm,播放第一电台,并显示带有编号的节目列表,用户输入编号,播放对应的电台,并且电台列表中对应的电台颜色修改红色。
以下是示例:
#!/bin/bash
set -euo pipefail
SUPPORTED_FMS=(
"清晨音乐台,https://lhttp.qingting.fm/live/4915/64k.mp3"
"AsiaFM 亚洲音乐台,https://lhttp.qingting.fm/live/4581/64k.mp3"
)
# 全局变量
CURRENT_PID=""
SELECTED_NUM=""
CURRENT_PLAY_NAME=""
MAX_NUM=${#SUPPORTED_FMS[@]}
print_radio_info() {
echo "节目列表:"
for idx in "${!SUPPORTED_FMS[@]}"; do
IFS=',' read -r name url <<< "${SUPPORTED_FMS[$idx]}"
local num=$((idx + 1))
if [[ "$SELECTED_NUM" =~ ^[0-9]+$ && "$num" -eq "$SELECTED_NUM" ]]; then
printf " \033[31m[%2d] %s\033[0m\n" "$num" "$name"
else
printf " %2d %s\n" "$num" "$name"
fi
done
}
# 播放节目(参数:节目编号)
play_radio() {
local radio_num="$1"
local index=$((radio_num - 1))
IFS=',' read -r radio_name radio_url <<< "${SUPPORTED_FMS[$index]}"
if ps -p "$CURRENT_PID" >/dev/null 2>&1; then
kill "$CURRENT_PID" >/dev/null 2>&1
wait "$CURRENT_PID" 2>/dev/null
fi
SELECTED_NUM="$radio_num"
CURRENT_PLAY_NAME="$radio_name"
mpg123 -q "$radio_url" >/dev/null 2>&1 < /dev/null &
CURRENT_PID=$!
if ! ps -p "$CURRENT_PID" >/dev/null; then
echo -e "播放失败!请检查网络连接或 mpg123 是否安装。"
CURRENT_PID=""
CURRENT_PLAY_NAME=""
SELECTED_NUM=""
fi
}
main_loop() {
while true; do
clear
print_radio_info
read -r -p "请输入节目编号( 1-$MAX_NUM )或 q 退出: " user_input
if [[ "$user_input" =~ ^[qQ]$ ]]; then
echo "正在退出播放器..."
if ps -p "$CURRENT_PID" >/dev/null 2>&1; then
kill "$CURRENT_PID" >/dev/null 2>&1
wait "$CURRENT_PID" 2>/dev/null
fi
clear
exit 0
fi
if ! [[ "$user_input" =~ ^[0-9]+$ ]]; then
echo -n "错误:请输入有效的数字或 q 退出!"
sleep 1
continue
fi
if [[ "$user_input" -lt 1 || "$user_input" -gt "$MAX_NUM" ]]; then
echo -n "错误:无效的节目编号!请输入 1-$MAX_NUM 之间的数字"
sleep 1
continue
fi
play_radio "$user_input"
done
}
# 程序入口
play_radio 1
main_loop
1
kekxv 4 小时 49 分钟前
|
4
shoushen OP 你把你的提示词发上来啊,别用我的啊。
|
6
Tink PRO |
7
Tink PRO |
8
Tink PRO 代码如下:
import sys import subprocess import threading import time import os # Radio Stations List STATIONS = [ {'id': 1, 'name': '内蒙古新闻广播 (Nei Menggu News)', 'url': 'http://lhttp.qtfm.cn/live/1883/64k.mp3'}, {'id': 2, 'name': '云南新闻广播 (Yunnan News)', 'url': 'http://lhttp.qtfm.cn/live/1926/64k.mp3'}, {'id': 3, 'name': '经典 80 年代 (China 80s)', 'url': 'http://lhttp.qtfm.cn/live/20207/64k.mp3'}, {'id': 4, 'name': 'SAW 80s (International)', 'url': 'http://stream.saw-musikwelt.de/saw-80er/mp3-128/'}, ] current_process = None current_station_id = 1 def clear_screen(): os.system('cls' if os.name == 'nt' else 'clear') def play_station(station): global current_process # Stop existing process if any if current_process: try: current_process.terminate() current_process.wait(timeout=1) except: try: current_process.kill() except: pass # Start new process # -nodisp: no graphical window # -autoexit: exit when stream ends (though streams usually don't end) # -loglevel quiet: suppress output cmd = ['ffplay', '-nodisp', '-autoexit', '-loglevel', 'quiet', station['url']] try: current_process = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except FileNotFoundError: print("Error: ffplay not found. Please install ffmpeg.") sys.exit(1) def print_ui(): clear_screen() print("=== CLI FM Radio ===") print("Enter station ID to switch. Ctrl+C to exit.\n") for station in STATIONS: if station['id'] == current_station_id: # Red color for active station print(f"\033[91m[{station['id']}] {station['name']} (Playing)\033[0m") else: print(f"[{station['id']}] {station['name']}") print("\n> ", end='', flush=True) def main(): global current_station_id # Play default station (first one) play_station(STATIONS[0]) print_ui() try: while True: try: user_input = input() if not user_input.strip(): print_ui() continue new_id = int(user_input.strip()) found = False for station in STATIONS: if station['id'] == new_id: current_station_id = new_id play_station(station) found = True break if not found: # Just refresh UI if invalid input pass print_ui() except ValueError: print_ui() except KeyboardInterrupt: print("\nExiting...") if current_process: current_process.terminate() sys.exit(0) if __name__ == "__main__": main() |
9
Tink PRO |
10
Tink PRO |