
    ҫfi"                    p    d dl mZ d dlmZmZmZmZ ddlmZ erddl	m
Z
 ddlmZ ddlmZ  G d	 d
      Zy)    )annotations)TYPE_CHECKINGClassVarAsyncIteratorOptional   )InvalidResponseException)	TikTokApi   )Video)Playlistc                      e Zd ZU dZded<   ded<   	 ded<   	 ded<   	 ded	<   	 	 	 	 	 d	 	 	 	 	 	 	 ddZddZdddZdddZ	 d	 	 	 	 	 ddZ	d Z
d Zd Zd Zy
)Userz|
    A TikTok User.

    Example Usage:
        .. code-block:: python

            user = api.user(username='therock')
    zClassVar[TikTokApi]parentstruser_idsec_uidusernamedictas_dictNc                ^    | j                  |||       ||| _        | j                          yy)z|
        You must provide the username or (user_id and sec_uid) otherwise this
        will not function correctly.
        N)!_User__update_id_sec_uid_usernamer   _User__extract_from_data)selfr   r   r   datas        R/home/homepc/tiktok-worker/venv/lib/python3.12/site-packages/TikTokApi/api/user.py__init__zUser.__init__    s4     	))'7HEDL$$&     c                f  K   t        | dd      }|st        d      t        | dd      }||nd||j                  d      d}| j                  j	                  d||j                  d	      |j                  d
             d{   }|t        |d      || _        | j                          |S 7 +w)a  
        Returns a dictionary of information associated with this User.

        Returns:
            dict: A dictionary of information associated with this User.

        Raises:
            InvalidResponseException: If TikTok returns an invalid response, or one we don't understand.

        Example Usage:
            .. code-block:: python

                user_data = await api.user(username='therock').info()
        r   NzJYou must provide the username when creating this class to use this method.r    ms_token)secUiduniqueIdmsTokenz'https://www.tiktok.com/api/user/detail/headerssession_indexurlparamsr%   r&   $TikTok returned an invalid response.)getattr	TypeErrorgetr   make_requestr	   r   r   )r   kwargsr   r   
url_paramsresps         r   infoz	User.info0   s       4T2\  $	40!(!4g" zz*-

 [[--9JJy) **_5	 . 
 
 <*41WXX  "
s   BB1B/,B1c               $  K   t        | dd      }||dk(  r | j                  di | d{    d}||k  r| j                  t        |d      |d}| j                  j                  d||j                  d      |j                  d	      
       d{   }|t        |d      |j                  dg       D ]&  }| j                  j                  |       |dz  }( |j                  dd      sy|j                  d      }||k  ryy7 7 {w)a  
        Returns a user's playlists.

        Returns:
            async iterator/generator: Yields TikTokApi.playlist objects.

        Raises:
            InvalidResponseException: If TikTok returns an invalid response, or one we don't understand.

        Example Usage:
            .. code-block:: python

                async for playlist in await api.user(username='therock').playlists():
                    # do something
        r   Nr    r      r"   countcursorz(https://www.tiktok.com/api/user/playlistr%   r&   r'   r*   playListr   r   hasMoreFr7    )	r+   r2   r   minr   r.   r-   r	   playlist)	r   r6   r7   r/   r   foundr)   r1   r=   s	            r   	playlistszUser.playlists[   s(    " $	40?gm$))%f%%%em,,UB F 11>

9-$jj9	 2  D |.@  !HHZ4 kk***99
 88Iu-XXh'F5 em &s)   *DDA%DDA6D
DDc                 K   t        | dd      }||dk(  r | j                  di | d{    d}||k  r| j                  d|d}| j                  j	                  d||j                  d      |j                  d	      
       d{   }|t        |d      |j                  dg       D ]&  }| j                  j                  |       |dz  }( |j                  dd      sy|j                  d      }||k  ryy7 7 {w)aJ  
        Returns a user's videos.

        Args:
            count (int): The amount of videos you want returned.
            cursor (int): The the offset of videos from 0 you want to get.

        Returns:
            async iterator/generator: Yields TikTokApi.video objects.

        Raises:
            InvalidResponseException: If TikTok returns an invalid response, or one we don't understand.

        Example Usage:
            .. code-block:: python

                async for video in api.user(username="davidteathercodes").videos():
                    # do something
        r   Nr    r   #   r5   z*https://www.tiktok.com/api/post/item_list/r%   r&   r'   r*   itemListr9   r   r:   Fr7   r;   r+   r2   r   r   r.   r-   r	   video	r   r6   r7   r/   r   r>   r)   r1   rD   s	            r   videoszUser.videos   s"    ( $	40?gm$))%f%%%em,, F 11@

9-$jj9	 2  D |.@  *b1 kk''U'33
 88Iu-XXh'F5 em &)   *DDADD	A6D DDc                 K   t        | dd      }||dk(  r | j                  di | d{    d}||k  r| j                  d|d}| j                  j	                  d||j                  d      |j                  d	      
       d{   }|t        |d      |j                  dg       D ]&  }| j                  j                  |       |dz  }( |j                  dd      sy|j                  d      }||k  ryy7 7 {w)az  
        Returns a user's liked posts if public.

        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
        r   Nr    r   rA   r5   z-https://www.tiktok.com/api/favorite/item_listr%   r&   r'   r*   rB   r9   r   r:   Fr7   r;   rC   rE   s	            r   likedz
User.liked   s"    , $	40?gm$))%f%%%em,, F 11C

9-$jj9	 2  D |.@  *b1 kk''U'33
 88Iu-XXh'F5 em &rG   c                   | j                   }|j                         }d|v r/| j                  |d   d   d   |d   d   d   |d   d   d          n| j                  |d   |d   |d          d | j                  | j                  | j
                  fv r>t        j                  j                  j                  d| d|j                                 y y )NuserInfouseridr"   r#   z!Failed to create User with data: z
which has keys )
r   keysr   r   r   r   r   r   loggererror)r   r   rN   s      r   __extract_from_datazUser.__extract_from_data   s    ||yy{--Z (.Z (2Z (4 --T
XZ  DMM4<<>>KK$$3D69J499;-X ?r   c                .    || _         || _        || _        y N)r   r   r   )r   rM   r   r   s       r   __update_id_sec_uid_usernamez!User.__update_id_sec_uid_username  s     r   c                "    | j                         S rS   )__str__)r   s    r   __repr__zUser.__repr__  s    ||~r   c                h    t        | dd       }t        | dd       }t        | dd       }d| d| d| dS )Nr   r   r   zTikTokApi.user(username='z', user_id='z', sec_uid='z'))r+   )r   r   r   r   s       r   rV   zUser.__str__  sJ    4T2$	40$	40*8*L	V]U^^`aar   )NNNN)r   Optional[str]r   rY   r   rY   r   zOptional[dict])returnr   )r4   r   )rZ   zAsyncIterator[Playlist])   r   )rZ   AsyncIterator[Video])r6   intr7   r]   rZ   r\   )__name__
__module____qualname____doc____annotations__r   r2   r?   rF   rI   r   r   rW   rV   r;   r   r   r   r      s      LL"M#M1 #'!%!%#'' ' 	'
 ' )V0(d3(l ./5(5('*5(	5(n,!
br   r   N)
__future__r   typingr   r   r   r   
exceptionsr	   tiktokr
   rD   r   r=   r   r   r;   r   r   <module>rg      s+    " C C 1""Pb Pbr   