
    gV=                     ,   d dl Z d dlmZ d dlmZ d dlmZmZmZ d dl	m
Z
mZmZ d dlmZ d dlmZmZmZmZmZmZmZ ej.                   G d d	eeeee             Zej.                   G d
 dee
eeee             Zej.                   G d dee             Zy)    N)Path)
quote_plus)mediautilsvideo)PlayablePlexPartialObjectPlexSession)
BadRequest)RatingMixinArtUrlMixinArtMixinPosterUrlMixinPosterMixinPhotoalbumEditMixinsPhotoEditMixinsc                   l    e 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ZddZed        Zy)
Photoalbuma   Represents a single Photoalbum (collection of photos).

        Attributes:
            TAG (str): 'Directory'
            TYPE (str): 'photo'
            addedAt (datetime): Datetime the photo album was added to the library.
            art (str): URL to artwork image (/library/metadata/<ratingKey>/art/<artid>).
            composite (str): URL to composite image (/library/metadata/<ratingKey>/composite/<compositeid>)
            fields (List<:class:`~plexapi.media.Field`>): List of field objects.
            guid (str): Plex GUID for the photo album (local://229674).
            images (List<:class:`~plexapi.media.Image`>): List of image objects.
            index (sting): Plex index number for the photo album.
            key (str): API URL (/library/metadata/<ratingkey>).
            lastRatedAt (datetime): Datetime the photo album was last rated.
            librarySectionID (int): :class:`~plexapi.library.LibrarySection` ID.
            librarySectionKey (str): :class:`~plexapi.library.LibrarySection` key.
            librarySectionTitle (str): :class:`~plexapi.library.LibrarySection` title.
            listType (str): Hardcoded as 'photo' (useful for search filters).
            ratingKey (int): Unique key identifying the photo album.
            summary (str): Summary of the photoalbum.
            thumb (str): URL to thumbnail image (/library/metadata/<ratingKey>/thumb/<thumbid>).
            title (str): Name of the photo album. (Trip to Disney World)
            titleSort (str): Title to use when sorting (defaults to title).
            type (str): 'photo'
            updatedAt (datetime): Datetime the photo album was updated.
            userRating (float): Rating of the photo album (0.0 - 10.0) equaling (0 stars - 5 stars).
    	Directoryphoto
photoalbumc                    t        j                  |j                  j                  d            | _        |j                  j                  d      | _        |j                  j                  d      | _        | j                  |t        j                        | _
        |j                  j                  d      | _        | j                  |t        j                        | _        t        j                  t        |j                  j                  d            | _        |j                  j                  dd      j#                  dd      | _        t        j                  |j                  j                  d	            | _        t        j                  t        |j                  j                  d
            | _        |j                  j                  d      | _        |j                  j                  d      | _        d| _        t        j                  t        |j                  j                  d            | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d| j6                        | _        |j                  j                  d      | _        t        j                  |j                  j                  d            | _        t        j                  t>        |j                  j                  d            | _         y)/ Load attribute values from Plex XML response. addedAtart	compositeguidindexkey 	/childrenlastRatedAtlibrarySectionIDlibrarySectionKeylibrarySectionTitler   	ratingKeysummarythumbtitle	titleSorttype	updatedAt
