1818from homeassistant .const import CONF_HOST , CONF_PORT , CONF_SSL
1919from homeassistant .helpers .entity import Entity
2020
21- __version__ = '0.1.5 '
21+ __version__ = '0.1.7 '
2222
2323_LOGGER = logging .getLogger (__name__ )
2424
@@ -74,9 +74,8 @@ def __init__(self, hass, conf):
7474 self .change_detected = False
7575 self ._state = None
7676 self .card_json = []
77- self .media_ids = [1 ]
7877 self .api_json = []
79- self .data = []
78+ self .data = [{ 1 } ]
8079
8180 @property
8281 def name (self ):
@@ -208,11 +207,23 @@ def update(self):
208207 """Get JSON for each library, combine and sort."""
209208 for library in sections :
210209 sub_sec = plex .get (recently_added .format (
211- library , self .max_items * 2 ), headers = headers , timeout = 10 )
212- self .api_json += sub_sec .json ()['MediaContainer' ]['Metadata' ]
210+ library , self .max_items * 2 ), headers = headers , timeout = 10 )
211+ try :
212+ self .api_json += sub_sec .json ()['MediaContainer' ]['Metadata' ]
213+ except :
214+ _LOGGER .warning ('No Metadata field for "{}"' .format (sub_sec .json ()['MediaContainer' ]['librarySectionTitle' ]))
215+ pass
213216 self .api_json = sorted (self .api_json , key = lambda i : i ['addedAt' ],
214217 reverse = True )[:self .max_items ]
218+ overview = get_info (self .api_json [0 ].get ('title' , '' ))
215219
220+ """Update attributes if view count changes"""
221+ if view_count (self .api_json ) != view_count (self .data ):
222+ self .change_detected = True
223+ self .data = self .api_json
224+
225+ api_ids = media_ids (self .api_json , True )
226+ data_ids = media_ids (self .data , True )
216227 if self .dl_images :
217228 directory = self .conf_dir + 'www' + self ._dir
218229 if not os .path .exists (directory ):
@@ -226,14 +237,12 @@ def update(self):
226237 dir_ids .sort (key = int )
227238
228239 """Update if media items have changed or images are missing"""
229- if (dir_ids != self .media_ids or
230- media_ids (self .api_json , True ) != self .media_ids ):
240+ if dir_ids != api_ids or data_ids != api_ids :
231241 self .change_detected = True # Tell attributes to update
232242 self .data = self .api_json
233- self .media_ids = media_ids (self .data , True )
234243 """Remove images not in list"""
235244 for file in dir_images :
236- if not any (str (ids ) in file for ids in self . media_ids ):
245+ if not any (str (ids ) in file for ids in data_ids ):
237246 os .remove (directory + file )
238247 """Retrieve image from Plex if it doesn't exist"""
239248 for media in self .data :
@@ -267,10 +276,9 @@ def update(self):
267276 continue
268277 else :
269278 """Update if media items have changed"""
270- if media_ids ( self . api_json , True ) != self . media_ids :
279+ if api_ids != data_ids :
271280 self .change_detected = True # Tell attributes to update
272281 self .data = self .api_json
273- self .media_ids = media_ids (self .data , False )
274282 else :
275283 self ._state = '%s cannot be reached' % self .server_ip
276284
@@ -340,3 +348,24 @@ def media_ids(data, remote):
340348 ids = ids * 2
341349 ids .sort (key = int )
342350 return ids
351+
352+
353+ def view_count (data ):
354+ ids = []
355+ for media in data :
356+ if 'ratingKey' in media :
357+ if 'viewCount' in media :
358+ ids .append (str (media ['viewCount' ]))
359+ else :
360+ ids .append ('0' )
361+ else :
362+ continue
363+ return ids
364+
365+
366+ def get_info (title ):
367+ tmdb_url = requests .get ('https://api.themoviedb.org/3/search/movie?'
368+ 'api_key=1f7708bb9a218ab891a5d438b1b63992&query='
369+ + title )
370+ tmdb_json = tmdb_url .json ()
371+ return tmdb_json ['results' ][0 ]['overview' ]
0 commit comments