-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA0.py
109 lines (96 loc) · 3.94 KB
/
A0.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
import json
import os
import re
DATALOC = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
def recurse_dir_search(filetarget: str, current_directory: str = DATALOC) -> str:
"""This is just a custom function for recursively searching the entire current directory
for the inputed file, including searching through all subfolders.
Reused from my other projects.
"""
validextension = re.compile(r"\.[a-zA-Z]{2,4}$")
path = current_directory
dirlist = os.listdir(path)
for filename in dirlist:
if filename == filetarget:
path = os.path.join(current_directory, filetarget)
return path
elif not validextension.search(filename):
try:
return recurse_dir_search(filetarget, os.path.join(path, filename))
except FileNotFoundError:
continue
raise (FileNotFoundError)
if __name__ == "__main__":
userdata = {}
datachanged = False
try:
fpath = recurse_dir_search("userdata.json")
with open(fpath, "rb") as f:
userdata = json.loads(f.read().decode())
except FileNotFoundError:
userdata = {}
firstname = input("Please enter your first name: ")
lastname = input("Please enter your last name: ")
if f"{firstname}{lastname}" in userdata:
print(
"\n".join(
f"""
{movie['title']}:
\tDirector: {movie['director']}
\tRelease Year: {movie['releaseyear']}
\tIMDB Rating: {movie['imdbrating']}
\tRotten Tomatoes Rating: {movie['tomatoesrating']}
\tMPAA Rating: {movie['mpaarating']}"""
for movie in userdata[f"{firstname}{lastname}"]["movies"]
)
)
else:
print("Please input your top 5 favorite movies in descending order.")
userdata[f"{firstname}{lastname}"] = {"movies": []}
for i in range(5):
moviedata = {
"title": "",
"director": "",
"releaseyear": None,
"imdbrating": None,
"tomatoesrating": None,
"mpaarating": "",
}
moviedata["title"] = input(f"Movie #{i+1} Title: ")
moviedata["director"] = input(f"Movie #{i+1} Director: ")
while not moviedata["releaseyear"]:
try:
moviedata["releaseyear"] = int(
input(f"Movie #{i+1} Release Year: ")
)
except ValueError:
print("Please enter a valid year.")
while not moviedata["imdbrating"]:
try:
rating = int(input(f"Movie #{i+1} IMDB Rating: "))
if rating < 0 or rating > 5:
raise ValueError
moviedata["imdbrating"] = rating
except ValueError:
print("Please enter a valid rating.")
while not moviedata["tomatoesrating"]:
try:
rating = int(input(f"Movie #{i+1} Tomatoes Rating: "))
if rating < 0 or rating > 5:
raise ValueError
moviedata["tomatoesrating"] = rating
except ValueError:
print("Please enter a valid rating.")
moviedata["mpaarating"] = input(
f"Movie #{i+1} MPAA Rating (eg: PG, PG-13, R, etc.): "
)
userdata[f"{firstname}{lastname}"]["movies"].append(moviedata)
datachanged = True
if datachanged:
try:
fpath = recurse_dir_search("userdata.json")
with open(fpath, "wb") as f:
f.write(json.dumps(userdata, indent=4).encode())
except FileNotFoundError:
with open(os.path.join(DATALOC, "userdata.json"), "wb") as f:
f.write(json.dumps(userdata, indent=4).encode())