
    g̿                     
   d dl Z d dlmZmZmZmZmZmZmZ d dl	Z	d dl
mZ d dlmZmZmZ d dlmZ d dlmZ d dlmZmZmZmZ d dlmZmZmZmZ erd d	lmZ  ed
d      Z  edd      Z! e"       Z#ddhZ$i dd dd dd dd dd dd dd dd  d!d" d#d$ d%d& d'd( d)d* d+d, d-d. d/d0 d1d2 Z% G d3 d      Z& G d4 d5e&      Z' G d6 d7      Z( G d8 d9e)      Z* G d: d;e)      Z+ G d< dee    ee    e&      Z,y)=    N)TYPE_CHECKINGGenericIterableListOptionalTypeVarUnion)cached_property)	parse_qsl	urlencodeurlparse)ElementTree)Element)CONFIGX_PLEX_CONTAINER_SIZElogutils)
BadRequestNotFoundUnknownTypeUnsupported)
PlexServerPlexObjectT
PlexObject)boundMediaContainerTMediaContainerkey	sourceURIexactc                     | |k(  S N vqs     !/opt/Tautulli/lib/plexapi/base.py<lambda>r(      s
    !q&     iexactc                 D    | j                         |j                         k(  S r"   lowerr$   s     r'   r(   r(      s    1779	1 r)   containsc                 
    || v S r"   r#   r$   s     r'   r(   r(      s
    Q!V r)   	icontainsc                 B    |j                         | j                         v S r"   r,   r$   s     r'   r(   r(      s    aggi17794 r)   nec                     | |k7  S r"   r#   r$   s     r'   r(   r(      s
    qAv r)   inc                 
    | |v S r"   r#   r$   s     r'   r(   r(      s
    qAv r)   gtc                     | |kD  S r"   r#   r$   s     r'   r(   r(      
    q1u r)   gtec                     | |k\  S r"   r#   r$   s     r'   r(   r(      
    Q r)   ltc                     | |k  S r"   r#   r$   s     r'   r(   r(      r8   r)   ltec                     | |k  S r"   r#   r$   s     r'   r(   r(      r;   r)   
startswithc                 $    | j                  |      S r"   )r@   r$   s     r'   r(   r(       s    q||A r)   istartswithc                 \    | j                         j                  |j                               S r"   )r-   r@   r$   s     r'   r(   r(   !   s    	 4 4QWWY ? r)   endswithc                 $    | j                  |      S r"   )rD   r$   s     r'   r(   r(   "   s    QZZ] r)   	iendswithc                 \    | j                         j                  |j                               S r"   )r-   rD   r$   s     r'   r(   r(   #   s    aggi00; r)   existsc                     |r| d uS | d u S r"   r#   r$   s     r'   r(   r(   $   s    A1D= 19 r)   regexc                 @    t        t        j                  ||             S r"   )boolresearchr$   s     r'   r(   r(   %   s    $ryyA/ r)   iregexc                 `    t        t        j                  || t        j                              S )N)flags)rL   rM   rN   
IGNORECASEr$   s     r'   r(   r(   &   s    4		!Qbmm DE r)   c                       e Zd ZdZdZdZdZddZd Zd Z	d Z
ddZddZd	 Zd
 ZddZ	 	 	 	 	 ddZddZddZddZd ZddZddZddZd Zd ZddZd Zd Zed        Zy)r   a   Base class for all Plex objects.

        Parameters:
            server (:class:`~plexapi.server.PlexServer`): PlexServer this client is connected to (optional)
            data (ElementTree): Response from PlexServer used to build this object (optional).
            initpath (str): Relative path requested when retrieving specified `data` (optional).
            parent (:class:`~plexapi.base.PlexObject`): The parent object that this object is built from (optional).
    Nc                 @   || _         || _        |xs | j                  | _        |t	        j
                  |      nd | _        d | _        d| _        t        j                  ddt              | _        d | _        || j                  |       | j                         | _        y )NTzplexapi.autoreload)_server_datar   	_initpathweakrefref_parent_details_key_overwriteNoner   getrL   _autoReload_edits	_loadData_buildDetailsKey)selfserverdatainitpathparents        r'   __init__zPlexObject.__init__7   s    
!-TXX.4.@w{{6*d  #!::&:D$GNN4  113r)   c                    | j                  | j                  ddddddd            }| j                  | j                  dd	d
ddd            }ddj                  | j                  j                  ||fD cg c]  }|s|	 c}       dS c c}w )N_baseurl	ratingKeyidr   playQueueIDuritypetitlenameusernameproducttagvalue<:>)_clean	firstAttrjoin	__class____name__)rb   uidrp   ps       r'   __repr__zPlexObject.__repr__I   s    kk$..[$}^ceklm{{4>>'6:yRWY`ab388)@)@#t(LR1PQQRSTTUVVRs   3B	
;B	
c                     | j                   j                  d      }|!|j                  d      s|| j                   vs|r|| j                   |<   y y )Nr\   _)__dict__r]   r@   )rb   attrrt   overwriteNones       r'   __setattr__zPlexObject.__setattr__N   sK    ))*:; 4DMM8QUb"'DMM$ Vcr)   c                     |rft        |      j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      dd	 S y)
z+ Clean attr value for display in __repr__. /library/metadata/ z	/childrenz
/accounts/z	/devices/ -N   )strreplace)rb   rt   s     r'   rx   zPlexObject._cleanT   si    J&&';R@EMM+r2EMM,3EMM+r2E==c*3B// r)   c           	         |xs | j                   }| || j                  |||       S |j                  j                  d|j                  j                  d|j                  j                  d                  }|r|j                   d| n|j                  }|dk(  r| d}n|j                  d      r| d	}t        j                  ||j                  
      }| || j                  |||       S t        d|j                   d| d      )zD Factory function to build objects based on registered PLEXOBJECTS. )rf   
streamTypetagTypern   .z/status/sessionsz.sessionz/status/sessions/historyz.history)defaultzUnknown library type <z type='z'../>)	rW   rU   attribr]   rs   r@   r   getPlexObjectr   )rb   elemclsre   etypeehasheclss          r'   
_buildItemzPlexObject._buildItem]   s     -t~~?t||T8DAAdkkooiY_I`.ab).488*AeW%DHH))gX&E  !;<gX&E""5$((;dHTBB2488*GE7%PQQr)   c                 H    	 | j                  |||      S # t        $ r Y yw xY w)zt Calls :func:`~plexapi.base.PlexObject._buildItem` but returns
            None if elem is an unknown type.
        N)r   r   )rb   r   r   re   s       r'   _buildItemOrNonezPlexObject._buildItemOrNonep   s,    	??4h77 		s    	!!c                    | j                   }i }|rPt        | d      rD| j                  j                         D ]'  \  }}|j	                  ||      }|dvs|du rdn|||<   ) |rNt        | d      rB| j
                  j                         D ]%  \  }}|j	                  |d      }||du rdn|||<   ' |r(|dt        t        |j                                     z   z  }|S )z Builds the details key with the XML include parameters.
            All parameters are included by default with the option to override each parameter
            or disable each parameter individually by setting it to False or 0.
        	_INCLUDES)Fr   0T   	_EXCLUDESN?)r   hasattrr   itemspopr   r   sorted)rb   kwargsdetails_keyparamskr%   rt   s          r'   ra   zPlexObject._buildDetailsKeyy   s    
 hh745,,. >1

