This is a teeny tiny API/Proxy to run on my server so you can see what I'm listening to on my website.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add no exception on close

+16 -15
+16 -15
src/main.py
··· 1 1 import asyncio 2 2 import base64 3 - import os 4 - import sys 5 3 import traceback 6 4 7 5 import uvicorn 8 6 from fastapi import FastAPI, Request, Response, WebSocket 9 7 from fastapi.middleware.cors import CORSMiddleware 10 8 from fastapi.websockets import WebSocketState 9 + from websockets import ConnectionClosedOK 11 10 12 11 from subsonic import get_cover_art, get_now_playing 13 12 ··· 149 148 @app.get("/") 150 149 async def root(request: Request, inline: int = 0): 151 150 await ensure_updating() 152 - print(request.url, file=sys.stderr) 153 151 actual_size = now_playing.get_cover_art_size(inline) 154 152 return { 155 153 "playing": now_playing.playing, ··· 217 215 last_size = None 218 216 last_is_playing = None 219 217 220 - while websocket.client_state == WebSocketState.CONNECTED: 221 - if now_playing.playing != last_is_playing: 222 - await websocket.send_json(now_playing.get_is_playing()) 223 - last_is_playing = now_playing.playing 218 + try: 219 + while websocket.client_state == WebSocketState.CONNECTED: 220 + if now_playing.playing != last_is_playing: 221 + await websocket.send_json(now_playing.get_is_playing()) 222 + last_is_playing = now_playing.playing 224 223 225 - if now_playing.revision != last_revision: 226 - await websocket.send_json(now_playing.get_metadata()) 227 - last_size = None 228 - last_revision = now_playing.revision 224 + if now_playing.revision != last_revision: 225 + await websocket.send_json(now_playing.get_metadata()) 226 + last_size = None 227 + last_revision = now_playing.revision 229 228 230 - if (new_size := now_playing.get_cover_art_size(res)) != last_size: 231 - await websocket.send_json(now_playing.get_cover_art(size=new_size)) 232 - last_size = new_size 233 - await asyncio.sleep(0.5) 229 + if (new_size := now_playing.get_cover_art_size(res)) != last_size: 230 + await websocket.send_json(now_playing.get_cover_art(size=new_size)) 231 + last_size = new_size 232 + await asyncio.sleep(0.5) 233 + except ConnectionClosedOK: 234 + return 234 235 235 236 236 237 if __name__ == "__main__":