-
Notifications
You must be signed in to change notification settings - Fork 0
/
openseed_connections.py
executable file
·287 lines (252 loc) · 9.22 KB
/
openseed_connections.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
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/python
import sys
import mysql.connector
import hashlib
import json
import base64
import urllib.parse
sys.path.append("..")
from hive import hive
import openseed_account as Account
import openseed_setup as Settings
import openseed_chat as Chat
import openseed_hive as Hive
settings = Settings.get_settings()
thenodes = ['anyx.io','api.steem.house','hive.anyx.io','steemd.minnowsupportproject.org','steemd.privex.io']
h = hive.Hive(nodes=thenodes)
def get_openseed_connections(account,external = False):
connections = '{"connections":"none"}'
ac = 0
accounts = ""
openseed = mysql.connector.connect(
host = "localhost",
user = settings["dbuser"],
password = settings["dbpassword"],
database = "openseed"
)
request_search = openseed.cursor()
search1 = "SELECT userid2,response FROM `connections` WHERE userid1 = %s AND response != 0"
search2 = "SELECT userid1,response FROM `connections` WHERE userid2 = %s AND response != 0"
vals = (account,)
request_search.execute(search1,vals)
exists1 = request_search.fetchall()
request_search.execute(search2,vals)
exists2 = request_search.fetchall()
if len(exists1) != 0:
ac += len(exists1)
for u in exists1:
cname = str(u[0])
if accounts == "":
accounts = '{"username":"'+str(cname)+'","linked":'+str(u[1])+','+str(Account.get_profile(str(cname)))+'}'
else:
accounts = accounts+',{"username":"'+str(cname)+'","linked":'+str(u[1])+','+str(Account.get_profile(str(cname)))+'}'
if len(exists2) != 0:
ac += len(exists2)
for u in exists2:
cname = str(u[0])
if accounts == "":
accounts = '{"username":"'+str(cname)+'","linked":'+str(u[1])+','+str(Account.get_profile(str(cname)))+'}'
else:
accounts = accounts+',{"username":"'+str(cname)+'","linked":'+str(u[1])+','+str(Account.get_profile(str(cname)))+'}'
if external == False:
connections = '{"total":'+str(ac)+',"connections":['+accounts.replace("'","\'")+']}'
else:
hiveaccount = json.loads(Hive.check_verified(account))["hive"]
if hiveaccount != "not connected":
hive = Hive.get_connections(hiveaccount)
hive_connections = ""
if len(hive) > 0:
ac += len(hive)
for i in hive:
try:
json.loads(i)
except:
pass
else:
stuff = json.loads(i)
if "username" in stuff and stuff["username"] not in accounts:
if hive_connections == "":
hive_connections = i
else:
hive_connections = hive_connections+","+i
if len(accounts) > 0:
connections = '{"total":'+str(ac)+',"connections":['+accounts.replace("'","\'")+','+hive_connections.replace("'","\'")+']}'
else:
connections = '{"total":'+str(ac)+',"connections":['+hive_connections.replace("'","\'")+']}'
else:
connections = '{"total":'+str(ac)+',"connections":['+accounts.replace("'","\'")+']}'
try:
json.loads(connections)
except:
return '{"total":0,"connections":"error"}'
else:
return connections
def get_account(account):
profile = '{"profile":"Not found"}'
full_account = h.get_account(account)
if full_account:
profile = full_account["json_metadata"]
return(profile)
def profile(token):
openseed = mysql.connector.connect(
host = "localhost",
user = settings["dbuser"],
password = settings["dbpassword"],
database = "openseed"
)
profile = '{"profile":"Not found"}'
hiveinfo = '{}'
mysearch = openseed.cursor()
search = "SELECT data1,data2,data3,data4,data5 FROM `profiles` WHERE `id` = %s"
val = (theid,)
mysearch.execute(search,val)
result = mysearch.fetchall()
if result[0][4]:
hiveinfo = result[0][4]
profile = result[0][0]+"','"+result[0][1]+"','"+result[0][2]+"','"+result[0][3]+"','"+hiveinfo
mysearch.close()
openseed.close()
return(profile)
# Requests have three states 1 pending 2 accepted 0 denied.
def connection_request(token,requestee,thetype,response,appPub):
output = '{"request":"error"}'
theresponse = 1
openseed = mysql.connector.connect(
host = "localhost",
user = settings["dbuser"],
password = settings["dbpassword"],
database = "openseed"
)
if response == "request":
theresponse = 1
if response == "accept":
theresponse = 2
if response == "denied":
theresponse = 0
username = json.loads(Account.user_from_id(token))["user"]
if username != "none":
request_search = openseed.cursor()
search = "SELECT * FROM `connections` WHERE userid1 LIKE %s AND userid2 LIKE %s"
vals_1 = (username,requestee)
vals_2 = (requestee,username)
request_search.execute(search,vals_1)
exists_1 = request_search.fetchall()
request_search.execute(search,vals_2)
exists_2 = request_search.fetchall()
# Checks to see if the request is already accepted
if len(exists_1) == 1 and exists_1[0][4] == 2 and theresponse != 0:
output = '{"request":"accepted","to":"'+requestee+'","from":"'+username+'"}'
elif len(exists_2) == 1 and exists_2[0][4] == 2 and theresponse != 0:
output = '{"request":"accepted","to":"'+requestee+'","from":"'+username+'"}'
# Checks to see if the request is already denied
elif len(exists_1) == 1 and exists_1[0][4] == 0:
output = '{"request":"denied","to":"'+requestee+'","from":"'+username+'"}'
elif len(exists_2) == 1 and exists_2[0][4] == 0:
output = '{"request":"denied","to":"'+requestee+'","from":"'+username+'"}'
# Checks to see if there is no request either direction
elif len(exists_1) != 1 and len(exists_2) !=1 and theresponse != 0:
insert = "INSERT INTO `connections` (`userid1`,`userid2`,`response`) VALUES (%s,%s,%s)"
values = (username,requestee,theresponse)
request_search.execute(insert,values)
openseed.commit()
output = '{"request":"sent","to":"'+requestee+'","from":"'+username+'"}'
# checks to see if the second user has sent a request to the first and auto connects users.
elif len(exists_2) == 1 and int(theresponse) == 1 and theresponse != 0:
update = "UPDATE `connections` SET `response` = %s WHERE userid1 LIKE %s AND userid2 LIKE %s"
values = ("2",requestee,username)
request_search.execute(update,values)
openseed.commit()
chatroom = Chat.find_attendees(token,requestee+","+username,1,appPub)
output = '{"request":"accepted","room":'+chatroom+'}'
# disallows user from create a second request or updating their own request to others.
elif len(exists_1) == 1 and int(theresponse) != 0:
output = '{"request":"exists","to":"'+requestee+'","from":"'+username+'"}'
# Allow users to disconnect from others or cancel request.
elif len(exists_1) == 1 and int(theresponse) == 0:
update = "UPDATE `connections` SET `response` = %s WHERE userid1 LIKE %s AND userid2 LIKE %s"
values = ("0",username,requestee)
request_search.execute(update,values)
openseed.commit()
output = '{"request":"denied","to":"'+requestee+'","from":"'+username+'"}'
elif len(exists_2) == 1 and int(theresponse) == 0:
update = "UPDATE `connections` SET `response` = %s WHERE userid1 LIKE %s AND userid2 LIKE %s"
values = ("0",requestee,username)
request_search.execute(update,values)
openseed.commit()
output = '{"request":"denied","to":"'+requestee+'","from":"'+username+'"}'
request_search.close()
openseed.close()
else:
output = '{"request":"error","log":"bad token"}'
return output
# gets requests based on token and limit. Only returns pending requests
def get_requests(token,count):
requests = ""
openseed = mysql.connector.connect(
host = "localhost",
user = settings["dbuser"],
password = settings["dbpassword"],
database = "openseed"
)
username = json.loads(Account.user_from_id(token))["user"]
mysearch = openseed.cursor()
search = "SELECT * FROM connections WHERE userid2 = %s AND response = 1 LIMIT "+str(count)
val = (username,)
mysearch.execute(search,val)
result = mysearch.fetchall()
if len(result) > 0:
for a in result:
theresponse = "pending"
thetype = "chat"
if a[4] == 0:
theresponse = "denied"
if a[4] == 1:
theresponse = "pending"
if a[4] == 2:
theresponse = "accepted"
if a[3] == 1:
thetype = "chat"
if a[3] == 2:
thetype = "group"
if requests == "":
requests = '{"request":"'+str(a[0])+'","from":"'+str(a[1])+'","type":"'+thetype+'","response":"'+theresponse+'"}'
else:
requests = requests+',{"request":"'+str(a[0])+'","from":"'+str(a[1])+'","type":"'+thetype+'","response":"'+theresponse+'"}'
else:
requests = '{"request":"none"}'
mysearch.close()
openseed.close()
return '{"requests":['+str(requests)+']}'
def request_status(token,account):
status = '{"request":"denied"}'
jsoned = status
openseed = mysql.connector.connect(
host = "localhost",
user = "openseed",
password = "b3V4ug3",
database = "openseed"
)
mysearch = openseed.cursor()
username = json.loads(Account.user_from_id(token))["user"]
search = "SELECT * FROM connections WHERE userid1 = %s AND userid2 = %s"
val1 = (username,account)
val2 = (account,username)
mysearch.execute(search,val1)
result1 = mysearch.fetchall()
mysearch.execute(search,val2)
result2 = mysearch.fetchall()
if len(result1) == 1:
status = result1[0]
elif len(result2) == 1:
status = result2[0]
thestate = "pending"
if status[4] == 0:
thestate = "denied"
if status[4] == 1:
thestate = "pending"
if status[4] == 2:
thestate = "accepted"
jsoned = '{"request":"'+thestate+'","account":"'+account+'"}'
mysearch.close()
openseed.close()
return '{"request_status":'+jsoned+'}'