1a(/%*d]F1I>
 745,,. >1

1d+$%*d]F1I>
 36&,,.+A!BBBKr)   c                     | }|rK|j                   ?|j                         }|r |j                  |j                  fi |ry|r|j                   ?y)aj   Returns True if this object is a child of the given attributes.
            This will search the parent objects all the way to the top.

            Parameters:
                **kwargs (dict): The attributes and values to search for in the parent objects.
                    See all possible `**kwargs*` in :func:`~plexapi.base.PlexObject.fetchItem`.
        TF)rZ   _checkAttrsrV   )rb   r   objs      r'   
_isChildOfzPlexObject._isChildOf   sO     ckk-++-Cssyy;F; ckk- r)   c                 P    t        j                  |      }| j                  ||      S )a   Manually load an XML string as a :class:`~plexapi.base.PlexObject`.

            Parameters:
                xml (str): The XML string to load.
                cls (:class:`~plexapi.base.PlexObject`): If you know the class of the
                    items to be fetched, passing this in will help the parser ensure
                    it only returns those items. By default we convert the xml elements
                    with the best guess PlexObjects based on tag and type attrs.
        )r   
fromstringr   )rb   xmlr   r   s       r'   _manuallyLoadXMLzPlexObject._manuallyLoadXML   s'     %%c*$$T3//r)   c                    |t        d      t        |t              r-t        d |D              rddj	                  d |D               }|xs d}|xs t
        }|}|t        ||      }t        |   | j                  t        d      |      }	i }
	 t        |      |
d	<   t        |      |
d
<   | j                  j                  ||
|      } | j                  |||fi |}t        j                  t        |j                   j#                  d      xs |j                   j#                  d            xs t%        |      }|s||kD  rt'        j(                  d       t        j                  t        |j                   j#                  d            }|r|D ]	  }||_         |	j-                  |       ||z  }||kD  r	 |	S ||z
  }|$t        ||      }t        ||t%        |	      z
        }|t%        |	      k  r	 |	S g)a   Load the specified key to find and build all items with the specified tag
            and attrs.

            Parameters:
                ekey (str or List<int>): API URL path in Plex to fetch items from. If a list of ints is passed
                    in, the key will be translated to /library/metadata/<key1,key2,key3>. This allows
                    fetching multiple items only knowing their key-ids.
                cls (:class:`~plexapi.base.PlexObject`): If you know the class of the
                    items to be fetched, passing this in will help the parser ensure
                    it only returns those items. By default we convert the xml elements
                    with the best guess PlexObjects based on tag and type attrs.
                etag (str): Only fetch items with the specified tag.
                container_start (None, int): offset to get a subset of the data
                container_size (None, int): How many items in data
                maxresults (int, optional): Only return the specified number of results.
                params (dict, optional): Any additional params to add to the request.
                **kwargs (dict): Optionally add XML attribute to filter the items.
                    See the details below for more info.

            **Filtering XML Attributes**

            Any XML attribute can be filtered when fetching results. Filtering is done before
            the Python objects are built to help keep things speedy. For example, passing in
            ``viewCount=0`` will only return matching items where the view count is ``0``.
            Note that case matters when specifying attributes. Attributes further down in the XML
            tree can be filtered by *prepending* the attribute with each element tag ``Tag__``.

            Examples:

                .. code-block:: python

                    fetchItem(ekey, viewCount=0)
                    fetchItem(ekey, contentRating="PG")
                    fetchItem(ekey, Genre__tag="Animation")
                    fetchItem(ekey, Media__videoCodec="h265")
                    fetchItem(ekey, Media__Part__container="mp4)

            Note that because some attribute names are already used as arguments to this
            function, such as ``tag``, you may still reference the attr tag by prepending an
            underscore. For example, passing in ``_tag='foobar'`` will return all items where
            ``tag='foobar'``.

            **Using PlexAPI Operators**

            Optionally, PlexAPI operators can be specified by *appending* it to the end of the
            attribute for more complex lookups. For example, passing in ``viewCount__gte=0``
            will return all items where ``viewCount >= 0``.

            List of Available Operators:

            * ``__contains``: Value contains specified arg.
            * ``__endswith``: Value ends with specified arg.
            * ``__exact``: Value matches specified arg.
            * ``__exists`` (*bool*): Value is or is not present in the attrs.
            * ``__gt``: Value is greater than specified arg.
            * ``__gte``: Value is greater than or equal to specified arg.
            * ``__icontains``: Case insensitive value contains specified arg.
            * ``__iendswith``: Case insensitive value ends with specified arg.
            * ``__iexact``: Case insensitive value matches specified arg.
            * ``__in``: Value is in a specified list or tuple.
            * ``__iregex``: Case insensitive value matches the specified regular expression.
            * ``__istartswith``: Case insensitive value starts with specified arg.
            * ``__lt``: Value is less than specified arg.
            * ``__lte``: Value is less than or equal to specified arg.
            * ``__regex``: Value matches the specified regular expression.
            * ``__startswith``: Value starts with specified arg.

            Examples:

                .. code-block:: python

                    fetchItem(ekey, viewCount__gte=0)
                    fetchItem(ekey, Media__container__in=["mp4", "mkv"])
                    fetchItem(ekey, guid__regex=r"com\.plexapp\.agents\.(imdb|themoviedb)://|tt\d+")
                    fetchItem(ekey, guid__id__regex=r"(imdb|tmdb|tvdb)://")
                    fetchItem(ekey, Media__Part__file__startswith="D:\Movies")

        zekey was not providedc              3   <   K   | ]  }t        |t                y wr"   )
