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' , '' ):
@@ -25,8 +25,7 @@ def __init__(
2525 port : int ,
2626 section_types : list ,
2727 section_libraries : list ,
28- exclude_keywords : list ,
29- verify_ssl : bool
28+ exclude_keywords : list
3029 ):
3130 self ._hass = hass
3231 self ._ssl = 's' if ssl else ''
@@ -38,9 +37,8 @@ def __init__(
3837 self ._section_types = section_types
3938 self ._section_libraries = section_libraries
4039 self ._exclude_keywords = exclude_keywords
41- self ._verify_ssl = verify_ssl
4240 self ._images_base_url = f'/{ name .lower () + "_" if len (name ) > 0 else "" } plex_recently_added'
43-
41+
4442 async def update (self ):
4543 info_url = 'http{0}://{1}:{2}' .format (
4644 self ._ssl ,
@@ -49,8 +47,6 @@ async def update(self):
4947 )
5048
5149 """ Getting the server identifier """
52- if not self ._verify_ssl :
53- requests .packages .urllib3 .disable_warnings (category = InsecureRequestWarning )
5450 try :
5551 info_res = await self ._hass .async_add_executor_job (
5652 requests .get ,
@@ -60,7 +56,6 @@ async def update(self):
6056 "User-agent" : USER_AGENT ,
6157 "Accept" : ACCEPTS ,
6258 },
63- "verify" :self ._verify_ssl ,
6459 "timeout" :10
6560 }
6661 )
@@ -87,7 +82,6 @@ async def update(self):
8782 "User-agent" : USER_AGENT ,
8883 "Accept" : ACCEPTS ,
8984 },
90- "verify" :self ._verify_ssl ,
9185 "timeout" :10
9286 }
9387 )
@@ -117,29 +111,42 @@ async def update(self):
117111 "User-agent" : USER_AGENT ,
118112 "Accept" : ACCEPTS ,
119113 },
120- "verify" :self ._verify_ssl ,
121114 "timeout" :10
122115 }
123116 )
124117 check_headers (sub_sec )
125118 root = ElementTree .fromstring (sub_sec .text )
126119 parsed_libs = parse_library (root )
120+
121+ # Fetch trailer URLs for each item
122+ for item in parsed_libs :
123+ item ['trailer' ] = await get_tmdb_trailer_url (self ._hass , item ['title' ], library ['type' ])
124+
127125 if library ["type" ] not in data ['all' ]:
128126 data ['all' ][library ["type" ]] = []
129127 data ['all' ][library ["type" ]] += parsed_libs
130128 data [library ["type" ]] += parsed_libs
131129
132130 data_out = {}
133131 for k in data .keys ():
134- 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 }
135142
136143 return {
137144 "data" : {** data_out },
138145 "online" : True ,
139146 "libraries" : libs
140147 }
141-
148+
142149
143150class FailedToLogin (Exception ):
144151 "Raised when the Plex user fail to Log-in"
145- pass
152+ pass
0 commit comments