/
Main.py
223 lines (192 loc) · 9.77 KB
/
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
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
import mysql.connector
import re
from os import listdir
# from datetime import date
import functions as api
from tables import createTables
# Intialization
dataOkay = False
while not dataOkay:
dbHost = input("Please input your host address: ")
dbUser = input("Please input your db user: ")
dbPassword = input("Please input your db password: ")
print('\nIs all of the following data that you entered okay?')
print(f'dbHost: {dbHost}; dbUser: {dbUser}; dbPassword: {dbPassword};')
dataOkay = input("\nIf the above is what you wanted, type 'Y'. Otherwise, hit any other key and the process will be restarted: ")
if dataOkay.upper() == 'Y':
dataOkay = True
if dbHost == "" and dbUser == "" and dbPassword == "": # This if case was for ease of testing can be deleted after
dbHost = "Localhost"
dbUser = "remoteuser"
dbPassword = "password"
successfullyConnected = False
try:
mydb = mysql.connector.connect(
host=dbHost,
user=dbUser,
passwd=dbPassword,
autocommit=True
)
except mysql.connector.Error as er:
print("Error while attempting to connect to server: {}".format(er))
else:
successfullyConnected = True
if successfullyConnected:
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES LIKE 'budgetBook';")
dbExists = mycursor.fetchall()
if not dbExists:
print("Creating database...")
mycursor.execute("CREATE DATABASE budgetBook;")
mycursor.execute("USE budgetBook;")
createTables(mycursor)
mycursor.execute('SET FOREIGN_KEY_CHECKS=0')
SQLInsertFilePaths = listdir('./SQLInserts')
for filePath in SQLInsertFilePaths:
print(f"Inserting data for {filePath}...")
file = open(f'./SQLInserts/{filePath}', 'r')
statements = file.readlines()
try:
for statement in statements:
mycursor.execute(statement)
except mysql.connector.Error as er:
pass
mydb.commit()
file.close()
mycursor.execute('SET FOREIGN_KEY_CHECKS=1')
print("Finished.")
else:
mycursor.execute("USE budgetBook")
# Initialize PostID if needed
mycursor.execute("SELECT * FROM Meta;")
result = mycursor.fetchall()
if len(result) == 0:
mycursor.execute("INSERT INTO Meta VALUE (44521)") # this 44521 number is to get past all the inserted data to save time
mydb.commit()
# Main Program Loop
executing = True
while executing:
loggedIn = False
UserID = ""
while not loggedIn:
loginOrRegister = input("Please login (using your userID), register, or exit: L = Login, R = Register, E = Exit: ")
if loginOrRegister.upper() == 'L':
userObject = api.login(mycursor)
if (userObject["success"]):
loggedIn = True
UserID = userObject["userID"]
break
elif loginOrRegister.upper() == 'R':
api.register(mycursor, mydb)
elif loginOrRegister.upper() == 'E':
print("Goodbye.")
executing = False
break
else:
print("That is not one of the options.")
if loggedIn:
while True:
action = input("Main Menu:\nP = Post Menu\nF = Friend Menu\nG = Group Menu\nL = Logout\nE = Exit the Application\nWhich menu do you want? ")
if action.upper() == 'E':
executing = False
break
elif action.upper() == 'L':
break
elif action.upper() == 'P':
while True:
action = input("Post Menu:\nP = Create a Post\nR = Read Unread Posts\nI = Read Post by ID\nG = Browse Posts by Group\nB = Back\nWhat would you like to do? ")
if action.upper() == 'P':
api.post(UserID, None, mycursor, mydb)
elif action.upper() == 'R' or action.upper() == 'I':
if action.upper() == 'R':
numPosts = input("How many unread posts would you like to preview? Number: ")
if not re.match("[0-9]+", numPosts):
print("You must specify a number.")
continue
print("")
listReturn = api.listUnreadPosts(UserID, numPosts, None, mycursor, mydb)
if listReturn < 0:
continue
while True:
PostID = input("Which post do you want to read? (B = Go Back) PostID: ")
print("")
if PostID.upper() == 'B':
break
readResult = api.read(PostID, UserID, mycursor, mydb, 0)
if readResult < 0:
continue
PostIDStack = []
while True:
if len(PostIDStack) == 0:
print("Menu for PostID "+PostID+": V = Upvote/Downvote, C = Comment, P = Preview comments, B = Go Back")
else:
print("Menu for CommentID "+PostID+": V = Upvote/Downvote, C = Comment, P = Preview Comments, B = Go Back")
postaction = input("What do you want to do? ")
if postaction.upper() == 'V':
Reaction = input("U = Upvote, D = Downvote: ")
api.react(UserID, PostID, Reaction, len(PostIDStack), mycursor, mydb)
elif postaction.upper() == 'C':
api.post(UserID, PostID, mycursor, mydb)
elif postaction.upper() == 'P':
numReplies = input("How many comments do you want to preview? Number: ")
if not re.match("[0-9]+", numReplies):
print("You must specify a number.")
continue
print("")
if action.upper() == 'I':
listReturn = api.listComments(UserID, numReplies, PostID, mycursor, mydb)
else:
listReturn = api.listUnreadPosts(UserID, numReplies, PostID, mycursor, mydb)
if listReturn < 0:
continue
PostIDStack.append(PostID)
PostID = input("Which comment do you want to read? (B = Go Back) CommentID: ")
if PostID.upper() == 'B':
PostID = PostIDStack.pop()
else:
api.read(PostID, UserID, mycursor, mydb, 1)
elif postaction.upper() == 'B':
if len(PostIDStack) == 0:
break
else:
PostID = PostIDStack.pop()
else:
print("That is not one of the options.")
elif action.upper() == 'G':
api.browsePostsInGroup(UserID, mycursor, mydb)
elif action.upper() == 'B':
break
else:
print("That is not one of the options.")
elif action.upper() == 'F':
while True:
print("Friend Menu:\nF = Add a Friend\nU = Unfollow a Friend\nR = Refollow a Friend\nB = Back")
action = input("What would you like to do? ")
if action.upper() == 'F':
api.friend(UserID, mycursor, mydb)
elif action.upper() == 'U':
api.unfollow(UserID, mycursor, mydb)
elif action.upper() == 'R':
api.follow(UserID, mycursor, mydb)
elif action.upper() == 'B':
break
else:
print("That is not one of the options.")
elif action.upper() == 'G':
while True:
print("Group Menu:\nJ = Join Group\nC = Create Group\nB = Back")
action = input("What would you like to do? ")
if action.upper() == 'J':
api.joinGroup(UserID, mycursor, mydb)
elif action.upper() == 'C':
api.createGroup(UserID, mycursor, mydb)
elif action.upper() == 'B':
break
else:
print("That is not one of the options.")
# TODO: Add all commands, possibly a hierarchy for some
else:
print("That is not one of the options.")
print("Goodbye!")
mycursor.close()
mydb.close()