
    lj5                     "   d Z ddlZddlZddlmZ ddlmZmZ  G d de      Z G d de      Z	 G d	 d
      Z
 G d d      Z G d d      Zg dg dg ddZdZdZdZdZdZdZdZdZdZdZdZdZdZd Zd!Zd"Zd#d$d%d&d'Zd(xZZ d)xZ!Z"d*xZ#Z$d+xZ%Z&y),a  
You can work with Mobile Sync on other devices straight away, but if you'd like to use your app as a `sync-target` (when
you can set items to be synced to your app) you need to init some variables.

.. code-block:: python

    def init_sync():
        import plexapi
        plexapi.X_PLEX_PROVIDES = 'sync-target'
        plexapi.BASE_HEADERS['X-Plex-Sync-Version'] = '2'
        plexapi.BASE_HEADERS['X-Plex-Provides'] = plexapi.X_PLEX_PROVIDES

        # mimic iPhone SE
        plexapi.X_PLEX_PLATFORM = 'iOS'
        plexapi.X_PLEX_PLATFORM_VERSION = '11.4.1'
        plexapi.X_PLEX_DEVICE = 'iPhone'

        plexapi.BASE_HEADERS['X-Plex-Platform'] = plexapi.X_PLEX_PLATFORM
        plexapi.BASE_HEADERS['X-Plex-Platform-Version'] = plexapi.X_PLEX_PLATFORM_VERSION
        plexapi.BASE_HEADERS['X-Plex-Device'] = plexapi.X_PLEX_DEVICE

You have to fake platform/device/model because transcoding profiles are hardcoded in Plex, and you obviously have
to explicitly specify that your app supports `sync-target`.
    N)
PlexObject)NotFound
BadRequestc                   F     e Zd ZdZd Zd fd	Zd Zd Zd Zd Z	d Z
 xZS )	SyncItemaX  
    Represents single sync item, for specified server and client. When you saying in the UI to sync "this" to "that"
    you're basically creating a sync item.

    Attributes:
        id (int): unique id of the item.
        clientIdentifier (str): an identifier of Plex Client device, to which the item is belongs.
        machineIdentifier (str): the id of server which holds all this content.
        version (int): current version of the item. Each time you modify the item (e.g. by changing amount if media to
                       sync) the new version is created.
        rootTitle (str): the title of library/media from which the sync item was created. E.g.:

            * when you create an item for an episode 3 of season 3 of show Example, the value would be `Title of
              Episode 3`
            * when you create an item for a season 3 of show Example, the value would be `Season 3`
            * when you set to sync all your movies in library named "My Movies" to value would be `My Movies`.

        title (str): the title which you've set when created the sync item.
        metadataType (str): the type of media which hides inside, can be `episode`, `movie`, etc.
        contentType (str): basic type of the content: `video` or `audio`.
        status (:class:`~plexapi.sync.Status`): current status of the sync.
        mediaSettings (:class:`~plexapi.sync.MediaSettings`): media transcoding settings used for the item.
        policy (:class:`~plexapi.sync.Policy`): the policy of which media to sync.
        location (str): plex-style library url with all required filters / sorting.
    c                 >    t         t        |   |||       || _        y N)superr   __init__clientIdentifier)selfserverdatainitpathr   	__class__s        9/DATA/.local/lib/python3.12/site-packages/plexapi/sync.pyr   zSyncItem.__init__<   s    h&vtX> 0    c                    t         j                  j                  t        |j                  j                  d            | _        t         j                  j                  t        |j                  j                  d            | _        |j                  j                  d      | _        |j                  j                  d      | _	        |j                  j                  d      | _
        |j                  j                  d      | _        |j                  d      j                  d      | _        t        di |j                  d	      j                  | _        t!        di |j                  d
      j                  | _        t%        di |j                  d      j                  | _        |j                  d      j                  j                  dd      | _        y)/ Load attribute values from Plex XML response. idversion	rootTitletitlemetadataTypecontentTypeServermachineIdentifierStatusMediaSettingsPolicyLocationuri N )plexapiutilscastintattribgetr   r   r   r   r   r   findr   r   statusr   mediaSettingsr    policylocation)r   r   s     r   	_loadDatazSyncItem._loadData@   s9   --$$S$++//$*?@}}))#t{{y/IJ5[[__W-
 KKOON;;;??=9!%8!4!8!89L!M:tyy299:*OTYY-G-N-NO:tyy299:		*-4488Cr   c                     | j                   j                         D cg c]  }|j                  | j                  k(  s|  }}t	        |      dk(  rt        d| j                         |d   S c c}w )zN Returns :class:`~plexapi.myplex.MyPlexResource` with server of current item. r   z Unable to find server with uuid )_server	resourcesr   r   lenr   )r   sr   s      r   r   zSyncItem.serverN   sn    !\\335f59K9KtOeOe9e!5fv;!=d>T>T=UVWWay gs
   A-A-c                 ~    | j                         j                         }d| j                   }|j                  |      S )zQ Returns list of :class:`~plexapi.base.Playable` which belong to this sync item. z/sync/items/)r   connectr   
