File size: 2,587 Bytes
7e4b742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import annotations

from typing import ClassVar, Iterator, Optional
from typing import TYPE_CHECKING, ClassVar, Optional

from TikTok.TikTokApi.exceptions import InvalidResponseException

if TYPE_CHECKING:
    from ..tiktok import TikTokApi
    from .user import User


class Comment:
    """
    A TikTok Comment.

    Example Usage
        .. code-block:: python

            for comment in video.comments:
                print(comment.text)
                print(comment.as_dict)
    """

    parent: ClassVar[TikTokApi]

    id: str
    """The id of the comment"""
    author: ClassVar[User]
    """The author of the comment"""
    text: str
    """The contents of the comment"""
    likes_count: int
    """The amount of likes of the comment"""
    as_dict: dict
    """The raw data associated with this comment"""

    def __init__(self, data: Optional[dict] = None):
        if data is not None:
            self.as_dict = data
            self.__extract_from_data()

    def __extract_from_data(self):
        data = self.as_dict
        self.id = self.as_dict["cid"]
        self.text = self.as_dict["text"]

        usr = self.as_dict["user"]
        self.author = self.parent.user(
            user_id=usr["uid"], username=usr["unique_id"], sec_uid=usr["sec_uid"]
        )
        self.likes_count = self.as_dict["digg_count"]

    async def replies(self, count=20, cursor=0, **kwargs) -> Iterator[Comment]:
        found = 0

        while found < count:
            params = {
                "count": 20,
                "cursor": cursor,
                "item_id": self.author.user_id,
                "comment_id": self.id,
            }

            resp = await self.parent.make_request(
                url="https://www.tiktok.com/api/comment/list/reply/",
                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 comment in resp.get("comments", []):
                yield self.parent.comment(data=comment)
                found += 1

            if not resp.get("has_more", False):
                return

            cursor = resp.get("cursor")

    def __repr__(self):
        return self.__str__()

    def __str__(self):
        id = getattr(self, "id", None)
        text = getattr(self, "text", None)
        return f"TikTokApi.comment(comment_id='{id}', text='{text}')"