-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
284 lines (248 loc) · 10.9 KB
/
server.py
File metadata and controls
284 lines (248 loc) · 10.9 KB
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
278
279
280
281
282
283
284
import setup_postgres
import places_api as P
import requests
def get_all_users_list():
code = "SELECT * FROM users"
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
cur.execute(code)
return_list = [dict(x) for x in cur.fetchall()]
for user_dict in return_list:
# if user_dict["user_location"]:
# user_technical_location = P.place_list(user_dict['user_location'])[0]
# user_dict["lat"] = user_technical_location.lat
# user_dict["long"] = user_technical_location.long
if user_dict["user_events"] not in [None, ''] and int(user_dict["user_events"]) > 0:
cur.execute("SELECT * FROM events WHERE event_no = %s", (user_dict["user_events"],))
event = cur.fetchall()
user_dict["user_events"] = dict(event[0])
if not user_dict["user_small_photo"]:
user_dict["user_small_photo"] = "https://cdn140.picsart.com/297361716279211.png"
user_dict["user_large_photo"] = "https://cdn140.picsart.com/297361716279211.png"
if not user_dict['user_ip']: user_dict['user_ip'] = '';
if not user_dict['user_location']: user_dict['user_location'] = '';
if not user_dict['user_bio']: user_dict['user_bio'] = '';
if not user_dict['user_extra_bio']: user_dict['user_extra_bio'] = '';
if not user_dict['user_friends']: user_dict['user_friends'] = '';
if not user_dict['user_events']: user_dict['user_events'] = '';
return return_list
ALL_USERS_LIST = get_all_users_list()
def get_all_users():
return_dict = dict()
for user_dict in ALL_USERS_LIST:
return_dict[user_dict["username"]] = user_dict
return return_dict
ALL_USERS = get_all_users()
def get_posts():
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
cur.execute("SELECT * FROM posts")
return_list = [dict(x) for x in cur.fetchall()]
for post_dict in return_list:
poster_username = post_dict["post_user"]
post_dict["post_user"] = ALL_USERS[poster_username]
post_dict['post_text'] = post_dict['post_text'].replace('$COM',',')
if post_dict['post_media']:
post_dict['has_media'] = True
else:
post_dict['has_media'] = False
return return_list
def get_events(username):
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
print("connected")
cur.execute("SELECT * FROM events")
return_list = [dict(x) for x in cur.fetchall()]
for post_dict in return_list:
poster_username = post_dict["user_posted"]
post_dict["user_posted"] = ALL_USERS[poster_username]
post_dict['event_description'] = post_dict['event_description'].replace('$COM',',')
if post_dict['event_media']:
post_dict['has_media'] = True
else:
post_dict['has_media'] = False
return return_list
def get_friends(username):
with setup_postgres.connect() as connect:
if connect:
return_list = []
conn, cur = connect
user_friends = ALL_USERS[username]["user_friends"]
for usern in ALL_USERS:
if usern in user_friends:
return_list.append(ALL_USERS[usern].copy())
l = [x["username"] for x in return_list]
print("get_friends will return",l,"for",username)
return return_list
def get_non_friends(username):
with setup_postgres.connect() as connect:
if connect:
return_list = []
conn, cur = connect
user_friends = ALL_USERS[username]["user_friends"]
for usern in ALL_USERS:
if not(usern in user_friends) and usern != username:
return_list.append(ALL_USERS[usern].copy())
l = [x["username"] for x in return_list]
print("get_non_friends will return",l,"for",username)
return return_list
def get_posts_from_friends(username):
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
user_friends = ALL_USERS[username]["user_friends"]
cur.execute("SELECT * FROM posts WHERE (position(post_user in %s)>0) or post_user = %s", (user_friends,username)) #check if post_user is a substring of user_friends
return_list = [dict(x) for x in cur.fetchall()]
print("get_posts_from_friends will return",return_list,"for",username)
for post_dict in return_list:
poster_username = post_dict["post_user"]
post_dict["post_user"] = ALL_USERS[poster_username]
post_dict["post_text"] = post_dict["post_text"].replace('$COM',',')
if post_dict['post_media']:
post_dict['has_media'] = True
else:
post_dict['has_media'] = False
return return_list
def search_user(query):
"""Should update this function, and all functions to accomodate dynamic user list"""
#cur.execute("SELECT * FROM users WHERE position(%s, username)>0 || position(%s, user_name)>0", (query,))
return_list = []
for username in ALL_USERS:
if query in username or query in ALL_USERS[username]["user_name"]:
return_list.append(ALL_USERS[username])
return return_list
def update_location(device_lat, device_long, username):
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
code = "UPDATE users SET device_lat = %s, device_long = %s WHERE username = %s"
cur.execute(code, (device_lat, device_long, username))
cur.execute("SELECT * FROM users")
return_list = [dict(x) for x in cur.fetchall()]
for user_dict in return_list:
# if user_dict["user_location"]:
# ...
# user_technical_location = P.place_list(user_dict['user_location'])[0]
# user_dict["lat"] = user_technical_location.lat
# user_dict["long"] = user_technical_location.long
if user_dict["user_events"] not in [None, ''] and int(user_dict["user_events"]) > 0:
cur.execute("SELECT * FROM events WHERE event_no = %s", (user_dict["user_events"],))
event = cur.fetchall()
user_dict["user_events"] = dict(event[0])
if not user_dict['user_ip']: user_dict['user_ip'] = '';
if not user_dict['user_location']: user_dict['user_location'] = '';
if not user_dict['user_bio']: user_dict['user_bio'] = '';
if not user_dict['user_extra_bio']: user_dict['user_extra_bio'] = '';
if not user_dict['user_friends']: user_dict['user_friends'] = '';
if not user_dict['user_events']: user_dict['user_events'] = '';
return return_list
def add_user(username,user_name,*args):
num_args = len(args)
num_empty = 8 - num_args
l = ['' for i in range(num_empty)]
assert all([',' not in username,
',' not in user_name,
'\u2019' not in username,
'\u2018' not in username])
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
insert_script = """INSERT INTO users (username, user_name, user_ip, user_location, user_bio,
user_extra_bio, user_events, user_small_photo, user_large_photo, user_friends) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
l2 = [username,user_name,*args]
l2.extend(l)
cur.execute(insert_script,l2)
update_users()
def add_post(username, kwargs):
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
params = post_text, post_event, post_time, post_user, post_media, post_tags = (kwargs['text'], 0,
kwargs['date'][:10]+' '+kwargs['time'],
username, kwargs['photo'], '')
insert_script = """INSERT INTO posts (post_text, post_event, post_time,
post_user, post_media, post_tags) VALUES (%s, %s, %s, %s, %s, %s)"""
cur.execute(insert_script, params)
def add_event(username, kwargs):
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
params = event_name, event_descr, event_location, user_posted, from_time_and_date, to_time_and_date, event_media = (kwargs['event_name'], kwargs['event_text'], kwargs['event_location'], username,
kwargs['from_date']+' '+kwargs['from_time'], kwargs['to_date']+' '+kwargs['to_time'], kwargs['event_media'])
insert_script = """INSERT INTO events (event_name, event_description, event_location,
user_posted, event_date_and_time, event_ending_date_and_time, event_media) VALUES (%s, %s, %s, %s, %s, %s, %s)"""
cur.execute(insert_script, params)
def edit_profile_pic(username, kwargs):
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
code = "UPDATE users SET user_small_photo = %s, user_large_photo = %s WHERE username = %s";
cur.execute(code, (kwargs['pic'], kwargs['pic'], username))
def update_users():
global ALL_USERS_LIST, ALL_USERS
ALL_USERS_LIST = get_all_users_list()
ALL_USERS = get_all_users()
def update_location_preference(username,status):
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
code = "UPDATE users SET location_pref0priv1frien2pub = %s WHERE username = %s";
cur.execute(code, (status, username))
def get_location_preference(username):
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
code = "SELECT location_pref0priv1frien2pub FROM users WHERE username = %s"
cur.execute(code, (username,))
status = cur.fetchall()[0]['location_pref0priv1frien2pub']
return status
def send_default_message(string, username, other_username):
chat.send_message(string,username,other_username)
class chat:
def add_user(username,user_name,*args):
url = "https://api.chatengine.io/users/"
l = user_name.split()
payload = {"username":username, "first_name":l[0], "last_name":l[1], "secret":"123456-dummy"}
headers = {
'PRIVATE-KEY': 'PKEY'
}
response = requests.post(url, headers=headers, data=payload)
print("added user to chat", response.status_code)
return response.status_code
def update_friends(username):
url = "https://api.chatengine.io/chats/"
resp = requests.get(url, data={}, headers={"Project-ID":"PID",
"User-name":username, "User-Secret":"123456-dummy"})
js = resp.json()
print(f"got user friends for {username}", resp.status_code)
new_friends = []
for chat in js:
p1, p2 = chat["people"][0], chat["people"][1]
if p1["person"]["username"] == username:
other_username = p2["person"]["username"]
else:
other_username = p1["person"]["username"]
new_friends.append(other_username)
update_script = "UPDATE users SET user_friends = %s WHERE username = %s"
get_script = "SELECT user_friends from users WHERE username = %s"
with setup_postgres.connect() as connect:
if connect:
conn, cur = connect
cur.execute(get_script, (username,))
current_friends = cur.fetchall()[0]["user_friends"]
if set(new_friends) != set(current_friends.split()):
new_friends.append(current_friends)
all_friends = ' '.join(new_friends)
cur.execute(update_script, (all_friends, username))
print(f"Updated friends of {username}")
return 0
def send_message(string,username,other_username):
url = f"https://api.chatengine.io/chats/{other_username}/messages/"
headers = {"Project-ID":"PID","User-Name":username,"User-Secret":"123456-dummy"}
resp = requests.post(url,data={"text":string,headers=headers})
return resp.status_code
#conn.commit()
#add_user("mike1","Mike Mike","","New Jersey")