Skip to content

Commit

Permalink
Display image dimensions when browsing TMDb within UI
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinHeist committed May 17, 2023
1 parent 68f2084 commit e2fa24d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 54 deletions.
28 changes: 4 additions & 24 deletions app/routers/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
create_source_image, download_episode_source_image, download_series_logo
)
import app.models as models
from app.schemas.card import SourceImage
from app.schemas.card import SourceImage, TMDbImage

from modules.Debug import log
from modules.WebInterface import WebInterface
Expand Down Expand Up @@ -179,8 +179,7 @@ def download_episode_source_image_(
def get_all_tmdb_episode_source_images(
episode_id: int,
db = Depends(get_database),
preferences = Depends(get_preferences),
tmdb_interface = Depends(get_tmdb_interface)) -> Optional[list[str]]:
tmdb_interface = Depends(get_tmdb_interface)) -> list[TMDbImage]:
"""
Get all Source Images on TMDb for the given Episode.
Expand All @@ -198,38 +197,19 @@ def get_all_tmdb_episode_source_images(
episode = get_episode(db, episode_id, raise_exc=True)
series = get_series(db, episode.series_id, raise_exc=True)

# Get the Templates for these items
series_template = get_template(db, series.template_id, raise_exc=True)
episode_template = get_template(db, episode.template_id, raise_exc=True)

# Determine title matching
if episode.match_title is not None:
match_title = episode.match_title
else:
match_title = series.match_titles

# Determine whether to skip localized images
if (episode_template is not None
and episode_template.skip_localized_images is not None):
skip_localized_images = episode_template.skip_localized_images
elif series.skip_localized_images is not None:
skip_localized_images = series.skip_localized_images
elif (series_template is not None
and series_template.skip_localized_images is not None):
skip_localized_images = series_template.skip_localized_images
else:
skip_localized_images = preferences.tmdb_skip_localized

# Get all sources
sources = tmdb_interface.get_all_source_images(
# Get all source images
return tmdb_interface.get_all_source_images(
series.as_series_info,
episode.as_episode_info,
match_title=match_title,
skip_localized_images=skip_localized_images,
)

return sources


@source_router.get('/series/{series_id}', status_code=200)
def get_existing_series_source_images(
Expand Down
5 changes: 5 additions & 0 deletions app/schemas/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ def convert_paths_to_str(cls, v):
"""
Return classes
"""
class TMDbImage(Base):
url: str
width: int
height: int

class SourceImage(Base):
episode_id: int
season_number: int
Expand Down
3 changes: 0 additions & 3 deletions app/templates/cardTemplates.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
<template id="filter-template">
<div class="ui three fields">
<div class="ui field">
<!-- <label>Argument</label> -->
<div data-value="filter-arguments" class="ui clearable selection dropdown">
<input name="argument" type="hidden">
<i class="dropdown icon"></i>
Expand All @@ -58,7 +57,6 @@
</div>
</div>
<div class="ui field">
<!-- <label>Operator</label> -->
<div data-value="filter-operators" class="ui clearable selection dropdown">
<input name="operation" type="hidden">
<i class="dropdown icon"></i>
Expand All @@ -67,7 +65,6 @@
</div>
</div>
<div class="ui field">
<!-- <label>Reference</label> -->
<div class="ui input">
<input data-value="filter-reference" name="reference" type="text" placeholder="Reference Value">
</div>
Expand Down
5 changes: 3 additions & 2 deletions app/templates/series.html
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,9 @@
}

// Images returned, add to browse modal
const imageElements = images.map(url => {
return `<a onclick="selectTmdbImage(${episodeId}, '${url}')"><img src="${url}"/></a>`
const imageElements = images.map(({url, width, height}, index) => {
const location = index % 2 ? 'right' : 'left';
return `<a class="ui image" onclick="selectTmdbImage(${episodeId}, '${url}')"><div class="ui blue ${location} ribbon label">${width}x${height}</div><img src="${url}"/></a>`;
});
$('#browse-tmdb-modal .content .images')[0].innerHTML = imageElements.join('');
$('#browse-tmdb-modal').modal('show');
Expand Down
2 changes: 1 addition & 1 deletion app/templates/theme.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function refreshTheme() {
const inverted = (window.localStorage.getItem('site-theme') === 'dark');
$('#main-content .ui').toggleClass('inverted', inverted);
$('#main-content .ui:not(.uninvertible)').toggleClass('inverted', inverted);
$('.modal:not(.basic):not(.uninvertible), .modal:not(.basic):not(.uninvertible) .ui, .accordion, .accordion .ui').toggleClass('inverted', inverted);
}

Expand Down
35 changes: 11 additions & 24 deletions modules/TMDbInterface2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from datetime import datetime, timedelta
from pathlib import Path
from tinydb import where
from typing import Any, Iterable, Optional, Union
from typing import Any, Optional, Union

from fastapi import HTTPException
from tmdbapis import TMDbAPIs, NotFound, Unauthorized, TMDbException

from modules.Debug import log
from modules.EpisodeDataSource import EpisodeDataSource
from modules.EpisodeInfo2 import EpisodeInfo
import modules.global_objects as global_objects
from modules.PersistentDatabase import PersistentDatabase
from modules.SeriesInfo import SeriesInfo
from modules.WebInterface import WebInterface
Expand Down Expand Up @@ -697,25 +695,23 @@ def get_all_source_images(self,
series_info: SeriesInfo,
episode_info: EpisodeInfo, *,
match_title: bool = True,
skip_localized_images: bool = False,
return_objects: bool = False,
) -> Optional[list[Union['tmdbapis.objs.image.Still', str]]]:
) -> Optional[list['tmdbapis.objs.image.Still']]:
"""
Get all source image for the requested entry.
Get all source images for the requested entry.
Args:
series_info: SeriesInfo for this entry.
episode_info: EpisodeInfo for this entry.
match_title: (Keyword only) Whether to require the episode
title to match when querying TMDb.
skip_localized_images: (Keyword only) Whether to skip images
with a non-null language code - i.e. skipping localized
images.
Returns:
List of Still objects for the requested entry. If the
episode is blacklisted or not found on TMDb, then None is
returned.
List of tmdbapis.objs.image.Still objects. If the episode is
blacklisted or not found on TMDb, then None is returned.
Raises:
HTTPException (404) if the given Series+Episode is not found
on TMDb.
"""

# Don't query the database if this episode is in the blacklist
Expand All @@ -730,17 +726,12 @@ def get_all_source_images(self,
status_code=404,
detail=f'"{series_info}" {episode_info} not found on TMDb'
)
return None

# Episode found on TMDb, get images/backdrops based on episode/movie
if hasattr(episode, 'stills'): images = episode.stills
else: images = episode.backdrops

# If returning objects, return TMDbImages directly - otherwise URL's.
if return_objects:
return images

return [image.url for image in images]
return images


@catch_and_log('Error getting source image', default=None)
Expand Down Expand Up @@ -768,11 +759,7 @@ def get_source_image(self,

# Get all images for this episode
all_images = self.get_all_source_images(
series_info,
episode_info,
match_title=match_title,
skip_localized_images=skip_localized_images,
return_objects=True,
series_info, episode_info, match_title=match_title,
)

# If None, either blacklisted or episode was not found
Expand Down

1 comment on commit e2fa24d

@CollinHeist
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an example of this:

Please sign in to comment.