fetchItems)r   r   keys      r   getMediazSyncItem.getMediaU   s7    &&(TWWI&  %%r   c                     d| j                    d|j                   d}|j                  j                  |t        j
                         y)z Mark the file as downloaded (by the nature of Plex it will be marked as downloaded within
            any SyncItem where it presented).

            Parameters:
                media (base.Playable): the media to be marked as downloaded.
        z/sync/z/item/z/downloaded)methodN)r   	ratingKeyr2   queryrequestsput)r   mediaurls      r   markDownloadedzSyncItem.markDownloaded[   s?     t,,-VEOO3DKPC5r   c                    t         j                  j                  | j                        }|dt	        | j
                        z   z  }| j                  j                  || j                  j                  j                         y)z Removes current SyncItem )clientId/N)
SyncListr9   formatr   strr   r2   r>   _sessiondelete)r   rB   s     r   rK   zSyncItem.deletee   sZ    ll!!4+@+@!AsS\!!3 5 5 < <=r   )NN)__name__
__module____qualname____doc__TAGr   r0   r   r:   rC   rK   __classcell__)r   s   @r   r   r       s.    2 C1D&6>r   r   c                       e Zd ZdZdZd Zd Zy)rG   a7   Represents a Mobile Sync state, specific for single client, within one SyncList may be presented
        items from different servers.

        Attributes:
            clientId (str): an identifier of the client.
            items (List<:class:`~plexapi.sync.SyncItem`>): list of registered items to sync.
    z-https://plex.tv/devices/{clientId}/sync_itemsc                     |j                   j                  d      | _        g | _        |j	                  d      }|rT|j                  d      D ]?  }t        | j                  || j                        }| j                  j                  |       A yy)r   r   	SyncItemsr   )r   N)	r)   r*   rE   itemsr+   iterr   r2   append)r   r   	syncItems	sync_itemitems        r   r0   zSyncList._loadDataw   so    (:;
IIk*	&^^J7	i$--X

!!$' 8 r   N)rL   rM   rN   rO   r9   rP   r0   r$   r   r   rG   rG   l   s     :C
C	(r   rG   c                       e Zd ZdZd Zd Zy)r   a   Represents a current status of specific :class:`~plexapi.sync.SyncItem`.

        Attributes:
            failureCode: unknown, never got one yet.
            failure: unknown.
            state (str): server-side status of the item, can be `completed`, `pending`, empty, and probably something
                         else.
            itemsCount (int): total items count.
            itemsCompleteCount (int): count of transcoded and/or downloaded items.
            itemsDownloadedCount (int): count of downloaded items.
            itemsReadyCount (int): count of transcoded items, which can be downloaded.
            totalSize (int): total size in bytes of complete items.
            itemsSuccessfulCount (int): unknown, in my experience it always was equal to `itemsCompleteCount`.
    c
                    t         j                  j                  t        |      | _        t         j                  j                  t        |      | _        t         j                  j                  t        |      | _        || _        |	| _        t         j                  j                  t        |      | _	        || _
        t         j                  j                  t        |      | _        t         j                  j                  t        |      | _        y r	   )r%   r&   r'   r(   itemsDownloadedCount	totalSizeitemsReadyCountfailureCodefailureitemsSuccessfulCountstateitemsCompleteCount
