V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
shoushen
V2EX  ›  程序员

ai 挑战赛

  •  
  •   shoushen · 5 小时 20 分钟前 · 310 次点击

    论坛里有些人无脑吹 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
    
    10 条回复    2025-11-21 00:35:19 +08:00
    shoushen
        2
    shoushen  
    OP
       4 小时 43 分钟前
    @kekxv 你把提示词和模型名称发上来
    kekxv
        3
    kekxv  
       4 小时 40 分钟前
    你点链接就能看到啊😂
    @shoushen
    shoushen
        4
    shoushen  
    OP
       4 小时 31 分钟前
    你把你的提示词发上来啊,别用我的啊。
    shoushen
        5
    shoushen  
    OP
       4 小时 31 分钟前
    Tink
        6
    Tink  
    PRO
       3 小时 54 分钟前


    这个算数不

    Tink
        7
    Tink  
    PRO
       3 小时 52 分钟前
    我靠图居然挂了



    Tink
        8
    Tink  
    PRO
       3 小时 50 分钟前
    代码如下:

    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()
    Tink
        10
    Tink  
    PRO
       3 小时 14 分钟前
    我又在 ai studio 里试了一下网页版的,更离谱了。

    和 anti-gravity 一样,也是一句搞定。




    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   939 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:50 · PVG 03:50 · LAX 11:50 · JFK 14:50
    ♥ Do have faith in what you're doing.