6
6
from homeassistant .core import HomeAssistant
7
7
from .const import DEFAULT_PARSE_DICT , USER_AGENT , ACCEPTS
8
8
from .parser import parse_data , parse_library
9
-
9
+ from . tmdb_api import get_tmdb_trailer_url
10
10
11
11
def check_headers (response ):
12
12
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__(
38
38
self ._section_libraries = section_libraries
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
-
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
41
69
42
async def update (self ):
70
43
info_url = 'http{0}://{1}:{2}' .format (
@@ -147,7 +120,7 @@ async def update(self):
147
120
148
121
# Fetch trailer URLs for each item
149
122
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 ' ])
151
124
152
125
if library ["type" ] not in data ['all' ]:
153
126
data ['all' ][library ["type" ]] = []
@@ -156,14 +129,23 @@ async def update(self):
156
129
157
130
data_out = {}
158
131
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 }
160
142
161
143
return {
162
144
"data" : {** data_out },
163
145
"online" : True ,
164
146
"libraries" : libs
165
147
}
166
-
148
+
167
149
168
150
class FailedToLogin (Exception ):
169
151
"Raised when the Plex user fail to Log-in"
0 commit comments