From 627eb8c5d029a0d68f6c9a81c9d74b1a8d22f3c6 Mon Sep 17 00:00:00 2001 From: Pranav Dronavalli <62522813+dronavallipranav@users.noreply.github.com> Date: Fri, 18 Aug 2023 16:47:40 -0500 Subject: [PATCH] :tada: 6.1.0 Add user like :tada: (#1043) * implemented user like method * added test for user likes * bump version to 6.1.0 --------- Co-authored-by: davidteather <34144122+davidteather@users.noreply.github.com> --- .sphinx/conf.py | 2 +- CITATION.cff | 4 ++-- TikTokApi/api/user.py | 54 +++++++++++++++++++++++++++++++++++++++---- setup.py | 2 +- tests/test_user.py | 3 +-- 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/.sphinx/conf.py b/.sphinx/conf.py index 865c2eb0..a4b7a93d 100644 --- a/.sphinx/conf.py +++ b/.sphinx/conf.py @@ -16,7 +16,7 @@ project = "TikTokAPI" copyright = "2023, David Teather" author = "David Teather" -release = "v6.0.1" +release = "v6.1.0" # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/main/usage/configuration.html#general-configuration diff --git a/CITATION.cff b/CITATION.cff index c01f96f8..54eafde2 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -5,5 +5,5 @@ authors: orcid: "https://orcid.org/0000-0002-9467-4676" title: "TikTokAPI" url: "https://github.com/davidteather/tiktok-api" -version: 6.0.1 -date-released: 2023-8-8 +version: 6.1.0 +date-released: 2023-8-18 diff --git a/TikTokApi/api/user.py b/TikTokApi/api/user.py index aae8eaa3..7486ef28 100644 --- a/TikTokApi/api/user.py +++ b/TikTokApi/api/user.py @@ -140,13 +140,59 @@ async def videos(self, count=30, cursor=0, **kwargs) -> Iterator[Video]: cursor = resp.get("cursor") - def liked(self, count: int = 30, cursor: int = 0, **kwargs) -> Iterator[Video]: + async def liked(self, count: int = 30, cursor: int = 0, **kwargs) -> Iterator[Video]: """ - Returns a dictionary listing TikToks that a given a user has liked. + Returns a user's liked posts if public. - TODO: Not currently implemented + Args: + count (int): The amount of recent likes you want returned. + cursor (int): The the offset of likes from 0 you want to get. + + Returns: + async iterator/generator: Yields TikTokApi.video objects. + + Raises: + InvalidResponseException: If TikTok returns an invalid response, the user's likes are private, or one we don't understand. + + Example Usage: + .. code-block:: python + + async for like in api.user(username="davidteathercodes").liked(): + # do something """ - raise NotImplementedError + sec_uid = getattr(self, "sec_uid", None) + if sec_uid is None or sec_uid == "": + await self.info(**kwargs) + + found = 0 + while found < count: + params = { + "secUid": self.sec_uid, + "count": 35, + "cursor": cursor, + } + + resp = await self.parent.make_request( + url="https://www.tiktok.com/api/favorite/item_list", + params=params, + headers=kwargs.get("headers"), + session_index=kwargs.get("session_index"), + ) + + if resp is None: + raise InvalidResponseException( + resp, "TikTok returned an invalid response." + ) + + for video in resp.get("itemList", []): + yield self.parent.video(data=video) + found += 1 + + if not resp.get("hasMore", False): + return + + cursor = resp.get("cursor") + def __extract_from_data(self): data = self.as_dict diff --git a/setup.py b/setup.py index f135f64f..b7afa050 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setuptools.setup( name="TikTokApi", packages=setuptools.find_packages(), - version="6.0.1", + version="6.1.0", license="MIT", description="The Unofficial TikTok API Wrapper in Python 3.", author="David Teather", diff --git a/tests/test_user.py b/tests/test_user.py index d50f0a1a..5c36ba02 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -38,11 +38,10 @@ async def test_user_videos(): @pytest.mark.asyncio async def test_user_likes(): - pytest.skip("Not implemented yet") api = TikTokApi() async with api: await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3) - user = api.user(username=username, sec_uid=sec_uid, user_id=user_id) + user = api.user(username="publicliketest", sec_uid="MS4wLjABAAAAHjhwCIwmvzVZfRrDAZ2aZy74LciLnoyaPfM2rrX9N7bwbWMFuwTFG4YrByYvsH5c") count = 0 async for video in user.liked(count=30):