Skip to content

Commit

Permalink
feat: add query for reposted videos
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandreteles committed Sep 20, 2024
1 parent b9924e3 commit 9c2c96d
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
63 changes: 63 additions & 0 deletions TikTok/Queries/User.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,66 @@ async def pinned_videos(
f"An unknown exception occurred while querying the TikTok API: {e}"
)
raise e

async def reposted_videos(
self,
username: str,
fields: list[User.UserVideosQueryFields],
max_count: int | None = None,
cursor: int | None = None,
) -> User.UserRepostedVideosResponseModel:
"""
Retrieves a list of videos reposted by the specified TikTok user.
Parameters:
username (str): The username of the TikTok user whose reposted videos are to be retrieved.
fields (list[User.UserVideosQueryFields]): A list of fields to retrieve from the API.
max_count (int | None): The maximum number of reposted videos to retrieve. Defaults to None.
cursor (int | None): A cursor for pagination, allowing retrieval of additional reposted videos. Defaults to None.
Returns:
User.UserRepostedVideosResponseModel: The response data model containing the user's reposted videos.
Raises:
QueryException: If the API query fails or returns an error.
ValidationError: If the response body is invalid according to the expected model.
Exception: For any other unexpected errors that may occur during the API request.
"""
headers = User.UserDataRequestHeadersModel(
authorization=await self.query.auth.get_access_token()
)
try:
response: httpx.Response = await self.query.client.post(
url=self.query.endpoints.UserRepostedVideosURL,
headers=headers.model_dump(by_alias=True),
params={"fields": fields},
json=User.UserRepostedVideosRequestModel(
username=username,
max_count=max_count,
cursor=cursor,
).model_dump(exclude_none=True),
)
if response.status_code != 200:
error_message: dict[str, str] = orjson.loads(response.text)

logger.error(
f"The attempted query failed with the status code: {response.status_code} because {error_message['error']['message']}"
)
raise QueryException(
f"TikTok API query failed because {error_message['error']['message']}"
)
return User.UserRepostedVideosResponseModel(
**orjson.loads(response.content)
)
except QueryException as e:
raise e
except ValidationError as e:
logger.error(
f"The attempted query failed because the response body was invalid: {e}"
)
raise e
except Exception as e:
logger.error(
f"An unknown exception occurred while querying the TikTok API: {e}"
)
raise e
3 changes: 3 additions & 0 deletions TikTok/ValidationModels/RestAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class APIEndpoints(NoExtraFieldsBaseModel):
UserPinnedVideosURL: HttpUrl = (
f"{BaseAPI.base_url}/{BaseAPI.api_version}/research/user/pinned_videos/"
)
UserRepostedVideosURL: HttpUrl = (
f"{BaseAPI.base_url}/{BaseAPI.api_version}/research/user/reposted_videos/"
)
PlaylistInfoURL: HttpUrl = (
f"{BaseAPI.base_url}/{BaseAPI.api_version}/research/playlist/info/"
)
56 changes: 56 additions & 0 deletions TikTok/ValidationModels/User.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,30 @@ class UserPinnedVideosRequestModel(NoExtraFieldsBaseModel):
username: str = Field(description="Username as the unique identifier")


class UserRepostedVideosRequestModel(NoExtraFieldsBaseModel):
"""
Model for the request to retrieve reposted videos of a user.
Attributes:
username (str): The unique identifier of the user whose reposted videos are to be fetched.
max_count (int | None): The maximum number of reposted videos to return in a single response.
The default is 20, with a maximum limit of 100. The actual number of videos returned may be
fewer due to content moderation, deletion, or privacy settings.
cursor (int | None): A Unix timestamp in UTC seconds indicating that only videos created on
or before this time will be returned. The default value is set to the time the request is made.
"""

username: str = Field(description="Username as the unique identifier")
max_count: int | None = Field(
default=None,
description="The maximum number of reposted videos in a single response. Default is 20, max is 100. It is possible that the API returns fewer videos than the max count due to content moderation outcomes, videos being deleted, marked as private by users, or more.",
)
cursor: int | None = Field(
default=None,
description="Videos created on or before this time will be returned. It is a Unix timestamp in UTC seconds. Default value is set as the time this request was made.",
)


class UserVideosDataModel(BaseModel):
"""
Model representing liked video data response data in the API response.
Expand Down Expand Up @@ -289,6 +313,25 @@ class UserPinnedVideosResponseDataModel(BaseModel):
)


class UserRepostedVideosResponseDataModel(BaseModel):
"""
Model for the response data of reposted videos.
Attributes:
cursor (int): A Unix timestamp in UTC seconds indicating where to start retrieving reposted videos.
has_more (bool): Indicates whether there are more reposted videos available for retrieval.
reposted_videos (list[UserVideosDataModel]): A list of data models containing information about the user's reposted videos.
"""

cursor: int = Field(
description="Retrieve reposted videos starting from the specified Unix timestamp in UTC seconds"
)
has_more: bool = Field(description="Whether there are more reposted videos or not")
reposted_videos: list[UserVideosDataModel] = Field(
description="The list of reposted videos"
)


class UserLikedVideosResponseModel(BaseModel):
"""
Model for the complete API response for liked videos.
Expand All @@ -313,3 +356,16 @@ class UserPinnedVideosResponseModel(BaseModel):

data: UserPinnedVideosResponseDataModel
error: ResponseErrorModel


class UserRepostedVideosResponseModel(BaseModel):
"""
Model for the complete API response for reposted videos.
Attributes:
data (UserRepostedVideosDataModel): The returned list of reposted video objects.
error (ResponseErrorModel): Error information, if any.
"""

data: UserRepostedVideosResponseDataModel
error: ResponseErrorModel

0 comments on commit 9c2c96d

Please sign in to comment.