· 8 years ago · Mar 31, 2018, 10:04 PM
1import random, sys, smtplib
2from time import sleep
3from string import Template
4from email.mime.multipart import MIMEMultipart
5from email.mime.text import MIMEText # Imports all libraries needed for email sending and
6
7user_info = []
8
9user_file = open("user_info.txt") # opens file containing all the users information
10
11for line in user_file:
12 line = line.strip("\n").split(",") # Reads a file containing all the users information and appends it to a list
13 user_info.append(line)
14
15user_file.close()
16
17def send_email(name, password, subject, email, username): # Function to send email to user if they have forgotten their login information
18 account_email = 'ocrtunesteam@gmail.com'
19 account_password = 'OCRadmin'
20 s = smtplib.SMTP(host='smtp.gmail.com', port=587) # Starts smtp on gmail port
21 s.starttls()
22 s.login(account_email, account_password) # Logs into gmail with setup account for ocrtunes
23 msg = MIMEMultipart()
24
25 if len(username) > 0:
26 message = "Hello" +name+"This email is to send your username and password.\n\nusername:" + username + "\npassword:" + password # Creates message to send to user
27 else:
28 message = "Hello " + name + " This email is to send your password.\n\npassword: " + password
29
30 msg['From']=account_email # Basic information for email
31 msg['To']= email
32 msg['Subject']=subject
33 msg.attach(MIMEText(message, "plain"))
34
35 s.send_message(msg) # Sends email to user
36 s.quit()
37
38def main_menu(user): # Function containing main menu of program taking the logged in user as a parameter
39
40 print("\n-----------Main Menu-----------\n")
41
42 user_playlists =[]
43
44 try:
45 user_playlists_file = open(user[1] + ".txt","r") # Trys to open the user's playlist file and does returns false if file is not found
46 playlists_found = True
47 except:
48 playlists_found = False
49
50 sleep(1)
51 print("Welcome", user[1]) # Welcomes user
52 sleep(1)
53
54 while True:
55
56 main_menu = input("""\nChoose an option:
57 1 --> View Playlists
58 2 --> Create Playlists
59 q --> Terminate Program\n\n-""") # Menu for user to navigate through main menu with
60
61 if main_menu == "1":
62
63 if playlists_found:
64
65 for line in user_playlists_file:
66 line = line.strip("\n").split(":") # appends information from user's playlist file to a list
67 user_playlists.append(line)
68 user_playlists_file.close()
69
70 print("\nWhich playlist would you like to view:")
71 sleep(0)
72
73 for lis in user_playlists:
74 print(" --> "+lis[0]) # Lists all the playlists from playlist list
75
76 while True:
77
78 playlist_choice = input("\n-")
79 print("\n")
80 choice_found = False # Allows user to choose a playlist
81
82 for lis in user_playlists:
83 if playlist_choice == lis[0]:
84 choice_found = True
85 break
86
87 chosen_playlist = []
88
89 if choice_found == True:
90
91 for list2 in lis:
92 chosen_playlist.append(list2.split(","))
93 chosen_playlist.remove(chosen_playlist[0])
94
95 for lis in chosen_playlist:
96 print(lis[0] +" --- "+ lis[1] +" --- "+ lis[2])
97
98 sleep(1)
99 print("\nWhat would you like to do next...")
100 sleep(1)
101 while True:
102 edit_playlist = input("""\nChoose an option:
103 1 --> Edit playlist
104 2 --> Delete playlist
105 3 --> Return to menu\n\n-""")
106
107 if edit_playlist == "1":
108 print("111") #Work Here
109
110 elif edit_playlist == "2":
111 while True:
112 reassurance = input("Are you sure you would like to delete this playlist") #Work Here
113
114 elif edit_playlist == "3":
115 break
116
117 else:
118 print("Invalid input, try again\n")
119 break
120
121 else:
122 print("Invalid input, Try again")
123
124 else:
125 sleep(1)
126 print("No playlists found please create one first")
127 sleep(1)
128
129 elif main_menu == "2":
130
131 while True:
132
133 found = False
134 new_playlist_name = input("Please enter the name of the name of the playlist you would like to make: ")
135
136 for playlist in user_playlists:
137 if playlist[0] == new_playlist_name:
138 found = True
139 print("\nPlaylist name taken, Please choose a new name\n")
140 sleep(1)
141 break
142
143 if found == False:
144 break
145
146 elif main_menu == "q":
147
148 print("Program quit, shutting down")
149 sleep(2)
150 sys.exit()
151 else:
152 print("Invalid input, try again\n")
153
154print("Welcome to OCR Tunes")
155
156while True:
157 print("\n-----------Login Menu-----------\n")
158
159 start_menu = input("""Choose an option:
160 1 --> Login
161 2 --> Register
162 q --> Terminate program\n\n-""")
163 if start_menu == "1":
164 print("\n-----------Login-----------\n")
165 counter = 2
166 found = False
167
168 while counter > -1:
169
170 username = input("Input your username/email: ")
171 password = input("Input your password: ")
172 for user in user_info:
173
174 if username == user[1] and password == user[2] :
175 print("\nSuccess!")
176 main_menu(user)
177
178 elif username == user[4] and password == user[2] :
179 print("\nSuccess!")
180 main_menu(user)
181
182 print("\nUsername or password Incorrect, you have", counter,"attempt(s) left\n")
183 counter -= 1
184
185 print("Incorrect info inputted 3 times\n")
186 sleep(2)
187
188 while True:
189 forgotten = input("""If you have forgotten your username or password:
190 1 --> Have an email sent with password (requires username used with account set up)
191 2 --> Have an email sent with username and password (requires email address used with account set up)
192 q --> Terminate program\n\n-""")
193
194 if forgotten == "1":
195 forgotten_password = input("What is your username: ")
196 found = False
197 for user in user_info:
198
199 if forgotten_password == user[1]:
200 forgotten_password = user[4]
201 found = True
202 break
203
204 if found == True:
205 subject = "Forgotten password"
206 send_email(user[0], user[2], subject, forgotten_password, "")
207 print("\nEmail sent")
208 break
209
210 else:
211 print("Username not found\n")
212 sleep(1)
213
214 elif forgotten == "2":
215 forgotten_info = input("What is your email: ")
216 found = False
217 for user in user_info:
218
219 if forgotten_info == user[4]:
220 found = True
221 break
222
223 if found == True:
224 subject = "Forgotten information"
225 send_email(user[0], user[2], subject, forgotten_info, user[1])
226 print("\nEmail sent")
227
228 else:
229
230 print("Email address not associated with any account\n")
231 sleep(1)
232
233 elif forgotten == "q":
234 print("Program shutting down")
235 break
236
237 else:
238
239 print("Invalid input\n")
240
241 sleep(3)
242 sys.exit()
243
244 elif start_menu == "2":
245
246 print("\n-----------Registration-----------\n")
247
248 while True:
249
250 name = input("Input your name: ")
251
252 if name.isalpha():
253 break
254
255 else:
256
257 print("name must contain characters, try again\n")
258
259 while True:
260
261 username = input("Create a username: ")
262
263 if not username.strip():
264
265 print("Username must contain characters, try again\n")
266
267 else:
268
269 found = False
270 for user in user_info:
271
272 if username == user[1]:
273
274 print("Username already taken, please choose another\n")
275 found = True
276 break
277
278 if found == False:
279 break
280
281 while True:
282 password = input("Create a password (Password must contain at least 1 letter, 1 capital and 7 characters): ")
283
284 if (any(x.isupper() for x in password) and any(x.isdigit() for x in password) and len(password) >= 7):
285 break
286
287 else:
288 print("Password did not meet requirements, please try again\n")
289
290 while True:
291 gender = input("Specify your gender(Male/Female/Other): ")
292
293 if gender.title() == "Male" or gender.title() == "Female" or gender.title() == "Other":
294 break
295
296 else:
297 print("Invalid input\n")
298
299 while True:
300 email = input("Input email address (Must be Gmail): ")
301
302 if email[email.lower().find("@"):] == "@gmail.com" and len(email[:email.find("@")])>1:
303 found = False
304
305 for user in user_info:
306
307 if email == user[4]:
308 print("This email has already been registered with an account, please choose another\n")
309 found = True
310 break
311
312 if found == False:
313 break
314
315 else:
316 print("Not a valid Gmail, please try again\n")
317
318 user = [name, username, password, gender, email.lower()]
319 user_info.append(user)
320 user_file = open("user_info.txt","w")
321
322 for lis in user_info:
323 user_file.write(",".join(lis)+"\n")
324 user_file.close()
325 main_menu(user)
326
327 elif start_menu == "q":
328 print("Program quit, shutting down")
329 sleep(2)
330 sys.exit()
331 else:
332 print("Invalid input, try again\n")