repl.it
@tecladocode/

mike-menu-functions

Python

movie listing with menu functions instead of if elif chain

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#imports
from bs4 import BeautifulSoup
import requests
import re
import lxml
from art import *

movie_db = []

#Movie Class
class Movie:
    def __init__(self, new_title, new_director, new_year):
        self.title = new_title
        self.director = new_director
        self.year = new_year

    def display_movie(self):
        print(f'{self.title:^20}\n{self.director:^20}\n{self.year:^20}')

    def search_term(self):
        data = (self.title, self.director, self.year)
        return [(e.lower()).split(" ") for e in data]


choices = {
    'a': add_movie,
    'r': result,
    'v': view_movies,
    'm': menu,
    's': search,
    'q': quit,
    'h': help_menu,
    'p': pagination,
    'c': clear_screen
}

#main menu
menu_prompt = '''
 *************************************
 *    Here are your menu options     *
 *************************************
 * V: View movies                    *
 * A: Add a movie                    *
 * S: Search for a movie             *
 * Q: Quit                           *
 * C: Clear screen                   *
 * M: Menu                           *
 * R: Make a selection               *
 * H: Help                           *
 * P: Pagination toggle              *
 *************************************
 '''


 def menu():
    print(menu_prompt)
    selection = input("Please make a selection: ").lower()
    
    while selection != "q":
        choice(selection, False)
        election = input("Please make a selection: ").lower()


def add_new_movie():
    name = input("Please enter the name of the movie: ")
    year = input("Please enter the year of the movie: ")
    director = input("Please enter the director of the movie: ")
    return Movie(name, director, year)


def display_movies(movies, pagination):
    pag_count = 0
    for movie in movies:
        pag_count += 1
        print("*" * 20)
        movie.display_movie()
        print("*" * 20, "\n")
        if pagination and pag_count > 3:
            retract = input("Press enter or P to break pagination: \n")
            if retract:
                pagination = False
            pag_count = 0


def search_movies(search_term, movies, pagination, count=0):
    found_movies = [movie for movie in movies if search_term in [e for e in movie.search_term() for e in e]]
    if found_movies:
        display_movies(found_movies, pagination)
    if count < 2 and not found_movies:
        search_term = input("Term not found Please Try again: ")
        search_movies(search_term, movies, pagination, count + 1)



def pagination(pagination = False,  *args):
    if not pagination:
        pagination = True
        print("pagination is on")
        result(pagination)
    elif pagination:
        pagination = False
        print("Pagination is off")
        result(pagination)


def add_movie(*args):
    movie_db.append(add_new_movie())
    input("Movie added successfully press enter to continue... ")


def view_movies(pagination=False, *args):
    display_movies(movie_db, pagination)


def search(pagination=False, *args):
    search_term = input("Please enter a search term: ")
    search_movies(search_term.lower(), movie_db, pagination)


def clear_screen():
    print("\n" * 100)


def help_menu(*args):
    print("press M to show menu: ")
    print("Press C to clear screen: ")
    print("Press Q to quit: ")


def quit(*args):
    question = input("Are you sure you want to quit")


def choice(selection, *args):
    print("*" * 20)
    choices[selection]()

# Variable to hold webcraping
url = 'http://www.imdb.com/chart/top'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
movies = soup.select('td.titleColumn')
crew = [(a.attrs.get('title')).split(" ") for a in soup.select('td.titleColumn a')]

#data from imdb
imdb = []

#Loop to populate imdb
for index in range(0, len(movies)):
    movie_string = movies[index].get_text()
    movie = (' '.join(movie_string.split()).replace('.', ''))
    movie_title = movie[len(str(index)) + 1:-7]
    year = str(re.search('\((.*?)\)', movie_string).group(1))
    data = {"movie_title": movie_title,
            "year": year,
            "director": [e for e in [crew[index][:2]]]}
    imdb.append(data)

#append imdb info to movie.db
for item in imdb:
    movie_db.append(Movie(item['movie_title'], " ".join(item['director'][0]), item['year']))

#Fancey menu heading
print("\n" * 2, "*" * 39)
tprint("  MOVIES\n by  Mike", font="cybermedium")
print("*" * 39, "\n" * 2)

#Start of app here on the bottom lonely by itself
menu()