itemsCount)
r   re   rd   rc   r^   r]   r_   rb   r`   ra   s
             r   r   zStatus.__init__   s    $+MM$6$6s<P$Q! ++C;&}}11#G&$+MM$6$6s<P$Q!
")--"4"4S:L"M!--,,S*=r   c                     t        | j                  | j                  | j                  | j                  | j
                        }d| j                  j                   d| S )N)re   rd   r]   r_   rb   <z>:)dictre   rd   r]   r_   rb   r   rL   )r   ds     r   __repr__zStatus.__repr__   sW    #66!%!:!: 00!%!:!:
 4>>**+2aS11r   N)rL   rM   rN   rO   r   rj   r$   r   r   r   r      s    
>2r   r   c                   L    e Zd ZdZ	 	 ddZed        Zed        Zed        Zy)r   a   Transcoding settings used for all media within :class:`~plexapi.sync.SyncItem`.

        Attributes:
            audioBoost (int): unknown.
            maxVideoBitrate (int|str): maximum bitrate for video, may be empty string.
            musicBitrate (int|str): maximum bitrate for music, may be an empty string.
            photoQuality (int): photo quality on scale 0 to 100.
            photoResolution (str): maximum photo resolution, formatted as WxH (e.g. `1920x1080`).
            videoResolution (str): maximum video resolution, formatted as WxH (e.g. `1280x720`, may be empty).
            subtitleSize (int): subtitle size on scale 0 to 100.
            videoQuality (int): video quality on scale 0 to 100.
    c	                 ^   t         j                  j                  t        |      | _        |dk7  r$t         j                  j                  t        |      nd| _        |dk7  r$t         j                  j                  t        |      nd| _        |dk7  r$t         j                  j                  t        |      nd| _        || _        || _	        |dk7  r$t         j                  j                  t        |      nd| _
        |dk7  r*t         j                  j                  t        |      | _        y d| _        y )Nr#   )r%   r&   r'   r(   
audioBoostmaxVideoBitratemusicBitratephotoQualityphotoResolutionvideoResolutionsubtitleSizevideoQuality)	r   rn   rt   rr   rm   ro   rp   rq   rs   s	            r   r   zMediaSettings.__init__   s    !--,,S*=KZ^`K`w}}11#GfhEQUWEWGMM..sLA]_EQUWEWGMM..sLA]_..EQUWEWGMM..sLA]_EQUWEWGMM..sLA]_r   c                     | t         k(  rt        ddd      S | t        t        d         k  r+t        t        d   |    t        d   |    t        d   |          S t	        d      )ah   Returns a :class:`~plexapi.sync.MediaSettings` object, based on provided video quality value.

            Parameters:
                videoQuality (int): idx of quality of the video, one of VIDEO_QUALITY_* values defined in this module.

            Raises:
                :exc:`~plexapi.exceptions.BadRequest`: When provided unknown video quality.
        r#   bitratert   rr   zUnexpected video quality)VIDEO_QUALITY_ORIGINALr   r4   VIDEO_QUALITIESr   )rt   s    r   createVideozMediaSettings.createVideo   sr     11 R,,C	 :;; !;L!I!0!@!N!01B!CL!QS S 788r   c                     t        |       S )a   Returns a :class:`~plexapi.sync.MediaSettings` object, based on provided music quality value

            Parameters:
                bitrate (int): maximum bitrate for synchronized music, better use one of MUSIC_BITRATE_* values from the
                               module
        )ro   )r   )rv   s    r   createMusiczMediaSettings.createMusic   s     '22r   c                 P    | t         v rt        t         |    |       S t        d      )a   Returns a :class:`~plexapi.sync.MediaSettings` object, based on provided photo quality value.

            Parameters:
                resolution (str): maximum allowed resolution for synchronized photos, see PHOTO_QUALITY_* values in the
                                  module.

            Raises:
                :exc:`~plexapi.exceptions.BadRequest`: When provided unknown video quality.
        )rp   rq   zUnexpected photo quality)PHOTO_QUALITIESr   r   )
resolutions    r   createPhotozMediaSettings.createPhoto   s+     ( oj.I[eff788r   N)i  d   1280x720r      J   	1920x1080r   )	rL   rM   rN   rO   r   staticmethodry   r{   r   r$   r   r   r   r      sS     gj^a	` 9 9$ 3 3 9 9r   r   c                   *    e Zd ZdZddZedd       Zy)r    aX   Policy of syncing the media (how many items to sync and process watched media or not).

        Attributes:
            scope (str): type of limitation policy, can be `count` or `all`.
            value (int): amount of media to sync, valid only when `scope=count`.
            unwatched (bool): True means disallow to sync watched media.
    c                     || _         t        j                  j                  t        |      | _        t        j                  j                  t        |      | _        y r	   )scoper%   r&   r'   bool	unwatchedr(   value)r   r   r   r   s       r   r   zPolicy.__init__   s9    
 ++D)<]]''U3
