@@ -39,6 +39,33 @@ def __init__(
39
39
self ._exclude_keywords = exclude_keywords
40
40
self ._images_base_url = f'/{ name .lower () + "_" if len (name ) > 0 else "" } plex_recently_added'
41
41
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
+
42
69
async def update (self ):
43
70
info_url = 'http{0}://{1}:{2}' .format (
44
71
self ._ssl ,
@@ -117,6 +144,11 @@ async def update(self):
117
144
check_headers (sub_sec )
118
145
root = ElementTree .fromstring (sub_sec .text )
119
146
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
+
120
152
if library ["type" ] not in data ['all' ]:
121
153
data ['all' ][library ["type" ]] = []
122
154
data ['all' ][library ["type" ]] += parsed_libs
@@ -135,4 +167,4 @@ async def update(self):
135
167
136
168
class FailedToLogin (Exception ):
137
169
"Raised when the Plex user fail to Log-in"
138
- pass
170
+ pass
0 commit comments