-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocialnetwork.py
225 lines (179 loc) · 6.51 KB
/
socialnetwork.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
import person
import csv
def main():
personList = [ ]
# Initializes all users in the social network as objects from CSV data and logs user in on startup
personList = refreshNetwork()
# Prints master list of all user data for easier testing
for user in personList:
print(user)
currentUser = userAuthenticate(personList)
continueNetwork = "y"
while continueNetwork == "y":
print("Welcome, " + currentUser.getUsername())
print("Social Network Program")
print("1. Print Friends List")
print("2. Print My Status Updates")
print("3. Post New Status")
print("4. Print Friends Statuses")
print("5. Add Friend")
print("6. Logout (Change User)")
print("7. Add New User")
print("8. Exit Program")
opSelection = int(input("Enter your choice: "))
while opSelection < 1 or opSelection > 8:
opSelection = int(input("Enter your choice: "))
if opSelection == 1:
print(str(currentUser.getUsername() + " Friends List: " + "\n" + str(selectionSort(currentUser.getFriends()))))
elif opSelection == 2:
print(str(currentUser.getUsername() + " Status Updates: " + "\n" + str(currentUser.getStatus())))
elif opSelection == 3:
currentUser.setStatus(postNewStatus(currentUser))
saveExit(personList)
personList = refreshNetwork()
elif opSelection == 4:
printFriendsStatus(currentUser, personList)
elif opSelection == 5:
currentUser.setFriends(addFriend(currentUser, personList))
saveExit(personList)
personList = refreshNetwork()
elif opSelection == 6:
saveExit(personList)
print("Logged Out")
personList = refreshNetwork()
currentUser = userAuthenticate(personList)
# Prints new user data after login for debugging purposes
print(str(currentUser))
print("New User is: " + currentUser.getUsername())
elif opSelection == 7:
personList = addNewUser(personList)
saveExit(personList)
personList = refreshNetwork()
elif opSelection == 8:
saveExit(personList)
exit(0)
print("Operation Finished")
continueNetwork = input("Would you like to perform another operation? (Y/N)")
while continueNetwork != "y" and continueNetwork != "n":
print("Please enter either Y or N")
continueNetwork = input("Would you like to perform another operation? (Y/N)")
saveExit(personList)
# Gets current saved state of network from CSV file
def refreshNetwork():
personList = [ ]
with open('network.csv', "r") as f:
reader = csv.reader(f)
next(reader, None)
for row in reader:
status = row[2]
statusList = status.split(":")
friends = row[3]
friendsList = friends.split(":")
newPerson = person.User(row[0], row[1], statusList, friendsList)
personList.append(newPerson)
return personList
# Username/password authentication function
def userAuthenticate(personList):
correctData = False
usernameInput = str(input("Enter username: "))
passwordInput = str(input("Enter password: "))
while correctData == False:
for i in range(len(personList)):
if (personList[i].getUsername() == usernameInput) and (personList[i].getPassword() == passwordInput):
print("Welcome, " + personList[i].getUsername())
currentUser = personList[i]
correctData = True
return personList[i]
else:
correctData = False
print("Invalid User Credentials")
usernameInput = str(input("Enter username: "))
passwordInput = str(input("Enter password: "))
def selectionSort(aList):
for i in range(len(aList)):
minPos = minimumPosition(aList, i)
temp = aList[minPos]
aList[minPos] = aList[i]
aList[i] = temp
return aList
def minimumPosition(aList, start):
minPos = start
for i in range(start + 1, len(aList)):
if aList[i] < aList[minPos]:
minPos = i
return minPos
def printFriendsStatus(username, accountList):
currentUser = username
friendsList = currentUser.getFriends()
personList = accountList
for friend in friendsList:
for user in personList:
if friend == user.getUsername():
print(str(user.getUsername() + ": " + "\n" + str(user.getStatus())))
def postNewStatus(username):
statusList = username.getStatus()
newtStatus = str(input("New status: "))
statusList.append(newtStatus)
print(statusList)
return statusList
def addFriend(username, userList):
friendExists = False
newFriend = str(input("Name of friend to add: "))
friendsList = username.getFriends()
namesList = [ ]
print(type(friendsList))
for user in userList:
namesList.append(user.getUsername())
while friendExists == False:
if not (newFriend in friendsList) and (newFriend in namesList):
friendsList.append(newFriend)
print(friendsList)
friendExists = True
return friendsList
elif newFriend in friendsList:
print("Friend already in list!")
friendExists = True
return friendsList
else:
friendExists = False
print("The specified user does not exist")
newFriend = str(input("Name of friend to add: "))
return friendsList
# Experimental add new user operation
def addNewUser(userList):
newUserName = str(input("Name of user to be added: "))
newPassword = str(input("Password for new user: "))
namesList = [ ]
for user in userList:
namesList.append(user.getUsername())
if newUserName in namesList:
print("User already exists in network!")
return userList
else:
newPerson = person.User(newUserName, newPassword, ["No Posts Yet"], ["person1"])
userList.append(newPerson)
print("User Successfully Added to Network")
return userList
# Saves current state of social network to CSV file
def saveExit(userList):
with open("network.csv", "w", newline="") as outfile:
writer = csv.writer(outfile, delimiter=',')
writer.writerow(["Username"]+["Passowrd"]+["Status List(posts separated by colon)"]+["Frineds List(friends separated by colon)"])
for user in userList:
# Converts friends and status lists back into CSV read/write format
statusList = user.getStatus()
statusString = ""
for i in range(len(statusList)):
if (len(statusList) <= 1) or (i == 0):
statusString += statusList[i]
else:
statusString = statusString + ":" + statusList[i]
friendsList = user.getFriends()
friendsString = ""
for i in range(len(friendsList)):
if (len(friendsList) <= 1) or (i == 0):
friendsString += friendsList[i]
else:
friendsString = friendsString + ":" + friendsList[i]
writer.writerow([user.getUsername()]+[user.getPassword()]+[statusString]+[friendsString])
main()