isinstanceint.0r   s     r'   	<genexpr>z(PlexObject.fetchItems.<locals>.<genexpr>  s     )O3*S#*>)O   r   ,c              3   2   K   | ]  }t        |        y wr"   )r   r   s     r'   r   z(PlexObject.fetchItems.<locals>.<genexpr>	  s     0JcS0Js   r   r   re   zX-Plex-Container-StartzX-Plex-Container-Size)headersr   	totalSizesizez3container_start is greater than the number of itemslibrarySectionID)r   r   listallrz   r   minr   rU   r   r   query	findItemsr   castr   r   r]   lenr   infor   extend)rb   ekeyr   container_startcontainer_size
maxresultsr   r   offsetresultsr   rd   
subresults
total_sizer   itemwanted_number_of_itemss                    r'   
fetchItemszPlexObject.fetchItems   s   p <455dD!c)O$)O&O'0JT0J(J'KLD).Q'@+@ ! <N %dllG<L4MX\]03O0DG,-/2>/BG+,<<%%dGF%KD'c4B6BJC)E)`Y_I`atehisetJJ&HHRS$zz#t{{?Q/RS& =D,<D)= NN:&~-O+  &0&%8"%),Z9O)P&!$^5KcRYl5Z![%W5A r)   c                     t        |t              rd| }	  | j                  ||fi |d   S # t        $ r# |r|j                  nd}t        d| d|       dw xY w)aD   Load the specified key to find and build the first item with the
            specified tag and attrs. If no tag or attrs are specified then
            the first item in the result set is returned.

            Parameters:
                ekey (str or int): Path in Plex to fetch items from. If an int is passed
                    in, the key will be translated to /library/metadata/<key>. This allows
                    fetching an item only knowing its key-id.
                cls (:class:`~plexapi.base.PlexObject`): If you know the class of the
                    items to be fetched, passing this in will help the parser ensure
                    it only returns those items. By default we convert the xml elements
                    with the best guess PlexObjects based on tag and type attrs.
                etag (str): Only fetch items with the specified tag.
                **kwargs (dict): Optionally add XML attribute to filter the items.
                    See :func:`~plexapi.base.PlexObject.fetchItems` for more details
                    on how this is used.
        r   r   NonezUnable to find elem: cls=z, attrs=N)r   r   r   
IndexErrorr|   r   )rb   r   r   r   clsnames        r'   	fetchItemzPlexObject.fetchItem7  sw    $ dC 'v.D	\"4??477:: 	\&)cllvG6wixxPQW[[	\s	   . ,Ac                    |r|j                   rd|vr|j                   |d<   |r|j                  rd|vr|j                  |d<   |r)t        t        j                  ||      t        d            }|j                  dk(  rt        |   | j                  ||      ng }|D ]=  } | j                  |fi |s| j                  |||      }|-|j                  |       ? |S )z Load the specified data to find and build all items with the specified tag
            and attrs. See :func:`~plexapi.base.PlexObject.fetchItem` for more details
            on how this is used.
        rs   etagrn   Emptyr   r   )TAGTYPEnextr   
iterXMLBFSr   rs   r   rU   r   r   append)	rb   rd   r   re   rtagr   r   r   r   s	            r'   r   zPlexObject.findItemsR  s     377uF2 WWF6N388f 4 XXF6N((t4gg6FGDNRhhZjNjs#DLL$Jpr 	'Dt//,,T3A#LL&		'
 r)   c                 T    	  | j                   ||||fi |d   S # t        $ r Y yw xY w)z Load the specified data to find and build the first items with the specified tag
            and attrs. See :func:`~plexapi.base.PlexObject.fetchItem` for more details
            on how this is used.
        r   N)r   r   )rb   rd   r   re   r   r   s         r'   findItemzPlexObject.findItemh  s:    
	!4>>$XtFvFqII 		s    	''c                 8    |D ]  }t        | |d      }||c S  y)z7 Return the first attribute in attrs that is not None. N)getattr)rb   attrsr   rt   s       r'   ry   zPlexObject.firstAttrr  s*     	DD$-E 	r)   c                     g }|r t        t        j                  ||      g       }|D ]H  }d|| d<    | j                  |fi |s|j	                  |j
                  j                  |             J |S )z2 Return a list of values from matching attribute. T__exists)r   r   r   r   r   r   r]   )rb   rd   r   r   r   r   r   s          r'   	listAttrszPlexObject.listAttrsy  sx    ((t4b9D 	6D(,FdV8$%t//t{{t45	6 r)   c                 *     | j                   dd|i|S )a   Reload the data for this object from self.key.

            Parameters:
                key (string, optional): Override the key to reload.
                **kwargs (dict): A dictionary of XML include parameters to include/exclude or override.
                    See :class:`~plexapi.base.PlexPartialObject` for all the available include parameters.
                    Set parameter to True to include and False to exclude.

            Example:

                .. code-block:: python

                    from plexapi.server import PlexServer
                    plex = PlexServer('http://localhost:32400', token='xxxxxxxxxxxxxxxxxxxx')

                    # Search results are partial objects.
                    movie = plex.library.section('Movies').get('Cars')
                    movie.isPartialObject()  # Returns True

                    # Partial reload of the movie without a default include parameter.
                    # The movie object will remain as a partial object.
                    movie.reload(includeMarkers=False)
                    movie.isPartialObject()  # Returns True

                    # Full reload of the movie with all default include parameters.
                    # The movie object will be a full object.
                    movie.reload()
                    movie.isFullObject()  # Returns True

                    # Full reload of the movie with all default and extra include parameter.
                    # Including `checkFiles` will tell the Plex server to check if the file
                    # still exists and is accessible.
                    # The movie object will be a full object.
                    movie.reload(checkFiles=True)
                    movie.isFullObject()  # Returns True

        r   r#   _reload)rb   r   r   s      r'   reloadzPlexObject.reload  s    L t||..v..r)   c                    |r | j                   di |n| j                  }|xs |xs | j                  }|st        d      || _        | j
                  j                  |      }|| _        | j                  |d          d| _        | S ) Perform the actual reload. z-Cannot reload an object not built from a URL.r   Tr#   )	ra   r[   r   r   rW   rU   r   r\   r`   )rb   r   r\   r   r   rd   s         r'   r   zPlexObject._reload  s    9?+d++5f5TEVEV,[,DHHMNN||!!#&,tAw"r)   c                 .   i }|j                         D ]h  \  }}| j                  |      \  }}}| j                  ||      }|dk(  r|s|dv r yd||<   |D ]%  }	| j                  |||	      }	 ||	|      s d||<    h j t	        |j                               S )Nr    )Nr   r   TF)r   _getAttrOperator_getAttrValue_castAttrValuer   values)
