async def save_audio(url):
chunk_size = 20 * 2 ** 20 # 20 MB
file_name = f"media/{shortuuid.uuid()}.mp3" # Save as mp3
with open(file_name, ‘wb’) as f:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.read()
json_data = json.loads(data)
audio_url = json_data.get(‘url’)
if audio_url:
async with session.get(audio_url) as audio_response:
while True:
chunk = await audio_response.content.read(chunk_size)
if not chunk:
break
f.write(chunk)
return file_name
The url doesn’t return a json, so you shouldn’t be parsing it as json at
json_data = json.loads(data)
Here is my code.This code can’t give me audio.But I can download videos with this code.
Do you have solution in Python ???
async def save_audio(url):
chunk_size = 20 * 2 ** 20 # 20 MB
file_name = f"media/{shortuuid.uuid()}.mp3" # Save as mp3
with open(file_name, ‘wb’) as f:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.read()
json_data = json.loads(data)
audio_url = json_data.get(‘url’)
if audio_url:
async with session.get(audio_url) as audio_response:
while True:
chunk = await audio_response.content.read(chunk_size)
if not chunk:
break
f.write(chunk)
return file_name
can you show you’re code?