""" Tool to select random movies from our to watch database """ import sqlite3 import sys from imdb import Cinemagoer ia = Cinemagoer() def create_table(): """ 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));") def add_movie(title, genre, year): """ 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)) 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 = (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: Show all movies 4: Exit ****************************\n Input: ''') while (user_input := input(main_prompt)) != "4": if user_input == "1": 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": 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()