rb   r   r   
attrsFoundr   r   opoperatorr   rt   s
             r'   r   zPlexObject._checkAttrs  s    
!<<> 	KD%!%!6!6t!<D"h''d3FW}V0F$Jt ++Bu=E5)'+Jt$		 :$$&''r)   c                     t         j                         D ]5  \  }}|j                  d|       s|j                  dd      d   }|||fc S  |dt         d   fS )N__r   r   r    )	OPERATORSr   rD   rsplit)rb   r   r   r   s       r'   r   zPlexObject._getAttrOperator  sd    %OO- 	*LB}}r"Y'{{4+A.R))	*
 Wi000r)   c                   	 |j                  dd      }|d   	t        |      dk(  r|d   nd }|r?|g n|}	fd|D        D ]  }|| j                  |||      z  } |D cg c]  }||	 c}S 	j                         dk(  r|j                  gS |j
                  j                         D ]+  \  }}	j                         |j                         k(  s(|gc S  g S c c}w )Nr   r   r      c              3   z   K   | ]2  }|j                   j                         j                         k(  s/| 4 y wr"   )rs   r-   )r   cr   s     r'   r   z+PlexObject._getAttrValue.<locals>.<genexpr>  s(     KQUU[[]djjl-J!Ks   0;;r   )splitr   r   r-   rs   r   r   )
rb   r   attrstrr   partschildr_attrrt   r   s
            @r'   r   zPlexObject._getAttrValue  s    dA&Qx!%jAo%(4#ObGKTK G4--eWgFFG&8!!-A88::<6!HH: KK--/ 	LE5zz|u{{},w	 	 9s    C(Cc                    |dk(  r|S t        |t              rt        t        |            S t        |t              rd|v rt        |      S t        |t              rt        |      S t        |t              rt        |      S |S )NrH   r   )r   rL   r   float)rb   r   r   rt   s       r'   r   zPlexObject._castAttrValue  sn    >LeT"E
##eS!cUl<eS!u:eU#<r)   c                     t        d      )Nz Abstract method not implemented.NotImplementedErrorrb   rd   s     r'   r`   zPlexObject._loadData  s    !"DEEr)   c                     | j                   S r"   )r   rb   s    r'   _searchTypezPlexObject._searchType  s    yyr)   NNr"   )NNNNN)NNN)NT)r|   
__module____qualname____doc__r   r   r   rg   r   r   rx   r   r   ra   r   r   r   r   r   r   ry   r   r   r   r   r   r   r   r`   propertyr  r#   r)   r'   r   r   *   s     CD
C4$W
(0R&00  HT\6,
&/P($1&F  r)   c                       e Zd ZdZi ddddddddddd	dd
ddddddddddddddddddddddddZddddZd Zd Zd Z fdZ	d Z
d Zd  Zd!efd"Zd# Zd$ Zd% Zd& Zd' Zd( Zd) Zd.d*Zd/d+Zd/d,Zd- Z xZS )0PlexPartialObjecta#   Not all objects in the Plex listings return the complete list of elements
        for the object. This object will allow you to assume each object is complete,
        and if the specified value you request is None it will fetch the full object
        automatically and update itself.
    
checkFilesr   includeAllConcertsincludeBandwidthsr   includeChaptersincludeChildrenincludeConcertsincludeExternalMediaincludeExtrasincludeFieldszthumbBlurHash,artBlurHashincludeGeolocationincludeLoudnessRampsincludeMarkersincludeOnDeckincludePopularLeavesincludePreferencesincludeRelatedincludeRelatedCount)includeReviewsincludeStationszbMedia,Genre,Country,Guid,Rating,Collection,Director,Writer,Role,Producer,Similar,Style,Mood,Formatzsummary,tagline)excludeElementsexcludeFieldsskipRefreshc                 `    t        |t              r| j                  |j                  k(  S t        S r"   )r   r  r   NotImplemented)rb   others     r'   __eq__zPlexPartialObject.__eq__  s&    e./88uyy((r)   c                 *    t        t        |             S r"   )hashreprr  s    r'   __hash__zPlexPartialObject.__hash__$  s    DJr)   c              #      K   |  y wr"   r#   r  s    r'   __iter__zPlexPartialObject.__iter__'  s     
s   c                 "   t         t        |   |      }|t        v r|S |t        v r|S |j                  d      r|S |d g fvr|S | j                         r|S t        | t        t        f      r|S | j                  du r|S | j                  j                  }| j                  j                  d| j                  j                  d            }|r| d| dn|}t        j                   d||       | j#                  d       t         t        |   |      S )	Nr   Fro   rp   z ''zReloading %s for attr '%s')r\   )superr  __getattribute___DONT_RELOAD_FOR_KEYSUSER_DONT_RELOAD_FOR_KEYSr@   isFullObjectr   PlexSessionPlexHistoryr^   r{   r|   r   r]   r   debugr   )rb   r   rt   r   ro   objnamer{   s         r'   r;  z"PlexPartialObject.__getattribute__*  s    '?E((,,,Ul??3r
"5Lud[+67u$Ul..))!!'4==+<+<V+DE,1WIRwa(w		.>E*&>tDDr)   c                     d| j                   j                  d       d}| j                  j                  || j                  j                  j
                         y)u   Tell Plex Media Server to performs analysis on it this item to gather
            information. Analysis includes:

            * Gather Media Properties: All of the media you add to a Library has
                properties that are useful to know–whether it's a video file, a
                music track, or one of your photos (container, codec, resolution, etc).
            * Generate Default Artwork: Artwork will automatically be grabbed from a
                video file. A background image will be pulled out as well as a
                smaller image to be used for poster/thumbnail type purposes.
            * Generate Video Preview Thumbnails: Video preview thumbnails are created,
                if you have that feature enabled. Video preview thumbnails allow
                graphical seeking in some Apps. It's also used in the Plex Web App Now
                Playing screen to show a graphical representation of where playback
                is. Video preview thumbnails creation is a CPU-intensive process akin
                to transcoding the file.
            * Generate intro video markers: Detects show intros, exposing the
                'Skip Intro' button in clients.
        /z/analyzemethodN)r   lstriprU   r   _sessionputrb   r   s     r'   analyzezPlexPartialObject.analyze>  sG    & $((//#&'x03t||'<'<'@'@Ar)   c                 H   t        | j                  xs | j                        }t        | j                        }t	        t        |j                              }t	        t        |j                              }| j                   xs  |j                  |j                  k(  xr ||k  S )aN   Returns True if this is already a full object. A full object means all attributes
            were populated from the api path representing only this item. For example, the
            search result for a movie often only contain a portion of the attributes a full
            object (main url) for that movie would contain.
        )r   r[   r   rW   setr   r   path)rb   
