feat: Add helper functions for PlaceholderEntity
This commit is contained in:
parent
7da19cb3e0
commit
d17de3ce75
76
helpers/placeholderentity_helpers.py
Normal file
76
helpers/placeholderentity_helpers.py
Normal file
@ -0,0 +1,76 @@
|
||||
from typing import List, Optional
|
||||
from datetime import datetime
|
||||
|
||||
def get_movie_by_id(movie_id: int, movies: List[dict]) -> Optional[dict]:
|
||||
"""
|
||||
Get a movie by its ID.
|
||||
|
||||
Args:
|
||||
movie_id (int): The ID of the movie to retrieve.
|
||||
movies (List[dict]): The list of movies.
|
||||
|
||||
Returns:
|
||||
Optional[dict]: The movie dictionary if found, None otherwise.
|
||||
"""
|
||||
for movie in movies:
|
||||
if movie['id'] == movie_id:
|
||||
return movie
|
||||
return None
|
||||
|
||||
def get_movies_by_title(title: str, movies: List[dict]) -> List[dict]:
|
||||
"""
|
||||
Get movies by their title.
|
||||
|
||||
Args:
|
||||
title (str): The title to search for.
|
||||
movies (List[dict]): The list of movies.
|
||||
|
||||
Returns:
|
||||
List[dict]: A list of movie dictionaries that match the title.
|
||||
"""
|
||||
return [movie for movie in movies if title.lower() in movie['title'].lower()]
|
||||
|
||||
def get_movies_by_year(year: int, movies: List[dict]) -> List[dict]:
|
||||
"""
|
||||
Get movies by their release year.
|
||||
|
||||
Args:
|
||||
year (int): The release year to search for.
|
||||
movies (List[dict]): The list of movies.
|
||||
|
||||
Returns:
|
||||
List[dict]: A list of movie dictionaries released in the given year.
|
||||
"""
|
||||
return [movie for movie in movies if movie['release_year'] == year]
|
||||
|
||||
def get_movies_by_genre(genre: str, movies: List[dict]) -> List[dict]:
|
||||
"""
|
||||
Get movies by their genre.
|
||||
|
||||
Args:
|
||||
genre (str): The genre to search for.
|
||||
movies (List[dict]): The list of movies.
|
||||
|
||||
Returns:
|
||||
List[dict]: A list of movie dictionaries that match the genre.
|
||||
"""
|
||||
return [movie for movie in movies if genre.lower() in [g.lower() for g in movie['genres']]]
|
||||
|
||||
def get_recently_added_movies(movies: List[dict], days: int = 30) -> List[dict]:
|
||||
"""
|
||||
Get recently added movies within a specified number of days.
|
||||
|
||||
Args:
|
||||
movies (List[dict]): The list of movies.
|
||||
days (int): The number of days to consider as recently added (default: 30).
|
||||
|
||||
Returns:
|
||||
List[dict]: A list of movie dictionaries added within the specified number of days.
|
||||
"""
|
||||
today = datetime.now().date()
|
||||
recently_added = []
|
||||
for movie in movies:
|
||||
added_date = datetime.strptime(movie['added_date'], '%Y-%m-%d').date()
|
||||
if (today - added_date).days <= days:
|
||||
recently_added.append(movie)
|
||||
return recently_added
|
Loading…
x
Reference in New Issue
Block a user