r   Nc                 .    d}| d} nd}t        |||       S )ah   Creates a :class:`~plexapi.sync.Policy` object for provided options and automatically sets proper `scope`
            value.

            Parameters:
                limit (int): limit items by count.
                unwatched (bool): if True then watched items wouldn't be synced.

            Returns:
                :class:`~plexapi.sync.Policy`.
        allr   count)r    )limitr   r   s      r   createzPolicy.create   s'     =EEeY..r   )r   )NF)rL   rM   rN   rO   r   r   r   r$   r   r   r    r       s     4
 / /r   r    )@   `      @  i  i  g     @@g     p@g     @@g     @@g     @g     p@g     @)220x128r   284x160420x240576x320720x480r   r   r   r   r   r   r   )
         r   (   <   r   K   r   r   r   Z   r   )rv   rr   rt                        	   r         r      r   r      1   r   c   )r   r   r   	3840x2160r   r   r   r   )'rO   r?   r%   plexapi.baser   plexapi.exceptionsr   r   r   rG   r   r   r    rx   VIDEO_QUALITY_0_2_MBPSVIDEO_QUALITY_0_3_MBPSVIDEO_QUALITY_0_7_MBPSVIDEO_QUALITY_1_5_MBPS_480pVIDEO_QUALITY_2_MBPS_720pVIDEO_QUALITY_3_MBPS_720pVIDEO_QUALITY_4_MBPS_720pVIDEO_QUALITY_8_MBPS_1080pVIDEO_QUALITY_10_MBPS_1080pVIDEO_QUALITY_12_MBPS_1080pVIDEO_QUALITY_20_MBPS_1080prw   AUDIO_BITRATE_96_KBPSAUDIO_BITRATE_128_KBPSAUDIO_BITRATE_192_KBPSAUDIO_BITRATE_320_KBPSr}   PHOTO_QUALITY_HIGHESTPHOTO_QUALITY_2160pPHOTO_QUALITY_HIGHPHOTO_QUALITY_1080pPHOTO_QUALITY_MEDIUMPHOTO_QUALITY_720pPHOTO_QUALITY_LOWPHOTO_QUALITY_480pr$   r   r   <module>r      s  0   # 3I>z I>X(z (.$2 $2ND9 D9N /  /H QXJ	                     	 /: 9 ++6 6 (,6 6 ))2 2 &r   