parsed_keyparsed_initpath	query_key
query_inits        r'   r>  zPlexPartialObject.isFullObjectT  s     d//;488<
"4>>2	*"2"234	?#8#89:
88|d
?3G3G G cIYcLcdr)   c                 $    | j                          S )z, Returns True if this is not a full object. )r>  r  s    r'   isPartialObjectz!PlexPartialObject.isPartialObject`  s    $$&&&r)   fieldc                 B    t        fd| j                  D        d      S )z Returns True if the specified field is locked, otherwise False.

            Parameters:
                field (str): The name of the field.
        c              3   V   K   | ]   }|j                   k(  s|j                   " y wr"   )rp   locked)r   frU  s     r'   r   z-PlexPartialObject.isLocked.<locals>.<genexpr>j  s     F!affoQXXFs   ))F)r   fields)rb   rU  s    `r'   isLockedzPlexPartialObject.isLockedd  s     Ft{{FNNr)   c                    t        | j                  t              r| j                  j                  |       | S d|vr"t	        j
                  | j                        |d<    | j                         j                  dd| i| | S )z Actually edit an object. rn   r   r#   )	r   r_   dictupdater   
searchTyper  section_editrb   r   s     r'   ra  zPlexPartialObject._editl  sl    dkk4(KKv&K"--d.>.>?F6N24262r)   c                 &     | j                   di |S )a   Edit an object.
            Note: This is a low level method and you need to know all the field/tag keys.
            See :class:`~plexapi.mixins.EditFieldMixin` and :class:`~plexapi.mixins.EditTagsMixin`
            for individual field and tag editing methods.

            Parameters:
                kwargs (dict): Dict of settings to edit.

            Example:

                .. code-block:: python

                    edits = {
                        'type': 1,
                        'id': movie.ratingKey,
                        'title.value': 'A new title',
                        'title.locked': 1,
                        'summary.value': 'This is a summary.',
                        'summary.locked': 1,
                        'collection[0].tag.tag': 'A tag',
                        'collection.locked': 1}
                    }
                    movie.edit(**edits)

        r#   )ra  rb  s     r'   editzPlexPartialObject.editx  s    4 tzz#F##r)   c                     i | _         | S )a   Enable batch editing mode to save API calls.
            Must call :func:`~plexapi.base.PlexPartialObject.saveEdits` at the end to save all the edits.
            See :class:`~plexapi.mixins.EditFieldMixin` and :class:`~plexapi.mixins.EditTagsMixin`
            for individual field and tag editing methods.

            Example:

                .. code-block:: python

                    # Batch editing multiple fields and tags in a single API call
                    Movie.batchEdits()
                    Movie.editTitle('A New Title').editSummary('A new summary').editTagline('A new tagline') \
                        .addCollection('New Collection').removeGenre('Action').addLabel('Favorite')
                    Movie.saveEdits()

        )r_   r  s    r'   
batchEditszPlexPartialObject.batchEdits  s    " r)   c                     t        | j                  t              st        d      | j                  }d| _         | j                  di | | S )z Save all the batch edits. The object needs to be reloaded manually,
            if required.
            See :func:`~plexapi.base.PlexPartialObject.batchEdits` for details.
        z?Batch editing mode not enabled. Must call `batchEdits()` first.Nr#   )r   r_   r]  r   ra  )rb   editss     r'   	saveEditszPlexPartialObject.saveEdits  sD    
 $++t,^__

Ur)   c                     | j                    d}| j                  j                  || j                  j                  j                         y)ad   Refreshing a Library or individual item causes the metadata for the item to be
            refreshed, even if it already has metadata. You can think of refreshing as
            "update metadata for the requested item even if it already has some". You should
            refresh a Library or individual item if:

            * You've changed the Library Metadata Agent.
            * You've added "Local Media Assets" (such as artwork, theme music, external
                subtitle files, etc.)
            * You want to freshen the item posters, summary, etc.
            * There's a problem with the poster image that's been downloaded.
            * Items are missing posters or other downloaded information. This is possible if
                the refresh process is interrupted (the Server is turned off, internet
                connection dies, etc).
        z/refreshrE  N)r   rU   r   rH  rI  rJ  s     r'   refreshzPlexPartialObject.refresh  s;     
