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

How to serialise Pydantic data to JSON? #22

Open
ColdFire87 opened this issue Jan 21, 2023 · 2 comments
Open

How to serialise Pydantic data to JSON? #22

ColdFire87 opened this issue Jan 21, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@ColdFire87
Copy link

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
Copy link
Owner

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
Copy link
Owner

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)

@Russell-Newton Russell-Newton added the enhancement New feature or request label Jan 30, 2023
Russell-Newton added a commit that referenced this issue Jun 15, 2023
* Switch to Pydantic V2
* Videos, creators, and tags are now computed properties requiring the `_api` property to be set
    * TODO: User posts aren't quite working yet

* Should open up more room for #22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants