oscie57 / tiktok-voice

Simple Python script to interact with the TikTok TTS API
515 stars 74 forks source link

KeyError: 'data' #55

Open RABIDYOTE opened 2 months ago

RABIDYOTE commented 2 months ago

Whenever I try to use the script with a file, it throws me this message which doesn't entirely seem to line up with any status code in particular. I've tried both using a file with multiple lines and one with just one line. I've also tried the recommended Session ID as well as my own, and making sure the aid value is set correctly.

Traceback (most recent call last):
  File "C:\Users\jprig\Downloads\tiktok-voice-main\main.py", line 158, in <module>
    main()
  File "C:\Users\jprig\Downloads\tiktok-voice-main\main.py", line 126, in main
    tts(args.session, text_speaker, item, f'{batch_dir}{i}.mp3', False)
  File "C:\Users\jprig\Downloads\tiktok-voice-main\main.py", line 31, in tts
    vstr = [r.json()["data"]["v_str"]][0]
            ~~~~~~~~^^^^^^^^
KeyError: 'data'

I also tried to use it with just a text argument, but it outputs this instead:

Traceback (most recent call last):
  File "C:\Users\jprig\Downloads\tiktok-voice-main\main.py", line 158, in <module>
    main()
  File "C:\Users\jprig\Downloads\tiktok-voice-main\main.py", line 138, in main
    tts(args.session, text_speaker, req_text, filename, play)
  File "C:\Users\jprig\Downloads\tiktok-voice-main\main.py", line 31, in tts
    vstr = [r.json()["data"]["v_str"]][0]
            ~~~~~~~~^^^^^^^^
KeyError: 'data'

I'm unfortunately not very code-savvy, so I'm a bit confused on what's going on and how I can rectify it.

bdwyer1911 commented 1 month ago

I'm getting the same error. Were you able to find a fix?

jvatsal21 commented 1 month ago

Also getting the same issue

jvatsal21 commented 1 month ago

Alright, I got the fix. First change this line: if r.json()["message"] == "Couldn't load speech. Try again.": to: if r.json()["message"] == "Couldn’t load speech. Try again.": This is just a minor error in apostrophe comparison; however, the main issue is that the code thinks your sessionID is incorrect. The way I fixed this is by changing this line: API_BASE_URL = f"https://api16-normal-v6.tiktokv.com/media/api/text/speech/invoke/" to: API_BASE_URL = f"https://api16-normal-useast5.us.tiktokv.com/media/api/text/speech/invoke/". This should do the job!