-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_func.py
277 lines (246 loc) · 8.21 KB
/
client_func.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
import pickle
import sys
import select
from classes import *
BUFFERSIZE = 6400
def SignUp(client_socket, username, password, email, name, age, gender, status, city, institute):
#client to server
new_signup = signup("SignUp",username, password, email, name, age, gender, status, city, institute, 0)
data = pickle.dumps(new_signup)
client_socket.send(data)
#server to client
reply=client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply=client_socket.recv(BUFFERSIZE)
data=pickle.loads(reply)
if(data.flag==0):
print("Weak password")
else:
print("Signed up")
return
def Login(client_socket, username, password):
#client to server
credentials = login("Login",username, password,list(),0)
data = pickle.dumps(credentials)
client_socket.send(data)
#server to client
reply=client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply=client_socket.recv(BUFFERSIZE)
data=pickle.loads(reply)
if(data.flag==0):
print("Login failed, invalid credentials")
else:
print("Login Succesful")
print("Recent tweets from your following")
for tweet in data.tweets:
print(tweet)
return data.flag
def NewTweet(client_socket, username):
tweet_msg = input("Enter New tweet: ")
hashtags = input("Provide the hashtags related to the above tweet (separated by space): ")
tags = list(hashtags.split())
#client to server
msg = newtweet("NewTweet",tweet_msg, tags,0)
data = pickle.dumps(msg)
client_socket.send(data)
#server to client
reply=client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply=client_socket.recv(BUFFERSIZE)
data=pickle.loads(reply)
if(data.flag==0):
print("Could not Tweet, try again later")
else:
print("Tweeted")
return
def Unfollow(client_socket ,following):
#client to server
msg=unfollow("Unfollow",following,0)
data=pickle.dumps(msg)
client_socket.send(data)
#server's reply
reply=client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply=client_socket.recv(BUFFERSIZE)
data=pickle.loads(reply)
if(data.flag==1):
print(following, "unfollowed")
def DeleteFollower(client_socket ,follower):
#client to server
msg=deletefollower("DeleteFollower",follower,0)
data=pickle.dumps(msg)
client_socket.send(data)
#server's reply
reply=client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply=client_socket.recv(BUFFERSIZE)
data=pickle.loads(reply)
if(data.flag==1):
print(follower, "deleted")
def ShowAllFollowers(client_socket, username):
arr=list()
msg=showallfollowers("ShowAllFollowers",arr,0)
data=pickle.dumps(msg)
client_socket.send(data)
#server to client
reply=client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply=client_socket.recv(BUFFERSIZE)
data=pickle.loads(reply)
names=data.arr
if len(names)==0:
print("No followers")
# return 0
else:
for name in names:
print(name[0])
def Refresh(client_socket):
#client to server
tweets=list()
msg=refresh("Refresh",tweets,0)
data=pickle.dumps(msg)
client_socket.send(data)
#server to client
reply=client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply=client_socket.recv(BUFFERSIZE)
data = pickle.loads(reply)
if(data.count==0):
print("No new Tweets")
else:
response = data.tweets
for j in response:
print(j)
return
def SearchPerson(client_socket, name):
#client to server
msg = searchperson("SearchPerson",name,name,"","","","","",0)
data = pickle.dumps(msg)
client_socket.send(data)
#server to client
reply = client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply = client_socket.recv(BUFFERSIZE)
data = pickle.loads(reply)
if(len(data.name)>0 or len(data.username)>0):
print("Name: ",data.name)
print("Username: ",data.username)
print("Age: ",data.age)
print("Gender: ",data.gender)
print("Status: ",data.status)
print("City: ",data.city)
print("Education: ",data.institute)
else:
print("No such user")
def Follow(client_socket,username):
#check if username to be followed exists
msg=follow("Follow",username,username)
data=pickle.dumps(msg)
client_socket.send(data)
reply = client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply = client_socket.recv(BUFFERSIZE)
data = pickle.loads(reply)
if(data.flag==0):
print("Invalid name/username")
else: #if the username existed, then he/she was followed
print("Following ",username)
def SearchByHashtag(client_socket, hashtag):
#client to server
tweets=list()
msg=searchbyhashtag("SearchByHashtag",hashtag,tweets)
data=pickle.dumps(msg)
client_socket.send(data)
#server to client
reply = client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply = client_socket.recv(BUFFERSIZE)
data = pickle.loads(reply)
if(len(data.tweets)==0):
print("No tweets with this hashtag")
else:
for tweet in data.tweets:
print("Tweet done by: ")
print(tweet[0])
print("Tweet ID: ")
print(tweet[1])
print("Tweet: ")
print(tweet[2])
print("Following hashtags were used in this tweet")
for j in range(3,8):
if(tweet[j]!="NULL"):
print(tweet[j])
else:
break
print("\n")
def TrendingHashtags(client_socket):
#client to server
message = trendinghashtags("TrendingHashtags", list())
data=pickle.dumps(message)
client_socket.send(data)
#server to client
reply = client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply = client_socket.recv(BUFFERSIZE)
data = pickle.loads(reply)
result = data.hashtags
print("Following are the top 5 trending hashtags")
for j in result:
print(j)
def EnterChatRoom(client_socket):
#notify server
flag=0
message = enterchatroom("EnterChatRoom")
data = pickle.dumps(message)
client_socket.send(data)
while True:
sockets_list = [sys.stdin, client_socket]
read_sockets,write_socket, error_socket = select.select(sockets_list,[],[])
for socks in read_sockets:
if socks == client_socket:
message = socks.recv(2048)
print (message.decode('ascii'))
else:
message = sys.stdin.readline().strip()
client_socket.send(message.encode('ascii'))
sys.stdout.write("<You> ")
sys.stdout.write(message+'\n')
if(str(message)=="exit"):
print("exiting")
flag=1
break
sys.stdout.flush()
if flag==1:
break
sys.stdout.write("Chat room exited\n")
sys.stdout.flush()
def Retweet(client_socket, id):
#ask server to update database
msg=retweet("Retweet",id)
data=pickle.dumps(msg)
client_socket.send(data)
#get confirmation of new tweet, this comes from newtweet function
reply=client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply=client_socket.recv(BUFFERSIZE)
data=pickle.loads(reply)
if(data.flag==0):
print("Could not Tweet, try again later")
else:
#ready to take the new tweet
client_socket.send(bytes("1".encode('ascii')))
# #get the retweeted tweet and print it, this comes from retweet function
reply=client_socket.recv(BUFFERSIZE)
# while(len(reply)==0):
# reply=client_socket.recv(BUFFERSIZE)
data=pickle.loads(reply)
print("Message:\n",data.message)
print("Hashtags:")
for i in range(len(data.hashtags)):
if(data.hashtags[i]!="NULL"):
print("#"+data.hashtags[i])
def Logout(client_socket):
message = logout("Logout")
data = pickle.dumps(message)
client_socket.send(data)