77https://github.com/custom-cards/upcoming-media-card
88
99"""
10- import json
11- import logging
12- import math
13- import os
1410import os .path
15- import re
16- import time
17- import xml .etree .ElementTree as ET
18- from datetime import datetime
19- from unicodedata import normalize
20- from urllib .parse import quote
21-
11+ import logging
12+ import json
2213import requests
23-
24- import homeassistant .helpers .config_validation as cv
2514import voluptuous as vol
15+ import homeassistant .helpers .config_validation as cv
16+ from datetime import datetime
2617from homeassistant .components .sensor import PLATFORM_SCHEMA
2718from homeassistant .const import CONF_HOST , CONF_PORT , CONF_SSL
2819from homeassistant .helpers .entity import Entity
29- from pytz import timezone , utc
3020
3121__version__ = '0.1.7'
3222
@@ -60,6 +50,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
6050class PlexRecentlyAddedSensor (Entity ):
6151
6252 def __init__ (self , hass , conf ):
53+ from pytz import timezone
6354 self .conf_dir = str (hass .config .path ()) + '/'
6455 self ._dir = conf .get (CONF_IMG_CACHE )
6556 self .img = '{0}{1}{2}{3}{4}.jpg' .format (
@@ -96,6 +87,7 @@ def state(self):
9687
9788 @property
9889 def device_state_attributes (self ):
90+ import math
9991 attributes = {}
10092 if self .change_detected :
10193 self .card_json = []
@@ -185,6 +177,8 @@ def device_state_attributes(self):
185177 return attributes
186178
187179 def update (self ):
180+ import re
181+ import os
188182 plex = requests .Session ()
189183 if not self .cert :
190184 """Default SSL certificate is for plex.tv not our api server"""
@@ -291,6 +285,7 @@ def update(self):
291285
292286def image_url (url_elements , cert_check , img ):
293287 """Plex can resize images with a long & partially % encoded url."""
288+ from urllib .parse import quote
294289 ssl , host , local , port , token , self_cert , dl_images = url_elements
295290 if not cert_check and not self_cert :
296291 ssl = ''
@@ -314,6 +309,8 @@ def image_url(url_elements, cert_check, img):
314309
315310def get_server_ip (name , token ):
316311 """With a token and server name we get server's ip, local ip, and port"""
312+ import xml .etree .ElementTree as ET
313+ from unicodedata import normalize
317314 plex_tv = requests .get (
318315 'https://plex.tv/api/servers.xml?X-Plex-Token=' + token , timeout = 10 )
319316 plex_xml = ET .fromstring (plex_tv .content )
@@ -326,6 +323,8 @@ def get_server_ip(name, token):
326323
327324
328325def days_since (date , tz ):
326+ import time
327+ from pytz import utc
329328 date = datetime .utcfromtimestamp (date ).isoformat () + 'Z'
330329 date = datetime .strptime (date , '%Y-%m-%dT%H:%M:%SZ' )
331330 date = str (date .replace (tzinfo = utc ).astimezone (tz ))[:10 ]
@@ -366,7 +365,11 @@ def view_count(data):
366365
367366def get_info (title ):
368367 tmdb_url = requests .get ('https://api.themoviedb.org/3/search/movie?'
369- 'api_key=1f7708bb9a218ab891a5d438b1b63992&query='
368+ + 'api_key=1f7708bb9a218ab891a5d438b1b63992&query='
370369 + title )
371- tmdb_json = tmdb_url .json ()
372- return tmdb_json ['results' ][0 ]['overview' ]
370+ try :
371+ tmdb_json = tmdb_url .json ()['results' ][0 ]['overview' ]
372+ except :
373+ _LOGGER .warning ("Failed to get information from TMDB" )
374+ tmdb_json = {}
375+ return tmdb_json
0 commit comments