Skip to content

Commit

Permalink
feat: add trailer and summary attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandoamaral committed Aug 22, 2024
1 parent e7fd0bb commit bfb38ec
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ views:
- title: Test environment
cards:
- type: custom:upcoming-media-card
entity: sensor.trakt_next_to_watch_all
entity: sensor.trakt_next_to_watch_all
enable_tooltips: true
enable_trailers: true
52 changes: 47 additions & 5 deletions custom_components/trakt_tv/apis/tmdb.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
from typing import Any, Dict
from typing import Any, Dict, Optional

import aiohttp

from custom_components.trakt_tv.const import TMDB_HOST, TMDB_TOKEN


async def get_media_data(kind: str, tmbd_id: int, language: str) -> Dict[str, Any]:
async def get_media_data(
kind: str, prefix: str, tmbd_id: int, language: str
) -> Dict[str, Any]:
"""
Get information from TMDB about a kind of media.
:param kind: The kind of media
:param prefix: The route prefix
:param tmbd_id: The ID of the media
:param language: The favorite language of the user
"""
url = f"{TMDB_HOST}/3/{kind}/{tmbd_id}?api_key={TMDB_TOKEN}&language={language}"
url = f"{TMDB_HOST}/3/{kind}/{tmbd_id}{prefix}?api_key={TMDB_TOKEN}&language={language}"
async with aiohttp.request("GET", url) as response:
return await response.json()

Expand All @@ -25,7 +28,7 @@ async def get_movie_data(tmbd_id: int, language: str) -> Dict[str, Any]:
:param tmbd_id: The ID of the movie
:param language: The favorite language of the user
"""
return await get_media_data("movie", tmbd_id, language)
return await get_media_data("movie", "", tmbd_id, language)


async def get_show_data(tmbd_id: int, language: str) -> Dict[str, Any]:
Expand All @@ -35,4 +38,43 @@ async def get_show_data(tmbd_id: int, language: str) -> Dict[str, Any]:
:param tmbd_id: The ID of the show
:param language: The favorite language of the user
"""
return await get_media_data("tv", tmbd_id, language)
return await get_media_data("tv", "", tmbd_id, language)


def _extract_trailer_from_data(data: Dict[str, Any]) -> Optional[str]:
"""
Extract trailer data from video data.
"""
videos = data.get("results", {})

for video in videos:
site = video.get("site")
_type = video.get("type")
key = video.get("key")

if site == "YouTube" and _type == "Trailer" and key is not None:
return f"https://www.youtube.com/watch?v={key}"

return None


async def get_movie_trailer(tmbd_id: int, language: str) -> Optional[str]:
"""
Get trailer url from TMDB about a movie.
:param tmbd_id: The ID of the movie
:param language: The favorite language of the user
"""
data = await get_media_data("movie", "/videos", tmbd_id, language)
return _extract_trailer_from_data(data)


async def get_show_trailer(tmbd_id: int, language: str) -> Optional[str]:
"""
Get trailer url from TMDB about a show.
:param tmbd_id: The ID of the show
:param language: The favorite language of the user
"""
data = await get_media_data("tv", "/videos", tmbd_id, language)
return _extract_trailer_from_data(data)
59 changes: 45 additions & 14 deletions custom_components/trakt_tv/models/media.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from abc import ABC, abstractmethod, abstractstaticmethod
from asyncio import gather
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional

from custom_components.trakt_tv.apis.tmdb import get_movie_data, get_show_data
from custom_components.trakt_tv.apis.tmdb import (
get_movie_data,
get_movie_trailer,
get_show_data,
get_show_trailer,
)

from ..const import UPCOMING_DATA_FORMAT

Expand Down Expand Up @@ -78,7 +84,7 @@ def common_information(self) -> Dict[str, Any]:

return {k: v for k, v in default.items() if v is not None}

async def get_more_information(self, language):
async def get_more_information(self, language: str):
"""
Get information from other API calls to complete the trakt movie.
Expand All @@ -93,6 +99,8 @@ class Movie(Media):
"""

genres: List[str] = field(default_factory=list)
trailer: Optional[str] = None
summary: Optional[str] = None
poster: Optional[str] = None
fanart: Optional[str] = None
rating: Optional[int] = None
Expand All @@ -119,15 +127,23 @@ def from_trakt(data) -> "Movie":
ids=Identifiers.from_trakt(movie),
)

async def get_more_information(self, language):
async def get_more_information(self, language: str):
"""
Get information from other API calls to complete the trakt movie.
:param language: The favorite language of the user
"""
data = await get_movie_data(self.ids.tmdb, language)
data, trailer = await gather(
get_movie_data(self.ids.tmdb, language),
get_movie_trailer(self.ids.tmdb, language),
)

if title := data.get("title"):
self.name = title
if trailer:
self.trailer = trailer
if summary := data.get("overview"):
self.summary = summary
if poster := data.get("poster_path"):
self.poster = f"https://image.tmdb.org/t/p/w500{poster}"
if fanart := data.get("backdrop_path"):
Expand Down Expand Up @@ -165,6 +181,10 @@ def to_homeassistant(self) -> Dict[str, Any]:

if self.ids.slug is not None:
default["deep_link"] = f"https://trakt.tv/movies/{self.ids.slug}"
if self.trailer is not None:
default["trailer"] = self.trailer
if self.summary is not None:
default["summary"] = self.summary

return default

Expand Down Expand Up @@ -193,6 +213,8 @@ def from_trakt(data) -> "Episode":

@dataclass
class Show(Media):
trailer: Optional[str] = None
summary: Optional[str] = None
poster: Optional[str] = None
fanart: Optional[str] = None
genres: List[str] = field(default_factory=list)
Expand Down Expand Up @@ -225,9 +247,23 @@ def from_trakt(data) -> "Show":
episode=episode,
)

def update_common_information(self, data: Dict[str, Any]):
async def get_more_information(self, language: str):
"""
Get information from other API calls to complete the trakt movie.
:param language: The favorite language of the user
"""
data, trailer = await gather(
get_show_data(self.ids.tmdb, language),
get_show_trailer(self.ids.tmdb, language),
)

if title := data.get("title"):
self.name = title
if trailer:
self.trailer = trailer
if summary := data.get("overview"):
self.summary = summary
if poster := data.get("poster_path"):
self.poster = f"https://image.tmdb.org/t/p/w500{poster}"
if fanart := data.get("backdrop_path"):
Expand All @@ -246,15 +282,6 @@ def update_common_information(self, data: Dict[str, Any]):
# If we really can't find the release date, we set it to the minimum date
self.released = datetime.min

async def get_more_information(self, language):
"""
Get information from other API calls to complete the trakt movie.
:param language: The favorite language of the user
"""
data = await get_show_data(self.ids.tmdb, language)
self.update_common_information(data)

def to_homeassistant(self) -> Dict[str, Any]:
"""
Convert the Show to upcoming data.
Expand Down Expand Up @@ -283,6 +310,10 @@ def to_homeassistant(self) -> Dict[str, Any]:

if self.ids.slug is not None:
default["deep_link"] = f"https://trakt.tv/shows/{self.ids.slug}"
if self.trailer is not None:
default["trailer"] = self.trailer
if self.summary is not None:
default["summary"] = self.summary

return default

Expand Down

0 comments on commit bfb38ec

Please sign in to comment.