sourcecode6687/Gupshup
0
1import asyncio2import random3from aiohttp import web4from main import AudioEngine5 6# --- CONFIGURATION ---7RADIO_HOST = "live.radioking.com" 8RADIO_PORT = "80"9RADIO_MOUNT = "/radiounlimited"10RADIO_USER = "Vedant_Rajpurohit" 11RADIO_PASS = "Yuvraj@2003" 12 13ICECAST_URL = f"icecast://{RADIO_USER}:{RADIO_PASS}@{RADIO_HOST}:{RADIO_PORT}{RADIO_MOUNT}"14 15# --- AUTODJ LIST (Add your songs here) ---16AUTODJ_PLAYLIST = [17 "Arijit Singh Lofi",18 "Bollywood LoFi Mix 2024",19 "Atif Aslam hits",20 "KK Best Songs",21 "Sidhu Moose Wala Mix",22 "Slowed Reverb Hindi Songs"23]24 25# Global State26queue = []27current_meta = {"title": "Offline", "user": "System", "status": "idle"}28engine = AudioEngine(ICECAST_URL)29 30# --- RADIO LOOP ---31async def radio_loop():32 print("📻 Radio Loop Started")33 while True:34 try:35 # 1. Check Queue (User Requests)36 if len(queue) > 0:37 item = queue.pop(0)38 query = item['query']39 user = item['user']40 41 # 2. Check AutoDJ (If Queue Empty)42 else:43 query = random.choice(AUTODJ_PLAYLIST)44 user = "AutoDJ"45 print(f"🤖 AutoDJ picking: {query}")46 47 # --- PLAY LOGIC ---48 current_meta['status'] = "fetching"49 50 # Fetch URL (Real Audio)51 data = await engine.get_stream_url(query)52 53 if data:54 current_meta.update({55 "title": data['title'],56 "user": user,57 "status": "playing"58 })59 print(f"▶️ Streaming: {data['title']}")60 61 # Stream the audio (This blocks until song finishes)62 await engine.stream(data['url'], is_silence=False)63 else:64 print("❌ Fetch failed, retrying...")65 await asyncio.sleep(2)66 67 await asyncio.sleep(0.5)68 69 except Exception as e:70 print(f"⚠️ Loop Error: {e}")71 # If crash, play silence briefly to keep connection alive72 await engine.stream("silence", is_silence=True)73 await asyncio.sleep(5)74 75# --- API ENDPOINTS ---76async def handle_play(request):77 try:78 data = await request.json()79 query = data.get('query')80 user = data.get('user', 'Guest')81 if not query: return web.json_response({"error": "No query"}, status=400)82 83 queue.append({'query': query, 'user': user})84 85 # If AutoDJ is currently playing, we can optionally stop it to play request immediately86 # engine.stop() 87 88 return web.json_response({"status": "queued", "pos": len(queue)})89 except Exception as e:90 return web.json_response({"error": str(e)}, status=500)91 92async def handle_status(request):93 return web.json_response({94 "meta": current_meta,95 "queue_length": len(queue),96 "queue": queue[:5]97 })98 99async def handle_skip(request):100 engine.stop()101 return web.json_response({"status": "skipped"})102 103async def handle_clear(request):104 queue.clear()105 engine.stop()106 return web.json_response({"status": "cleared"})107 108async def handle_root(request):109 return web.Response(text="Hugging Face Radio Online")110 111# --- STARTUP ---112async def start_background_tasks(app):113 asyncio.create_task(radio_loop())114 115def main():116 app = web.Application()117 app.router.add_get('/', handle_root)118 app.router.add_post('/play', handle_play)119 app.router.add_get('/status', handle_status)120 app.router.add_post('/skip', handle_skip)121 app.router.add_post('/clear', handle_clear)122 123 app.on_startup.append(start_background_tasks)124 web.run_app(app, port=7860)125 126if __name__ == "__main__":127 main()128 