-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovieTextUI.py
337 lines (243 loc) · 8.21 KB
/
MovieTextUI.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
'''
3580: Recommendation Engine
Author: Mujtaba Ashfaq
Date: 3/23/21
This is a content based movie search engine.
'''
# Used for movie search engine
import MovieRecommendationEngine as mre
# Allow console commands
import os
'''
Manage console main menu branching for the user
'''
def textMenu():
# Hold user input
user_input = ''
formatted_user_input = 1
# Loop through menu options
while formatted_user_input > 0:
print("\n\n")
#Show menu
print("Main menu")
print("----------")
print("1. Search by title")
print("2. Search by year")
print("3. Search by genre")
print("---")
print("4. Show your selected movie list")
print("5. Show recommended movies")
print("--")
print("6. Recommendation Filter: Set minimum year")
print("7. Recommendation Filter: Set minimum rating")
print()
print("(Enter -1 to exit)")
print("Enter selection (1-5): ", end="")
# Get input
user_input = input()
# Verify input
try:
formatted_user_input = int(user_input)
except:
print("Invalid input! Try again!")
continue
# Process input
if 0 < formatted_user_input <= 3:
searchTextUI(formatted_user_input)
elif formatted_user_input == 4:
print("\n\nSelected movies")
print("----------------------")
print("Loading movies (this may take a while due to ratings data)")
print("Please wait for main menu...\n")
showSelectedMovies()
elif formatted_user_input == 5:
print("\n\nRecommended movies")
print("----------------------")
print("Note:")
print("- Title weighting is set to " + str(mre.title_weighting) + " decimal percentage")
print("- Genre weighting is set to " + str(mre.genre_weighting) + " decimal percentage")
print()
print("Loading movies (this may take a while due to ratings API call)")
print("Please wait for main menu...\n")
displayMovies(mre.calculateRecommendations(), True)
print("\n\n")
elif formatted_user_input == 6:
print("\n\nEnter min year: ", end="")
global user_min_year
try:
user_min_year = int(input())
except:
print("Invalid input! Try again!")
continue
print("\n\n")
elif formatted_user_input == 7:
print("\n\nNote: Do not choose a rating above 7 as results may take a long time to load!")
print("Enter min rating: ", end="")
global user_min_rating
try:
user_min_rating = float(input())
except:
print("Invalid input! Try again!")
continue
print("\n\n")
else:
print("Input out of range!")
if formatted_user_input == -1:
return
'''
Menu for allowing user to search for movies using the console
'''
def searchTextUI(menu_id):
user_input = ''
# For searching
while user_input != "-1":
print("\n\n")
# Clear console
cls()
# Print menu info
print("(Enter -1 to return to main menu)")
if menu_id == 1:
print("Enter the movie NAME you want to search: ", end="")
elif menu_id == 2:
print("Enter the movie YEAR you want to search: ", end="")
elif menu_id == 3:
print("Enter the movie GENRES you want to search: ", end="")
# Get user input
user_input = input()
# Go back to main menu
if user_input == "-1":
return
# Show search results
print("\n\nResults")
print("------------")
print()
results_df = ''
# Do the correct search
if menu_id == 1:
results_df = mre.searchMovieByTitle(user_input)
printTopResults(results_df)
elif menu_id == 2:
results_df = mre.searchMovieByYear(user_input)
printTopResults(results_df)
elif menu_id == 3:
results_df = mre.searchMovieByGenre(user_input)
printTopResults(results_df)
# Show menu
print()
print("Enter the index of the movie you want to add to your movie list")
print("(Enter -1 to return to main menu)")
print("(Enter out of bounds value to skip)")
print("Your input: ", end="")
# Get user input
user_input = input()
# Verify input
try:
user_input = int(user_input)
except:
print("Invalid input! Try again!")
continue
# Go to main menu
if user_input == "-1":
return
# Save users selection
mre.selectMovie(results_df, user_input)
if user_input < 1 or user_input > mre.number_of_results:
print("Out of bounds index! Selection not saved :(")
else:
print("Selection saved!")
print("\nEnter 1 to search again")
print("Enter -1 to return to main menu")
print("Your input: ", end="")
# Get user input
user_input = input()
'''
Allows clearing the console (doesn't work in pycharm!)
'''
def cls():
os.system('cls' if os.name == 'nt' else 'clear')
'''
Display the users selected movies in a clean way
'''
def showSelectedMovies():
displayMovies(getSelectedMovies(), False)
def getSelectedMovies():
# Retrieve movies
user_movies_df = mre.df.loc[(mre.df['movieId'].isin(mre.user_selection))]
return user_movies_df
'''
Display the movie search results in a clean way
'''
def printTopResults(results_df):
# Pull out the movie ids
movie_ids = results_df['movieId'].to_list()
row_num = 0
# Get movies from original dataframe
for i in movie_ids:
# Get row
movie_item = mre.og_df.loc[(mre.df['movieId'] == i)]
year = movie_item['year'].to_list()[0]
# Format for displaying
print()
print("Index: ", end="")
print(1 + row_num)
print(movie_item['title'].to_list()[0])
print(" - Year: ", end="")
print(year)
print(" - Genres: ", end="")
genres = movie_item['genre_array'].to_list()[0]
for i in range(len(genres)):
print(genres[i], end="")
if i != len(genres) - 1:
print(", ", end="")
print()
# Count number of results
row_num += 1
# End method if number of results has been reached
if row_num == mre.number_of_results:
return
print()
'''
Display the recommended movies in a clean way
'''
def displayMovies(recommend_df, limit_rows):
# Pull out the movie ids
movie_ids = recommend_df['movieId'].to_list()
row_num = 0
# Get movies from original dataframe
for i in movie_ids:
# Get row
movie_item = mre.og_df.loc[(mre.df['movieId'] == i)]
# Get more movie data from imdb
movie = mre.ia.get_movie(movie_item['imdbId'].to_list()[0])
rating = movie.get('rating')
year = movie_item['year'].to_list()[0]
# Apply user restrictions
if rating < mre.user_min_rating:
continue
if int(year) < mre.user_min_year:
continue
# Format for displaying
print(movie_item['title'].to_list()[0])
print(" - Rating: ", end="")
print(rating)
print(" - Year: ", end="")
print(year)
print(" - Genres: ", end="")
genres = movie_item['genre_array'].to_list()[0]
for i in range(len(genres)):
print(genres[i], end="")
if i != len(genres) - 1:
print(", ", end="")
print()
print(" - Plot: ", end="")
# Get the first plot and remove the author signature
print(movie.get('plot')[0].split(':')[0])
print()
# Count number of results
if limit_rows:
row_num += 1
# End method if number of results has been reached
if row_num == mre.number_of_results:
return
# Actually run menu
#textMenu()