From 6a109dd53a20b19ef434c419adf464ebfbd74c7f Mon Sep 17 00:00:00 2001 From: WorldObservationLog Date: Thu, 30 May 2024 08:05:29 +0800 Subject: [PATCH] fix: try to fix KeyError --- src/api.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/api.py b/src/api.py index a427595..40b4219 100644 --- a/src/api.py +++ b/src/api.py @@ -1,5 +1,6 @@ import asyncio import logging +from io import BytesIO from ssl import SSLError from typing import Optional @@ -80,7 +81,14 @@ async def get_token(): stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def download_song(url: str) -> bytes: async with download_lock: - return (await client.get(url)).content + result = BytesIO() + async with client.stream('GET', url) as response: + total = int(response.headers["Content-Length"]) + async for chunk in response.aiter_bytes(): + result.write(chunk) + if len(result.getvalue()) != total: + raise httpx.HTTPError + return result.getvalue() @alru_cache