(#3t||'<'<'@'@Ar)   c                 `    | j                   j                  j                  | j                        S )zL Returns the :class:`~plexapi.library.LibrarySection` this item belongs to. )rU   librarysectionByIDr   r  s    r'   r`  zPlexPartialObject.section  s#    ||##//0E0EFFr)   c                     	 | j                   j                  | j                  | j                   j                  j                        S # t
        $ r" t        j                  d| j                          w xY w)za Delete a media element. This has to be enabled under settings > server > library in plex webui. rE  zSFailed to delete %s. This could be because you have not allowed items to be deleted)rU   r   r   rH  deleter   r   errorr  s    r'   rp  zPlexPartialObject.delete  s`    	<<%%dhht||7L7L7S7S%TT 	II 78<B	s   AA +A2c                 R    | j                   j                  ||| j                        S )z Get Play History for a media item.

            Parameters:
                maxresults (int): Only return the specified number of results (optional).
                mindate (datetime): Min datetime to return results from.
        )r   mindaterj   )rU   historyrj   )rb   r   rs  s      r'   rt  zPlexPartialObject.history  s'     ||##z7VZVdVd#eer)   c                 R    | j                   j                  |d| j                        S )z Get the Plex Web URL with the correct parameters.
            Private method to allow overriding parameters from subclasses.
        details)baseendpointr   )rU   _buildWebURLr   rb   rw  s     r'   
_getWebURLzPlexPartialObject._getWebURL  s$     ||((dYDHH(UUr)   c                 &    | j                  |      S )z Returns the Plex Web URL for a media item.

            Parameters:
                base (str): The base URL before the fragment (``#!``).
                    Default is https://app.plex.tv/desktop.
        )rw  )r{  rz  s     r'   	getWebURLzPlexPartialObject.getWebURL  s     D))r)   c                 P    ddl m}  |j                  | j                  | g|i |S )z Returns a new :class:`~plexapi.playqueue.PlayQueue` from this media item.
            See :func:`~plexapi.playqueue.PlayQueue.create` for available parameters.
        r   )	PlayQueue)plexapi.playqueuer  createrU   )rb   argsr   r  s       r'   	playQueuezPlexPartialObject.playQueue  s*     	0ydDTDVDDr)   r  r"   )r|   r  r  r  r   r   r1  r5  r7  r;  rK  r>  rT  r   r[  ra  rd  rf  ri  rk  r`  rp  rt  r{  r}  r  __classcell__r{   s   @r'   r  r    sh   
aa 	Q 	1	
 	1 	1 	 	 	4 	a 	 	! 	 	 	a  	!!" 	q#$ 'I. q*I
 E(B,
e'Oc O
$8(B$GfV*Er)   r  c                   X    e 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ddZy
)Playablea   This is a general place to store functions specific to media that is Playable.
        Things were getting mixed up a bit when dealing with Shows, Season, Artists,
        Albums which are all not playable.

        Attributes:
            playlistItemID (int): Playlist item ID (only populated for :class:`~plexapi.playlist.Playlist` items).
            playQueueItemID (int): PlayQueue item ID (only populated for :class:`~plexapi.playlist.PlayQueue` items).
    c                     t        j                  t        |j                  j	                  d            | _        t        j                  t        |j                  j	                  d            | _        y )NplaylistItemIDplayQueueItemID)r   r   r   r   r]   r  r  r  s     r'   r`   zPlayable._loadData  sD    #jjdkkoo>N.OP$zz#t{{?P/QRr)   c                    | j                   dvrt        d| j                    d      |j                  dd      }|j                  dd      }|j                  dd      }| j                  |j                  d	d
      |j                  d	d
      ||j                  dd      |j                  dd      |j                  dd
      |rt	        |d      ndt        j                  d|      r|nd|j                  dd      d
}|j                  |       |j                         D ci c]  \  }}|	|| }}}| j                   dv rdnd}|dk(  rdnd}	| j                  j                  d| d|	 dt        |       d      S c c}}w )a   Returns a stream url that may be used by external applications such as VLC.

            Parameters:
                **kwargs (dict): optional parameters to manipulate the playback when accessing
                    the stream. A few known parameters include: maxVideoBitrate, videoResolution
                    offset, copyts, protocol, mediaIndex, partIndex, platform.

            Raises:
                :exc:`~plexapi.exceptions.Unsupported`: When the item doesn't support fetching a stream URL.
        )movieepisodetrackclipzFetching stream URL for z is unsupported.maxVideoBitrateNvideoResolutionr   protocol
mediaIndexr   fastSeekr   copytsr   @   z	^\d+x\d+$platformChrome)
rN  r  	partIndexr  r  r  r   r  r  zX-Plex-Platform)r  albumaudiovideodashmpdm3u8rD  z/:/transcode/universal/start.r   T)includeToken)r   r   r   r   maxrM   matchr^  r   rU   urlr   )
rb   r   mvbvrr  r   r   r%   
streamtypeexts
             r'   getStreamURLzPlayable.getStreamURL  s{    99AA 8CSTUUjj*D1ZZ)2.::j$/ HH **\15L!4 

:q1jj1-jj1-/2s3|%'XXlB%?rT%zz*h?
 	f $*<<>C41aQ]!Q$CC $		-? ?WW
6)ev||
|8Qy?P>QR   
 	
	 Ds   
E<*E<c              #   X   K   | j                   D ]  }|j                  D ]  }|   yw)z- Iterates over the parts of this media item. N)mediar  )rb   r   parts      r'   	iterPartszPlayable.iterParts,  s1     JJ 	D

 
	s   (*c                     | j                         r| j                          t        d | j                         D        g       S )zS Returns a list of :class:`~plexapi.media.videoStream` objects for all MediaParts. c              3   <   K   | ]  }|j                           y wr"   )videoStreamsr   r  s     r'   r   z(Playable.videoStreams.<locals>.<genexpr>6       EDD%%'Er   rT  r   sumr  r  s    r'   r  zPlayable.videoStreams2  1    !KKMEDNN4DErJJr)   c                     | j                         r| j                          t        d | j                         D        g       S )zS Returns a list of :class:`~plexapi.media.AudioStream` objects for all MediaParts. c              3   <   K   | ]  }|j                           y wr"   )audioStreamsr  s     r'   r   z(Playable.audioStreams.<locals>.<genexpr><  r  r   r  r  s    r'   r  zPlayable.audioStreams8  r  r)   c                     | j                         r| j                          t        d | j                         D        g       S )zV Returns a list of :class:`~plexapi.media.SubtitleStream` objects for all MediaParts. c              3   <   K   | ]  }|j                           y wr"   )subtitleStreamsr  s     r'   r   z+Playable.subtitleStreams.<locals>.<genexpr>B  s     HtD((*Hr   r  r  s    r'   r  zPlayable.subtitleStreams>  s1    !KKMHt~~7GH"MMr)   c                     | j                         r| j                          t        d | j                         D        g       S )zS Returns a list of :class:`~plexapi.media.LyricStream` objects for all MediaParts. c              3   <   K   | ]  }|j                           y wr"   )lyricStreamsr  s     r'   r   z(Playable.lyricStreams.<locals>.<genexpr>H  r  r   r  r  s    r'   r  zPlayable.lyricStreamsD  r  r)   c                 &    |j                  |        y)z Start playback on the specified client.

            Parameters:
                client (:class:`~plexapi.client.PlexClient`): Client to start playing on.
        N)	playMedia)rb   clients     r'   playzPlayable.playJ  s     	r)   Nc                    g }| j                         D cg c]  }|s|	 }}|D ]0  }|s3t        j                  | j                          d|j                         }n|j
                  }|rk| j                  j                  |j                               |d<   |j                         j                  j                  |      |d<    | j                  di |}	n(| j                  j                  |j                   d      }	t        j                  |	| j                  j                  ||| j                  j                         }
