Update, add all movie function

This commit is contained in:
jordan 2022-12-10 23:16:57 +11:00
parent e25466aa77
commit 46fb24d5b5
2 changed files with 105 additions and 21 deletions

126
app.py
View File

@ -1,54 +1,138 @@
""" Tool to select random movies from our to watch database """ """ Tool to select random movies from our to watch database """
import sqlite3 import sqlite3
import sys
from imdb import Cinemagoer
#connection = sqlite3.connect("data.db") ia = Cinemagoer()
def create_table(): 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: with sqlite3.connect("data.db") as connection:
connection.execute("CREATE TABLE IF NOT EXISTS movies (title TEXT, genre TEXT, year INT)," \ connection.execute("CREATE TABLE IF NOT EXISTS movies (title TEXT, genre TEXT, year INT)," \
"UNIQUE(title));") "UNIQUE(title));")
# close_connection()
def add_movie(title, genre, year): 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: with sqlite3.connect("data.db") as connection:
connection.execute("INSERT OR IGNORE INTO movies VALUES (?, ?, ?)", (title, genre, year)) 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(): def select_random_movie():
""" Function for selecting random movie """ """ Function for selecting random movie """
with sqlite3.connect("data.db") as connection: with sqlite3.connect("data.db") as connection:
rand_mov_list = connection.execute("SELECT * FROM movies ORDER BY RANDOM() LIMIT 1;") rand_mov_list = connection.execute("SELECT * FROM movies ORDER BY RANDOM() LIMIT 1;")
for movie in rand_mov_list: for movie in rand_mov_list:
movie_name = (f"{movie[0]}, from {movie[2]}") movie_name = (movie[0], movie[2])
return movie_name
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(): def main():
""" This is the main function of the script""" """ This is the main function of the script"""
# create_table()
print('Please enter your choice...') print('Please enter your choice...')
main_prompt = (''' main_prompt = ('''
**************************** ****************************
1: Enter a movie 1: Enter a movie
2: Get random movie 2: Get random movie
3: Exit 3: Show all movies
**************************** 4: Exit
''') ****************************\n
while (user_input := input(main_prompt)) != "3": Input: ''')
while (user_input := input(main_prompt)) != "4":
if user_input == "1": if user_input == "1":
print("Adding movie") new_movie = search_movie_title()
movie_title = input("Enter Title: ") #id = new_movie.movieID
movie_genre = input("Enter Genre: ") new_movie_details = get_full_details(new_movie.movieID)
movie_year = input("Enter Year: ") 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) add_movie(movie_title, movie_genre, movie_year)
print("Movie Added") print("Movie Added")
continue continue
if user_input == "2": if user_input == "2":
print("Selecting Movie...") re_roll = True
random_movie = select_random_movie() while re_roll:
print(f"Let's watch {random_movie}") try:
continue print("Selecting Movie...")
print("Invalid option, please try again") 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() main()

View File