userRatingN)!r   
toDatetimeattribgetr   r   r   	findItemsr   Fieldfieldsr   Imageimagescastintr   replacer   r"   r#   r$   r%   listTyper&   r'   r(   r)   r*   r+   r,   floatr-   selfdatas     "/opt/Tautulli/lib/plexapi/photo.py	_loadDatazPhotoalbum._loadData6   s   ''	(BC;;??5)5nnT5;;7KKOOF+	nnT5;;7ZZT[[__W%=>
;;??5"-55k2F ++DKKOOM,JK %

3@R0S T!%1D!E#';;??3H#I C)EF{{y1[[__W-
[[__W-
djjAKKOOF+	))$++//+*FG**UDKKOOL,IJ    c                 P    | j                    d}| j                  |t        |      S )z Returns the :class:`~plexapi.photo.Photoalbum` that matches the specified title.

            Parameters:
                title (str): Title of the photo album to return.
        r!   title__iexact)r   	fetchItemr   r<   r)   r   s      r>   albumzPhotoalbum.albumN   s)     
)$~~c:U~CCr@   c                 P    | j                    d} | j                  |t        fi |S )zL Returns a list of :class:`~plexapi.photo.Photoalbum` objects in the album. r!   )r   
fetchItemsr   r<   kwargsr   s      r>   albumszPhotoalbum.albumsW   s*    
)$tsJ9&99r@   c                 P    | j                    d}| j                  |t        |      S )z Returns the :class:`~plexapi.photo.Photo` that matches the specified title.

            Parameters:
                title (str): Title of the photo to return.
        r!   rB   )r   rD   PhotorE   s      r>   r   zPhotoalbum.photo\   s)     
)$~~c5~>>r@   c                 P    | j                    d} | j                  |t        fi |S )zG Returns a list of :class:`~plexapi.photo.Photo` objects in the album. r!   )r   rH   rM   rI   s      r>   photoszPhotoalbum.photose   s*    
)$tsE4V44r@   c                 d    | j                    d}| j                  |t        j                  |      S )z Returns the :class:`~plexapi.video.Clip` that matches the specified title.

            Parameters:
                title (str): Title of the clip to return.
        r!   rB   )r   rD   r   CliprE   s      r>   clipzPhotoalbum.clipj   s-     
)$~~c5::U~CCr@   c                 d    | j                    d} | j                  |t        j                  fi |S )zF Returns a list of :class:`~plexapi.video.Clip` objects in the album. r!   )r   rH   r   rQ   rI   s      r>   clipszPhotoalbum.clipss   s.    
)$tsEJJ9&99r@   c                 $    | j                  |      S )z3 Alias to :func:`~plexapi.photo.Photoalbum.photo`. )episode)r<   r)   s     r>   r0   zPhotoalbum.getx   s    ||E""r@   Nc                 0   g }| j                         D ]E  }|r*t        j                  j                  ||j                        n|}||j                  ||      z  }G | j                         | j                         z   D ]  }||j                  ||      z  } |S )a   Download all photos and clips from the photo album. See :func:`~plexapi.base.Playable.download` for details.

            Parameters:
                savepath (str): Defaults to current working dir.
                keep_original_name (bool): True to keep the original filename otherwise
                    a friendlier filename is generated.
                subfolders (bool): True to separate photos/clips in to photo album folders.
        )rK   ospathjoinr)   downloadrO   rT   )r<   savepathkeep_original_name
