From 46fb24d5b55bb82d4af27d211bfdf547aa299e9b Mon Sep 17 00:00:00 2001 From: jordan Date: Sat, 10 Dec 2022 23:16:57 +1100 Subject: [PATCH] Update, add all movie function --- app.py | 126 ++++++++++++++++++++++++++++++++++++++++++++++---------- tool.py | 0 2 files changed, 105 insertions(+), 21 deletions(-) delete mode 100644 tool.py diff --git a/app.py b/app.py index 9cdc60d..53470ee 100644 --- a/app.py +++ b/app.py @@ -1,54 +1,138 @@ """ Tool to select random movies from our to watch database """ import sqlite3 +import sys +from imdb import Cinemagoer -#connection = sqlite3.connect("data.db") +ia = Cinemagoer() def create_table(): - """ Function to create new table if none already exists""" + """ Function to create new table if none already exists """ with sqlite3.connect("data.db") as connection: connection.execute("CREATE TABLE IF NOT EXISTS movies (title TEXT, genre TEXT, year INT)," \ "UNIQUE(title));") - # close_connection() def add_movie(title, genre, year): - """ Function for adding movies to the db""" + """ Function for adding movies to the db """ with sqlite3.connect("data.db") as connection: connection.execute("INSERT OR IGNORE INTO movies VALUES (?, ?, ?)", (title, genre, year)) - # close_connection() + +def remove_movie(title): + """ Function for removing a movie from the database """ + with sqlite3.connect("data.db") as connection: + connection.execute(f"DELETE FROM movies WHERE title LIKE '{title}'") + +def search_movie_title(): + """ Uses Cinemagoer to search through IMDB and retrive + the appropriate entries """ + title = ia.search_movie(input('Movie: ')) + print(f"We have found {title[0]}, is this correct?\n") + correct_movie = input("Y/N: ") + try: + if correct_movie.lower() == "y": + return title[0] + if correct_movie.lower() == "n": + print("1: ", title[1]) + print("2: ", title[2]) + print("3: ", title[3]) + choice = int(input("Select from above: ")) + print(f"Adding {title[choice]}") + return title[choice] + print("Invalid Choice, Try Again") + except KeyboardInterrupt: + sys.exit(0) + except error as error: + print(error) + +def get_full_details(movie_id): + """ Uses the movieID from the search to retrieve + comprehensive movie info """ + full_movie = ia.get_movie(movie_id) + return full_movie + +def get_title(movie): + """ Extracts title from the movie """ + return movie['localized title'] + +def get_genres(movie): + """ Extracts genres from the movie """ + genres_list = [] + for genres in movie['genres']: + genres_list.append(genres) + return ', '.join(genres_list) + +def get_release_year(movie): + """ Extracts release year from the movie """ + return movie['year'] def select_random_movie(): """ Function for selecting random movie """ with sqlite3.connect("data.db") as connection: rand_mov_list = connection.execute("SELECT * FROM movies ORDER BY RANDOM() LIMIT 1;") for movie in rand_mov_list: - movie_name = (f"{movie[0]}, from {movie[2]}") - return movie_name + movie_name = (movie[0], movie[2]) + +def show_all_movies(): + """ Function to show all movies currently in the list """ + with sqlite3.connect("data.db") as connection: + full_movie_list = connection.execute("SELECT * FROM movies;") + for movie in full_movie_list: + movie_name = (movie[0], movie[2]) + print(movie_name) + + return movie_name def main(): """ This is the main function of the script""" + # create_table() print('Please enter your choice...') main_prompt = (''' **************************** 1: Enter a movie 2: Get random movie - 3: Exit - **************************** - ''') - while (user_input := input(main_prompt)) != "3": + 3: Show all movies + 4: Exit + ****************************\n + Input: ''') + while (user_input := input(main_prompt)) != "4": if user_input == "1": - print("Adding movie") - movie_title = input("Enter Title: ") - movie_genre = input("Enter Genre: ") - movie_year = input("Enter Year: ") + new_movie = search_movie_title() + #id = new_movie.movieID + new_movie_details = get_full_details(new_movie.movieID) + movie_title = get_title(new_movie_details) + movie_genre = get_genres(new_movie_details) + movie_year = get_release_year(new_movie_details) add_movie(movie_title, movie_genre, movie_year) print("Movie Added") continue if user_input == "2": - print("Selecting Movie...") - random_movie = select_random_movie() - print(f"Let's watch {random_movie}") - continue - print("Invalid option, please try again") - + re_roll = True + while re_roll: + try: + print("Selecting Movie...") + random_movie = select_random_movie() + print(f"Should we watch {random_movie[0]}, from {random_movie[1]}?\n") + choice = input(''' + **************************** + 1: Watch and Remove + 2: Re-Roll + 3: Remove and Re-Roll + **************************** + ''') + if choice == "1": + print("Enjoy!") + remove_movie(random_movie[0]) + sys.exit(0) + elif choice == "2": + print("Re_Rolling!") + continue + elif choice == "3": + print("Removing and Re-Rolling") + remove_movie(random_movie[0]) + continue + except KeyboardInterrupt: + sys.exit(0) + print("Invalid option, please try again") + if user_input == "3": + show_all_movies() main() diff --git a/tool.py b/tool.py deleted file mode 100644 index e69de29..0000000