From 7bb3d718c87cd1227044a88a1140f8d9e6f33803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lker=20SI=C4=9EIRCI?= Date: Mon, 9 Dec 2024 17:00:15 +0300 Subject: [PATCH] get fallback image url for search results --- Makefile | 2 +- docs/steps.md | 5 ++--- src/movie_guess/fasthtml_app.py | 13 +++++++++---- src/movie_guess/utils/movie.py | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index cbc9e88..0125698 100644 --- a/Makefile +++ b/Makefile @@ -200,4 +200,4 @@ run-streamlit: ## Run streamlit app uv run streamlit run src/movie_guess/streamlit_app.py run-fasthtml: ## Run fasthtml app - uv run streamlit run src/movie_guess/fasthtml_app.py + uv run python src/movie_guess/fasthtml_app.py diff --git a/docs/steps.md b/docs/steps.md index 519d7b0..6a3a213 100644 --- a/docs/steps.md +++ b/docs/steps.md @@ -1,7 +1,6 @@ -I want to create a movie name - guess app with following steps +I want to create a movie name guess app with following steps -- Use Python streamlit package +- Use Python fasthtml package - Get the movie backdrops from tmdb randomly - Show each backdrop with card component - Create a text input for the user to type in movie name diff --git a/src/movie_guess/fasthtml_app.py b/src/movie_guess/fasthtml_app.py index eadb412..3bb8b19 100644 --- a/src/movie_guess/fasthtml_app.py +++ b/src/movie_guess/fasthtml_app.py @@ -4,6 +4,9 @@ app, rt = fast_app() +# Fallback image URL when no backdrop is found +FALLBACK_IMAGE_URL = "https://placehold.co/500x281/808080/FFFFFF/png?text=No+Image" + @rt("/") def get(): @@ -62,11 +65,13 @@ def post(query: str = ""): for movie in results: backdrop_img = "" if movie["backdrop_path"]: - # TMDB image base URL img_url = f"https://image.tmdb.org/t/p/w500{movie['backdrop_path']}" - backdrop_img = Img( - src=img_url, cls="movie-backdrop", alt=f"{movie['title']} backdrop" - ) + else: + img_url = FALLBACK_IMAGE_URL + + backdrop_img = Img( + src=img_url, cls="movie-backdrop", alt=f"{movie['title']} backdrop" + ) movie_items.append( Card( diff --git a/src/movie_guess/utils/movie.py b/src/movie_guess/utils/movie.py index 487885d..52fb3c3 100644 --- a/src/movie_guess/utils/movie.py +++ b/src/movie_guess/utils/movie.py @@ -119,3 +119,21 @@ def fuzzy_search_movies( sorted_matches = sorted(fuzzy_matches, key=lambda x: x["similarity"], reverse=True) return sorted_matches[:limit] + + +def get_random_movie_with_details() -> dict: + """Get a random movie with all its details including backdrops. + + Returns: + A dictionary containing movie details including title, backdrops, etc. + """ + movie = get_random_movie() + backdrops = get_movie_backdrops(movie.id) + + return { + "id": movie.id, + "title": movie.title, + "backdrops": backdrops, + "overview": getattr(movie, "overview", "N/A"), + "release_date": getattr(movie, "release_date", "N/A"), + }