subfolders	filepathsrF   	_savepathr   s           r>   r[   zPhotoalbum.download|   s     	[[] 	GE?IXu{{;xI	3EFFI	G [[]TZZ\1 	FE2DEEI	Fr@   c                 T    | j                   j                  |d| j                  d      S z3 Get the Plex Web URL with the correct parameters. details   )baseendpointr   legacy)_server_buildWebURLr   r<   re   s     r>   
_getWebURLzPhotoalbum._getWebURL   s%    ||((dYDHH]^(__r@   c                     t        j                  | j                        }t        t	        d      dz  |d   z  |dd  dz        S zL Returns the Plex Media Server data directory where the metadata is stored. MetadataPhotosr   rd   Nz.bundle)r   sha1hashr   strr   r<   	guid_hashs     r>   metadataDirectoryzPhotoalbum.metadataDirectory   sG     NN499-	4
#h.1=9QR=/QX@YYZZr@   )NFFN)__name__
__module____qualname____doc__TAGTYPE_searchTyper?   rF   rK   r   rO   rR   rT   r0   r[   rk   propertyrt    r@   r>   r   r      sd    6 CDKK0D:
?5
D:
#"` [ [r@   r   c                   d    e Zd ZdZd ZdZdZd Zd Zd Z	d Z
ed        Zdd	Zdd
Zed        Zy)rM   a	   Represents a single Photo.

        Attributes:
            TAG (str): 'Photo'
            TYPE (str): 'photo'
            addedAt (datetime): Datetime the photo was added to the library.
            createdAtAccuracy (str): Unknown (local).
            createdAtTZOffset (int): Unknown (-25200).
            fields (List<:class:`~plexapi.media.Field`>): List of field objects.
            guid (str): Plex GUID for the photo (com.plexapp.agents.none://231714?lang=xn).
            images (List<:class:`~plexapi.media.Image`>): List of image objects.
            index (sting): Plex index number for the photo.
            key (str): API URL (/library/metadata/<ratingkey>).
            lastRatedAt (datetime): Datetime the photo was last rated.
            librarySectionID (int): :class:`~plexapi.library.LibrarySection` ID.
            librarySectionKey (str): :class:`~plexapi.library.LibrarySection` key.
            librarySectionTitle (str): :class:`~plexapi.library.LibrarySection` title.
            listType (str): Hardcoded as 'photo' (useful for search filters).
            media (List<:class:`~plexapi.media.Media`>): List of media objects.
            originallyAvailableAt (datetime): Datetime the photo was added to Plex.
            parentGuid (str): Plex GUID for the photo album (local://229674).
            parentIndex (int): Plex index number for the photo album.
            parentKey (str): API URL of the photo album (/library/metadata/<parentRatingKey>).
            parentRatingKey (int): Unique key identifying the photo album.
            parentThumb (str): URL to photo album thumbnail image (/library/metadata/<parentRatingKey>/thumb/<thumbid>).
            parentTitle (str): Name of the photo album for the photo.
            ratingKey (int): Unique key identifying the photo.
            sourceURI (str): Remote server URI (server://<machineIdentifier>/com.plexapp.plugins.library)
                (remote playlist item only).
            summary (str): Summary of the photo.
            tags (List<:class:`~plexapi.media.Tag`>): List of tag objects.
            thumb (str): URL to thumbnail image (/library/metadata/<ratingKey>/thumb/<thumbid>).
            title (str): Name of the photo.
            titleSort (str): Title to use when sorting (defaults to title).
            type (str): 'photo'
            updatedAt (datetime): Datetime the photo was updated.
            userRating (float): Rating of the photo (0.0 - 10.0) equaling (0 stars - 5 stars).
            year (int): Year the photo was taken.
    r   c                 X
   t        j                  | |       t        j                  |j                  j                  d            | _        |j                  j                  d      | _        t        j                  t        |j                  j                  d            | _
        | j                  |t        j                        | _        |j                  j                  d      | _        | j                  |t        j                         | _        t        j                  t        |j                  j                  d            | _        |j                  j                  dd      | _        t        j                  |j                  j                  d            | _        t        j                  t        |j                  j                  d	            | _        |j                  j                  d
      | _        |j                  j                  d      | _        d| _        | j                  |t        j2                        | _        t        j                  |j                  j                  d      d      | _        |j                  j                  d      | _        t        j                  t        |j                  j                  d            | _        |j                  j                  d      | _        t        j                  t        |j                  j                  d            | _        |j                  j                  d      | _        |j                  j                  d      | _         t        j                  t        |j                  j                  d            | _!        |j                  j                  d      | _"        |j                  j                  d      | _#        | j                  |t        jH                        | _%        |j                  j                  d      | _&        |j                  j                  d      | _'        |j                  j                  d| jN                        | _(        |j                  j                  d      | _)        t        j                  |j                  j                  d            | _*        t        j                  tV        |j                  j                  d            | _,        t        j                  t        |j                  j                  d            | _-        y) r   r   createdAtAccuracycreatedAtTZOffsetr   r   r   r    r"   r#   r$   r%   r   originallyAvailableAtz%Y-%m-%d
parentGuidparentIndex	parentKeyparentRatingKeyparentThumbparentTitler&   sourcer'   r(   r)   r*   r+   r,   r-   yearN).r   r?   r   r.   r/   r0   r   r   r6   r7   r   r1   r   r2   r3   r   r4   r5   r   r   r"   r#   r$   r%   r9   Mediar   r   r   r   r   r   r   r&   	sourceURIr'   Tagtagsr(   r)   r*   r+   r,   r:   r-   r   r;   s     r>   r?   zPhoto._loadData   s.   4&''	(BC!%1D!E!&CAT1U!VnnT5;;7KKOOF+	nnT5;;7ZZT[[__W%=>
;;??5"- ++DKKOOM,JK %

3@R0S T!%1D!E#';;??3H#I ^^D%++6
%*%5%5dkkooF]6^`j%k"++//,7 ::c4;;??=+IJ5$zz#t{{?P/QR;;??=9;;??=9C)EF2{{y1NN43	[[__W-
[[__W-
djjAKKOOF+	))$++//+*FG**UDKKOOL,IJJJsDKKOOF$;<	r@   c                 h    | j                   r| j                    d| j                   S | j                  S )z) Returns a filename for use in download. z - )r   r)   r<   s    r>   _prettyfilenamezPhoto._prettyfilename   s1    &&'s4::,77zzr@   c                 8    | j                  | j                        S )z8 Return the photo's :class:`~plexapi.photo.Photoalbum`. )rD   r   r   s    r>   r   zPhoto.photoalbum   s    ~~dnn--r@   c                     t        | d      r/| j                  j                  j                  | j                        S | j
                  r=| j                  j                  j                  | j                         j                        S t        d      )zK Returns the :class:`~plexapi.library.LibrarySection` the item belongs to. r#   z<Unable to get section for photo, can't find librarySectionID)hasattrrh   librarysectionByIDr#   r   r   r   r   s    r>   sectionzPhoto.section   si    4+,<<''33D4I4IJJ^^<<''33DOO4E4V4VWW[\\r@   c                     | j                   D cg c]"  }|j                  D ]  }|s|j                   $ c}}S c c}}w )z This does not exist in plex xml response but is added to have a common
            interface to get the locations of the photo.

            Returns:
                List<str> of file paths where the photo is found on disk.
        )r   partsfile)r<   itemparts      r>   	locationszPhoto.locations  s2     '+jjPdTZZPT4		P	PPPs   ;;Nc                 *   ddl m}m}m} | j                  j                         }	 || j                  d      }
|r|n| j                  |
_        | j                  |
_        | j                  |
_	        | j                  |
_        | j                  j                  |
_        | j                         }d|j                   dt        | j                          |
_        |j%                  |      |
_        |j)                  |      |
_        |	j-                  |
||      S )a   Add current photo as sync item for specified device.
            See :func:`~plexapi.myplex.MyPlexAccount.sync` for possible exceptions.

            Parameters:
                resolution (str): maximum allowed resolution for synchronized photos, see PHOTO_QUALITY_* values in the
                                  module :mod:`~plexapi.sync`.
                client (:class:`~plexapi.myplex.MyPlexDevice`): sync destination, see
                                                               :func:`~plexapi.myplex.MyPlexAccount.sync`.
                clientId (str): sync destination, see :func:`~plexapi.myplex.MyPlexAccount.sync`.
                limit (int): maximum count of items to sync, unlimited if `None`.
                title (str): descriptive title for the new :class:`~plexapi.sync.SyncItem`, if empty the value would be
                             generated from metadata of current photo.

            Returns:
                :class:`~plexapi.sync.SyncItem`: an instance of created syncItem.
        r   )SyncItemPolicyMediaSettingsNz
library://z/item/)clientclientId)plexapi.syncr   r   r   rh   myPlexAccountr)   	rootTitler9   contentTypeMETADATA_TYPEmetadataTypemachineIdentifierr   uuidr   r   locationcreatepolicycreatePhotomediaSettingssync)r<   
resolutionr   r   limitr)   r   r   r   myplex	sync_itemr   s               r>   r   z
Photo.sync  s    $ 	A@++-T\\40	#(%djj	"jj	 $	!%!3!3	&*ll&D&D	#,,.)',,vj>R=ST	!==/	"/";";J"G	{{9Vh{GGr@   c                 T    | j                   j                  |d| j                  d      S rb   )rh   ri   r   rj   s     r>   rk   zPhoto._getWebURL/  s%    ||((dYDNNcd(eer@   c                     t        j                  | j                        }t        t	        d      dz  |d   z  |dd  dz        S rm   )r   rp   r   rq   r   rr   s     r>   rt   zPhoto.metadataDirectory3  sG     NN4??3	4
#h.1=9QR=/QX@YYZZr@   )NNNNru   )rv   rw   rx   ry   rz   r{   r   r?   r   r   r   r}   r   r   rk   rt   r~   r@   r>   rM   rM      sh    &N CDM"=H.] Q Q"HHf [ [r@   rM   c                       e Zd ZdZdZd Zy)PhotoSessionzh Represents a single Photo session
        loaded from :func:`~plexapi.server.PlexServer.sessions`.
    Tc                 \    t         j                  | |       t        j                  | |       y)r   N)rM   r?   r
   r;   s     r>   r?   zPhotoSession._loadDataA  s     d#dD)r@   N)rv   rw   rx   ry   _SESSIONTYPEr?   r~   r@   r>   r   r   :  s     L*r@   r   )rX   pathlibr   urllib.parser   plexapir   r   r   plexapi.baser   r	   r
   plexapi.exceptionsr   plexapi.mixinsr   r   r   r   r   r   r   registerPlexObjectr   rM   r   r~   r@   r>   <module>r      s    	  # ' ' A A )   D[k	D[ D[N ^[x	^[ ^[B 	*; 	* 	*r@   