Skip to content

Commit

Permalink
feat!: implement video queries
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandreteles committed Sep 24, 2024
1 parent 5157017 commit 07529bb
Show file tree
Hide file tree
Showing 7 changed files with 1,030 additions and 4 deletions.
3 changes: 2 additions & 1 deletion TikTok/Queries/Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ async def _fetch_data(
headers=headers,
params=params or {},
json=request_model_class(**(json_data or {})).model_dump(
exclude_none=True
by_alias=True, exclude_none=True, mode="json"
),
)
logger.info(response.text)
# Whoever made the TikTok API return correct data on 500 status codes should be ashamed of themselves
# This behavior happens mainly on the Playlist Info endpoint, which I never saw return a single 200 status code before
if response.is_error and response.status_code < 500:
Expand Down
39 changes: 39 additions & 0 deletions TikTok/Queries/Video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
This module contains the VideoQueries class for handling video-related API queries to the TikTok platform.
"""

from TikTok.ValidationModels import Video

from TikTok.Queries.Common import QueryClass, RequestModel, ResponseModel


class VideoQueries(QueryClass[RequestModel, ResponseModel]):
"""
A class to handle video-related API queries to the TikTok platform.
This class inherits from QueryClass and provides methods to interact with the TikTok API
for retrieving video information.
"""

async def search(
self,
request: Video.VideoQueryRequestModel,
fields: list[Video.VideoQueryFields],
) -> Video.VideoQueryResponseModel:
"""
Searches for videos based on the provided request parameters.
Parameters:
request (Video.VideoQueryRequestModel): The request parameters for the video search.
fields (list[Video.VideoQueryFields]): A list of fields to include in the response.
Returns:
Video.VideoQueryResponseModel: The response model containing the video search results.
"""
return await self._fetch_data(
url=self.query.endpoints.VideoSearchURL,
request_model_class=Video.VideoQueryRequestModel,
response_model_class=Video.VideoQueryResponseModel,
params=self._build_params(fields),
json_data=request.model_dump(by_alias=True, exclude_none=True),
)
3 changes: 3 additions & 0 deletions TikTok/Query.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

from TikTok.Queries.User import UserQueries
from TikTok.Queries.Playlist import PlaylistQueries
from TikTok.Queries.Video import VideoQueries

logger = structlog.get_logger()

Expand All @@ -89,6 +90,7 @@ class Query:
endpoints: APIEndpoints
user: UserQueries
playlist: PlaylistQueries
video: VideoQueries

def __init__(self, auth: OAuth2):
"""
Expand All @@ -102,3 +104,4 @@ def __init__(self, auth: OAuth2):
self.endpoints = APIEndpoints()
self.user = UserQueries(self)
self.playlist = PlaylistQueries(self)
self.video = VideoQueries(self)
14 changes: 12 additions & 2 deletions TikTok/ValidationModels/RestAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ class APIEndpoints(BaseRequestModel):
Model representing the API endpoints for the TikTok API.
This model encapsulates the various endpoints used to interact with the TikTok API,
providing a structured way to access the URLs required for OAuth token requests and
user information retrieval.
providing a structured way to access the URLs required for OAuth token requests, user
information retrieval, and other actions.
Attributes:
OAuthTokenRequestURL (HttpUrl): The URL for requesting an OAuth token.
UserInfoURL (HttpUrl): The URL for retrieving user information.
UserLikedVideosURL (HttpUrl): The URL for retrieving videos liked by a user.
UserPinnedVideosURL (HttpUrl): The URL for retrieving videos pinned by a user.
UserRepostedVideosURL (HttpUrl): The URL for retrieving videos reposted by a user.
UserFollowingURL (HttpUrl): The URL for retrieving users that a specified user is following.
UserFollowersURL (HttpUrl): The URL for retrieving followers of a specified user.
PlaylistInfoURL (HttpUrl): The URL for retrieving information about a specific playlist.
VideoSearchURL (HttpUrl): The URL for searching videos based on specified criteria.
"""

OAuthTokenRequestURL: HttpUrl = (
Expand All @@ -66,3 +73,6 @@ class APIEndpoints(BaseRequestModel):
PlaylistInfoURL: HttpUrl = (
f"{BaseAPI.base_url}/{BaseAPI.api_version}/research/playlist/info/"
)
VideoSearchURL: HttpUrl = (
f"{BaseAPI.base_url}/{BaseAPI.api_version}/research/video/query/"
)
2 changes: 1 addition & 1 deletion TikTok/ValidationModels/User.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class UserVideosQueryFields(StrEnum):
favorites_count = "favorites_count"


class UserInfoResponseDataModel(BaseRequestModel):
class UserInfoResponseDataModel(BaseModel):
"""
Model for user data in the API response.
Expand Down
Loading

0 comments on commit 07529bb

Please sign in to comment.