Skip to content

Commit

Permalink
Added video trailer attribute for video trailer playback in UMC
Browse files Browse the repository at this point in the history
  • Loading branch information
mkanet committed Jun 25, 2024
1 parent 131528a commit 6134a83
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
17 changes: 10 additions & 7 deletions custom_components/sonarr_upcoming_media/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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
return unload_ok
2 changes: 1 addition & 1 deletion custom_components/sonarr_upcoming_media/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

45 changes: 44 additions & 1 deletion custom_components/sonarr_upcoming_media/parsing.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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':
Expand All @@ -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
return attributes

class TMDBApiNotResponding(Exception):
"Raised when the TMDB API is not responding"
pass

0 comments on commit 6134a83

Please sign in to comment.