This is my Code:
import os
import requests
import asyncio
from TikTokApi import TikTokApi
async def get_sound_download_url(sound_id: str) -> str:
"""Get the download URL for a TikTok sound."""
api = TikTokApi()
sound_info = await api.sound(sound_id)
return sound_info["itemInfo"]["itemStruct"]["music"]["playUrl"]
def download_sound(download_url: str, output_path: str):
"""Download a TikTok sound."""
response = requests.get(download_url)
with open(output_path, "wb") as f:
f.write(response.content)
async def main(sound_id: str, output_path: str):
"""Main function to download TikTok sound."""
download_url = await get_sound_download_url(sound_id)
download_sound(download_url, output_path)
if __name__ == "__main__":
sound_id = "7210093783109651758" # Example sound ID
output_path = os.path.join(".", "sound.mp3")
asyncio.run(main(sound_id, output_path))
I get the following error: TypeError: object Sound can't be used in 'await' expression [Finished in 794ms]
I tried to solve it but then the id-paramter is not delivered to the Api.
In summary, this Code should give me the sound download-URL with the specified sound_id using the unofficial library TikTokApi. I am hoping, that you can help me out with this problem. I think I have to structure the Code differently but I don't know how. Also please make your answer simple I am in the early stages of programming in Python and I don't understand everything.