Russell-Newton / TikTokPy

Extract data from TikTok without needing any login information or API keys.
https://pypi.org/project/tiktokapipy/
MIT License
197 stars 25 forks source link

How to serialise Pydantic data to JSON? #22

Open ColdFire87 opened 1 year ago

ColdFire87 commented 1 year ago

Hi, I'm trying this code but I'm getting an error:

from tiktokapipy.api import TikTokAPI

if __name__ == '__main__':
    with TikTokAPI(
            scroll_down_time=2.5, navigation_retries=2, navigation_timeout=10
    ) as api:        
        challenge = api.challenge("foo")
        for video in challenge.videos.sorted_by(lambda vid: vid.create_time):
            with open(f"videos/{video.id}.json", "w") as file:
                file.write(video.json())
File "pydantic/json.py", line 90, in pydantic.json.pydantic_encoder
TypeError: Object of type 'LightUserGetter' is not JSON serializable

Any suggestions would be helpful, thanks!

Russell-Newton commented 1 year ago

At the moment, I think the only solution is to use exclude=["creator", "tags"] when calling video.json(). Similarly if you want to serialize a models.user.User or models.challenge.Challenge, you'll need to use exclude=["videos"].

I'll look into a way to make this automatic so you don't have to do it.

Russell-Newton commented 1 year ago

Also I would suggest adapting your code to only specify a scroll_down_time for the api.challenge call if you don't want to scroll down on videos. This will make your runtime faster (see #19)