76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
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 |