|
s |j#                  |
       3 |S c c}w )a   Downloads the media item to the specified location. Returns a list of
            filepaths that have been saved to disk.

            Parameters:
                savepath (str): Defaults to current working dir.
                keep_original_name (bool): True to keep the original filename otherwise
                    a friendlier filename is generated. See filenames below.
                **kwargs (dict): Additional options passed into :func:`~plexapi.audio.Track.getStreamURL`
                    to download a transcoded stream, otherwise the media item will be downloaded
                    as-is and saved to disk.

            **Filenames**

            * Movie: ``<title> (<year>)``
            * Episode: ``<show title> - s00e00 - <episode title>``
            * Track: ``<artist title> - <album title> - 00 - <track title>``
            * Photo: ``<photoalbum title> - <photo/clip title>`` or ``<photo/clip title>``
        r   r  r  z?download=1)filenamesavepathsessionr#   )r  r   cleanFilename_prettyfilename	containerfiler  indexrZ   r  r  rU   r  r   download_tokenrH  r   )rb   r  keep_original_namer   	filepathsir  r  r  download_urlfilepaths              r'   r  zPlayable.downloadR  s2   & 	 NN,2q22 	+D% ..$2F2F2H1I4>>JZ/[\99'+zz'7'7'G|$&*lln&:&:&@&@&F{#0t00:6:#||//488*K0HI~~##!!--H   */	+2 7 3s
   EEc                 f    d| j                    d| d| }| j                  j                  |       | S )a   Set the watched progress for this video.

            Note that setting the time to 0 will not work.
            Use :func:`~plexapi.mixins.PlayedUnplayedMixin.markPlayed` or
            :func:`~plexapi.mixins.PlayedUnplayedMixin.markUnplayed` to achieve
            that goal.

            Parameters:
                time (int): milliseconds watched
                state (string): state of the video, default 'stopped'
        z/:/progress?key=-&identifier=com.plexapp.plugins.library&time=&state=)rj   rU   r   )rb   timestater   s       r'   updateProgresszPlayable.updateProgress  s>     ! 00]^b]ccjkpjqr3r)   c           	          d}||t        |      z   }n|t        | j                        z   }d| j                   d| j                   dt	        |       d| | 	}| j
                  j                  |       | S )z Set the timeline progress for this video.

            Parameters:
                time (int): milliseconds watched
                state (string): state of the video, default 'stopped'
                duration (int): duration of the item
        z
&duration=z/:/timeline?ratingKey=z&key=r  r  )r   durationrj   r   r   rU   r   )rb   r  r  r  durationStrr   s         r'   updateTimelinezPlayable.updateTimeline  s     #%H5K%DMM(::K''7uTXXJ G>>A$i[PUwWbVce3r)   )NF)stopped)r  N)r|   r  r  r  r`   r  r  r  r  r  r  r  r  r  r  r#   r)   r'   r  r    sE    S(
TKKNK/b r)   r  c                   B    e Zd ZdZd Zed        Zd Zd	dZd Z	d
dZ
y)r?  a   This is a general place to store functions specific to media that is a Plex Session.

        Attributes:
            live (bool): True if this is a live tv session.
            player (:class:`~plexapi.client.PlexClient`): PlexClient object for the session.
            session (:class:`~plexapi.media.Session`): Session object for the session
                if the session is using bandwidth (None otherwise).
            sessionKey (int): The session key for the session.
            transcodeSession (:class:`~plexapi.media.TranscodeSession`): TranscodeSession object
                if item is being transcoded (None otherwise).
    c                 T   t        j                  t        |j                  j	                  dd            | _        | j                  |d      | _        | j                  |d      | _        t        j                  t        |j                  j	                  d            | _
        | j                  |d      | _        |j                  d      }|j                  j	                  d	      | _        t        j                  t        |j                  j	                  d
            | _        | j                  r| j                  gng | _        | j                  r| j                  gng | _        | j                  r| j                  gng | _        | j                  r| j                  g| _        y g | _        y )Nliver   Player)r   Session
sessionKeyTranscodeSessionUserro   rk   )r   r   rL   r   r]   r  r   playerr  r   r  transcodeSessionfind	_username_userIdplayerssessionstranscodeSessions	usernames)rb   rd   users      r'   r`   zPlexSession._loadData  s#   JJtT[[__VS%AB	mmDxm8}}T	}:**S$++//,*GH $d9K Lyy 1zz#t{{t'<= )-}*.,,B<@<Q<Q$"7"7!8WY-1^^$..)r)   c                     | j                   j                         }| j                  dk(  r|S |j                  | j                        S )z Returns the :class:`~plexapi.myplex.MyPlexAccount` object (for admin)
            or :class:`~plexapi.myplex.MyPlexUser` object (for users) for this session.
        r   )rU   myPlexAccountr  r  r  )rb   r  s     r'   r  zPlexSession.user  s=    
 224<<1  !!$..11r)   c                 "    | j                         S )z Reload the data for the session.
            Note: This will return the object as-is if the session is no longer active.
        r   r  s    r'   r   zPlexSession.reload  s     ||~r)   c                     |r| S | j                   }| j                  j                  |      }|D ]G  }|j                  j	                  d      t        | j                        k(  s5| j                  |        | S  | S )r   r  )rW   rU   r   r   r]   r   r  r`   )rb   r^   r   r   rd   r   s         r'   r   zPlexSession._reload  sq     Knn||!!#& 	D{{|,DOO0DDt$		 r)   c                 8    | j                  | j                        S )z1 Return the source media object for the session. )r   r[   r  s    r'   sourcezPlexSession.source  s    ~~d//00r)   c                 r    | j                   j                  |d}d}| j                  j                  ||      S )z Stop playback for the session.

            Parameters:
                reason (str): Message displayed to the user for stopping playback.
        )	sessionIdreasonz/status/sessions/terminate)r   )r  rk   rU   r   )rb   r  r   r   s       r'   stopzPlexSession.stop  s:     
 +||!!#f!55r)   N)F)r   )r|   r  r  r  r`   r
   r  r   r   r  r  r#   r)   r'   r?  r?    s5    
D" 2 216r)   r?  c                   (    e Zd ZdZd Zd Zd Zd Zy)r@  a   This is a general place to store functions specific to media that is a Plex history item.

        Attributes:
            accountID (int): The associated :class:`~plexapi.server.SystemAccount` ID.
            deviceID (int): The associated :class:`~plexapi.server.SystemDevice` ID.
            historyKey (str): API URL (/status/sessions/history/<historyID>).
            viewedAt (datetime): Datetime item was last watched.
    c                    t        j                  t        |j                  j	                  d            | _        t        j                  t        |j                  j	                  d            | _        |j                  j	                  d      | _        t        j                  |j                  j	                  d            | _	        y )N	accountIDdeviceID
historyKeyviewedAt)
r   r   r   r   r]   r  r  r  
toDatetimer  r  s     r'   r`   zPlexHistory._loadData  ss    C)EF

3
(CD++//,7(()DEr)   c                     t        d      )z( Reload the data for the history entry. zNHistory objects cannot be reloaded. Use source() to get the source media item.r  rb  s     r'   r   zPlexHistory._reload  s    !"rssr)   c                 T    | j                   r| j                  | j                         S dS )z Return the source media object for the history entry
            or None if the media no longer exists on the server.
        N)r[   r   r  s    r'   r  zPlexHistory.source	  s&     594E4Et~~d//0O4Or)   c                     | j                   j                  | j                  | j                   j                  j                        S )z Delete the history entry. rE  )rU   r   r  rH  rp  r  s    r'   rp  zPlexHistory.delete  s0    ||!!$//$,,:O:O:V:V!WWr)   N)r|   r  r  r  r`   r   r  rp  r#   r)   r'   r@  r@    s    FtPXr)   r@  c                        e Zd ZdZd Zddddddededee   d	ee	   d
df fdZ
dedeee   ef   d
df fdZd Z xZS )r   ap   Represents a single MediaContainer.

        Attributes:
            TAG (str): 'MediaContainer'
            allowSync (int): Sync/Download is allowed/disallowed for feature.
            augmentationKey (str): API URL (/library/metadata/augmentations/<augmentationKey>).
            identifier (str): "com.plexapp.plugins.library"
            librarySectionID (int): :class:`~plexapi.library.LibrarySection` ID.
            librarySectionTitle (str): :class:`~plexapi.library.LibrarySection` title.
            librarySectionUUID (str): :class:`~plexapi.library.LibrarySection` UUID.
            mediaTagPrefix (str): "/system/bundle/media/flags/"
            mediaTagVersion (int): Unknown
            offset (int): The offset of current results.
            size (int): The number of items in the hub.
            totalSize (int): The total number of items for the query.

    N)re   rf   rc   r   rd   r  re   rf   returnc                P    t        |   |  t        j                  | ||||       y r"   )r:  rg   r   )rb   rc   rd   re   rf   r  r{   s         r'   rg   zMediaContainer.__init__,  s(     	$D&$&Ar)   rb   _MediaContainer__iterablec           	         | j                   | j                   n
t        |       }t        |   |       t	        |t
              sy |j                  |j                  n| j                  | _        ||j                   |j                   n
t        |      z   | _         | j                  2|j                  &t        | j                  |j                        | _        n)| j                  | j                  n|j                  | _        dD ]?  }t        | |      rt        | |      t        ||      s)t        | |t        ||             A y )N)	allowSyncaugmentationKey
identifierr   librarySectionTitlelibrarySectionUUIDmediaTagPrefixmediaTagVersion)r   r   r:  r   r   r   r   r   r   r   r   setattr)rb   r  	curr_sizer   r{   s       r'   r   zMediaContainer.extend8  s    "&!6DIICI	z"*n5
 ##/    	 ):JOOJ
	
 ;;"z'8'8'Ddkk:+<+<=DK  ${{6J<M<M K
	
 	=C D#&GD#,>,Fz3/c7:s#;<	=r)   c                    || _         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                  j                  d      | _        |j                  j                  d      | _        t        j                  t        |j                  j                  d	            | _        t        j                  t        |j                  j                  d
            | _        t        j                  t        |j                  j                  d            | _        y )Nr  r	  r
  r   r  r  r  r  r   r   r   )rV   r   r   r   r   r]   r  r	  r
  r   r  r  r  r  r   r   r   r  s     r'   r`   zMediaContainer._loadDataf  s(   
C)EF#{{/@A++//,7 %

3@R0S T#';;??3H#I "&++//2F"G"kkoo.>?#{{/@Ajjdkkooh&?@JJsDKKOOF$;<	C)EFr)   )r|   r  r  r  r   r   r   r   r   r   rg   r   r	   r   r   r`   r  r  s   @r'   r   r     s    
" C #''+
B
B 
B 	
B
 3-
B $
B 

B,=,=(;/@A,= 
,=\Gr)   )-rM   typingr   r   r   r   r   r   r	   rX   	functoolsr
   urllib.parser   r   r   	xml.etreer   xml.etree.ElementTreer   plexapir   r   r   r   plexapi.exceptionsr   r   r   r   plexapi.serverr   r   r   rM  r=  r<  r   r   r  r  objectr?  r@  r   r#   r)   r'   <module>r     s   	 S S S  % 7 7 ! ) = = M M)m<8+3CDE ,  1 # 4	
 	
 	
 	
 
 	
 
 . ? * ; <  /!" E#	*O OduE
 uEpo odL6& L6^X& X>^GK^Gr)   