diff --git a/custom_components/sonarr_upcoming_media/__init__.py b/custom_components/sonarr_upcoming_media/__init__.py index e12be9c..3388f8e 100644 --- a/custom_components/sonarr_upcoming_media/__init__.py +++ b/custom_components/sonarr_upcoming_media/__init__.py @@ -1,26 +1,27 @@ from homeassistant.config_entries import ConfigEntry -from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.const import ( - CONF_API_KEY, - CONF_HOST, - CONF_PORT, + Platform, + CONF_API_KEY, + CONF_HOST, + CONF_PORT, CONF_SSL ) from .const import ( - DOMAIN, + DOMAIN, CONF_DAYS, CONF_URLBASE, CONF_MAX, - ) +) from .coordinator import SonarrDataCoordinator from .helpers import setup_client from .sonarr_api import ( FailedToLogin, SonarrCannotBeReached ) +from .parsing import TMDBApiNotResponding PLATFORMS = [ Platform.SENSOR @@ -43,6 +44,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b raise ConfigEntryNotReady("Failed to Log-in") from err except SonarrCannotBeReached as err: raise ConfigEntryNotReady("Sonarr cannot be reached") from err + except TMDBApiNotResponding as err: + raise ConfigEntryNotReady("TMDB API is not responding") from err coordinator = SonarrDataCoordinator(hass, client) await coordinator.async_config_entry_first_refresh() @@ -60,4 +63,4 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> del hass.data[DOMAIN][config_entry.entry_id] if not hass.data[DOMAIN]: del hass.data[DOMAIN] - return unload_ok \ No newline at end of file + return unload_ok diff --git a/custom_components/sonarr_upcoming_media/manifest.json b/custom_components/sonarr_upcoming_media/manifest.json index c4ad8c3..6a02d34 100644 --- a/custom_components/sonarr_upcoming_media/manifest.json +++ b/custom_components/sonarr_upcoming_media/manifest.json @@ -8,6 +8,6 @@ "iot_class": "local_polling", "issue_tracker": "https://github.com/custom-components/sensor.sonarr_upcoming_media/issues", "requirements": [], - "version": "0.4.2" + "version": "0.4.3" } \ No newline at end of file diff --git a/custom_components/sonarr_upcoming_media/parsing.py b/custom_components/sonarr_upcoming_media/parsing.py index 71e7d25..6d338df 100644 --- a/custom_components/sonarr_upcoming_media/parsing.py +++ b/custom_components/sonarr_upcoming_media/parsing.py @@ -1,9 +1,14 @@ import time from datetime import date, datetime from pytz import timezone +import requests from .const import DEFAULT_PARSE_DICT +TMDB_BASE_URL = 'http://api.tmdb.org/3/search/tv?api_key=1f7708bb9a218ab891a5d438b1b63992&query={}' +TMDB_DETAILS_URL = 'http://api.tmdb.org/3/tv/{}?api_key=1f7708bb9a218ab891a5d438b1b63992&append_to_response=videos' +TMDB_BASE_IMAGE_URL = 'https://image.tmdb.org/t/p/w{0}{1}' + def days_until(date, tz): from pytz import utc date = datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ') @@ -60,6 +65,40 @@ def parse_data(data, tz, host, port, ssl, url_base=None): else: card_item['genres'] = '' card_item['summary'] = show.get('overview', '') + + if 'title' in show['series']: + session = requests.Session() + try: + search_url = TMDB_BASE_URL.format(requests.utils.quote(show['series']['title'].split('(')[0].strip())) + tmdb_url = session.get(search_url) + tmdb_json = tmdb_url.json() + + if tmdb_json['results']: + tmdb_id = tmdb_json['results'][0]['id'] + details_url = TMDB_DETAILS_URL.format(tmdb_id) + details_response = session.get(details_url) + details_json = details_response.json() + + if 'videos' in details_json and details_json['videos']['results']: + for video in details_json['videos']['results']: + if video['type'] == 'Trailer': + card_item['trailer'] = f'https://www.youtube.com/watch?v={video["key"]}' + break + else: + card_item['trailer'] = '' + else: + card_item['trailer'] = '' + + if 'poster_path' in tmdb_json['results'][0]: + card_item['poster'] = TMDB_BASE_IMAGE_URL.format('500', tmdb_json['results'][0]['poster_path']) + if 'backdrop_path' in tmdb_json['results'][0]: + card_item['fanart'] = TMDB_BASE_IMAGE_URL.format('780', tmdb_json['results'][0]['backdrop_path']) + else: + card_item['trailer'] = '' + except: + card_item['trailer'] = '' + raise TMDBApiNotResponding + try: for img in show['series']['images']: if img['coverType'] == 'poster': @@ -78,4 +117,8 @@ def parse_data(data, tz, host, port, ssl, url_base=None): card_item['deep_link'] = f'{protocol}://{host}:{port}/{url_base.strip("/") + "/" if url_base else ""}series/{series_title_slug}' card_json.append(card_item) attributes['data'] = card_json - return attributes \ No newline at end of file + return attributes + +class TMDBApiNotResponding(Exception): + "Raised when the TMDB API is not responding" + pass