66from homeassistant .core import HomeAssistant
77from .const import DEFAULT_PARSE_DICT , USER_AGENT , ACCEPTS
88from .parser import parse_data , parse_library
9-
9+ from . tmdb_api import get_tmdb_trailer_url
1010
1111def check_headers (response ):
1212 if 'text/xml' not in response .headers .get ('Content-Type' , '' ) and 'application/xml' not in response .headers .get ('Content-Type' , '' ):
@@ -38,33 +38,6 @@ def __init__(
3838 self ._section_libraries = section_libraries
3939 self ._exclude_keywords = exclude_keywords
4040 self ._images_base_url = f'/{ name .lower () + "_" if len (name ) > 0 else "" } plex_recently_added'
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
6841
6942 async def update (self ):
7043 info_url = 'http{0}://{1}:{2}' .format (
@@ -147,7 +120,7 @@ async def update(self):
147120
148121 # Fetch trailer URLs for each item
149122 for item in parsed_libs :
150- item ['trailer' ] = await self .get_trailer_url ( item ['ratingKey ' ])
123+ item ['trailer' ] = await get_tmdb_trailer_url ( self ._hass , item ['title' ], library [ 'type ' ])
151124
152125 if library ["type" ] not in data ['all' ]:
153126 data ['all' ][library ["type" ]] = []
@@ -156,14 +129,23 @@ async def update(self):
156129
157130 data_out = {}
158131 for k in data .keys ():
159- data_out [k ] = {'data' : [DEFAULT_PARSE_DICT ] + parse_data (data [k ], self ._max , info_url , self ._token , identifier , k , self ._images_base_url , k == "all" )}
132+ parsed_data = parse_data (data [k ], self ._max , info_url , self ._token , identifier , k , self ._images_base_url , k == "all" )
133+
134+ # Ensure trailer URLs are correctly set for the "all" sensor
135+ if k == "all" :
136+ for item in parsed_data :
137+ if item .get ('trailer' ) is None :
138+ item_type = 'movie' if item .get ('episode' ) == '' else 'show'
139+ item ['trailer' ] = await get_tmdb_trailer_url (self ._hass , item ['title' ], item_type )
140+
141+ data_out [k ] = {'data' : [DEFAULT_PARSE_DICT ] + parsed_data }
160142
161143 return {
162144 "data" : {** data_out },
163145 "online" : True ,
164146 "libraries" : libs
165147 }
166-
148+
167149
168150class FailedToLogin (Exception ):
169151 "Raised when the Plex user fail to Log-in"
0 commit comments