Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - Missing 'author' key in __extract_from_data causes KeyError #1057

Closed
trislee opened this issue Sep 1, 2023 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@trislee
Copy link
Contributor

trislee commented Sep 1, 2023

Describe the bug

When using the api.hashtag method, the API responses from a small percentage of videos don't have an 'author' field, resulting in the error KeyError: 'author'.

The buggy code

For example, retrieving videos that use the hashtag "juliusevola" results in the error:

import asyncio
import os

from TikTokApi import TikTokApi

MS_TOKEN = os.environ.get("MS_TOKEN", None)
BAD_HASHTAG = 'juliusevola'

async def fetch_hashtag_videos():
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[MS_TOKEN])
        async for video in api.hashtag(name=BAD_HASHTAG).videos():
            print(video.id)

if __name__ == "__main__":
    asyncio.run(fetch_hashtag_videos())

We see on the TikTok website that when searching for that specific hashtag, there is indeed a video (id 7063900161278166533) that doesn't have an associated username. This may be because the creator was banned or their account was deleted.

Expected behavior

One video that doesn't have an associated username shouldn't cause the whole request to fail. If there is no associated username for a given video, a null value for the 'author' attribute seems like a reasonable choice.

Error Trace (if any)

Traceback (most recent call last):
  File "<MY DIRECTORY>/mwe.py", line 15, in <module>
    asyncio.run(fetch_hashtag_videos())
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "<MY DIRECTORY>/mwe.py", line 11, in fetch_hashtag_videos
    async for video in api.hashtag(name = 'juliusevola').videos(count = 1000):
  File "<MY VIRTUAL ENVIRONMENT>/lib/python3.9/site-packages/TikTokApi/api/hashtag.py", line 131, in videos
    yield self.parent.video(data=video)
  File "<MY VIRTUAL ENVIRONMENT>/lib/python3.9/site-packages/TikTokApi/api/video.py", line 60, in __init__
    self.__extract_from_data()
  File "<MY VIRTUAL ENVIRONMENT>/lib/python3.9/site-packages/TikTokApi/api/video.py", line 191, in __extract_from_data
    author = data["author"]
KeyError: 'author'

Desktop (please complete the following information):

  • OS: Ubuntu 20.04
  • TikTokApi Version: 6.1.1

Additional context

The issue appears to be straightforward, changing line 191 of TikTokApi/api/video.py in __extract_from_data from author = data["author"] to author = data.get("author") fixes this problem.

@trislee trislee added the bug Something isn't working label Sep 1, 2023
@trislee
Copy link
Contributor Author

trislee commented Feb 9, 2024

Fixed in #1113

@trislee trislee closed this as completed Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant