@@ -39,6 +39,33 @@ def __init__(
3939 self ._exclude_keywords = exclude_keywords
4040 self ._images_base_url = f'/{ name .lower () + "_" if len (name ) > 0 else "" } plex_recently_added'
4141
42+ async def get_trailer_url (self , item_key ):
43+ extras_url = f'http{ self ._ssl } ://{ self ._host } :{ self ._port } /library/metadata/{ item_key } /extras?X-Plex-Token={ self ._token } '
44+ try :
45+ extras_res = await self ._hass .async_add_executor_job (
46+ requests .get ,
47+ extras_url ,
48+ {
49+ "headers" : {
50+ "User-agent" : USER_AGENT ,
51+ "Accept" : ACCEPTS ,
52+ },
53+ "timeout" : 10
54+ }
55+ )
56+ check_headers (extras_res )
57+ root = ElementTree .fromstring (extras_res .text )
58+
59+ for video in root .findall (".//Video" ):
60+ if video .get ("type" ) == "clip" and video .get ("subtype" ) == "trailer" :
61+ part = video .find (".//Part" )
62+ if part is not None and part .get ("key" ):
63+ return f'http{ self ._ssl } ://{ self ._host } :{ self ._port } { part .get ("key" )} &X-Plex-Token={ self ._token } '
64+
65+ except Exception as e :
66+ print (f"Error fetching trailer: { str (e )} " )
67+ return None
68+
4269 async def update (self ):
4370 info_url = 'http{0}://{1}:{2}' .format (
4471 self ._ssl ,
@@ -117,6 +144,11 @@ async def update(self):
117144 check_headers (sub_sec )
118145 root = ElementTree .fromstring (sub_sec .text )
119146 parsed_libs = parse_library (root )
147+
148+ # Fetch trailer URLs for each item
149+ for item in parsed_libs :
150+ item ['trailer' ] = await self .get_trailer_url (item ['ratingKey' ])
151+
120152 if library ["type" ] not in data ['all' ]:
121153 data ['all' ][library ["type" ]] = []
122154 data ['all' ][library ["type" ]] += parsed_libs
@@ -135,4 +167,4 @@ async def update(self):
135167
136168class FailedToLogin (Exception ):
137169 "Raised when the Plex user fail to Log-in"
138- pass
170+ pass
0 commit comments