-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainmoduleSQL.py
500 lines (425 loc) · 14.1 KB
/
MainmoduleSQL.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import sqlite3
from sqlite3 import Error
import logging
import json
import requests
import telegramclient
import time
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
database = "testlib002"
key = "6WTWncHTBCrv6foypFNS7gdPX6avd8bMC1i6QGpxq9wx"
def create_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def tablewrite():
try:
db = sqlite3.connect('testlib002')
cursor = db.cursor()
cursor.execute('''create table SWTMain (
id VARCHAR,
ip VARCHAR,
publickey VARCHAR,
country VARCHAR,
countryname VARCHAR,
city VARCHAR,
platform VARCHAR,
countrust INTEGER,
timeregistration VARCHAR,
timeactive VARCHAR,
latitude VARCHAR,
longitude VARCHAR,
active VARCHAR,
totalfee VARCHAR,
timeswritter INTEGER)''')
logging.info ("Succes")
except Exception as E:
logging.error('Error :{}'.format(E))
else:
logging.info('table created')
def tablewriteuser():
try:
db = sqlite3.connect('testlib002')
cursor = db.cursor()
cursor.execute('''create table SWTuser (
chatid VARCHAR,
publickey VARCHAR,
running INTEGER)''')
logging.info ("Succes")
except Exception as E:
logging.error('Error :{}'.format(E))
else:
logging.info('table created')
def tablewritehourly():
try:
db = sqlite3.connect('testlib002')
cursor = db.cursor()
cursor.execute('''create table SWThour (
chatid VARCHAR,
publickey VARCHAR,
running INTEGER)''')
logging.info ("Succes")
except Exception as E:
logging.error('Error :{}'.format(E))
else:
logging.info('table created')
def Adduserkey(key,chatid):
debug = True
global userdatafound
conn = create_connection(database)
cur = conn.cursor()
sqlline = "SELECT * FROM SWTuser WHERE publickey='{}' AND chatid='{}' ".format(key,chatid)
cur.execute(sqlline)
data = cur.fetchone()
try:
if data == None:
try:
runningstatus = True
data = (chatid), (key), (runningstatus)
cur = conn.cursor()
logging.info(data)
cur.execute('INSERT INTO SWTuser VALUES(?,?,?)',data)
conn.commit()
logging.info('data inserted')
userdatafound = False
except Exception as E:
logging.info('Error : {}'.format(E))
if debug:
logging.info("Key not found")
elif data != None:
userdatafound = True
if debug:
logging.info("Key found")
except Exception as E:
logging.info('Error : {}'.format(E))
def lastid():
global idvalue
conn = create_connection(database)
cur = conn.cursor()
cur.execute("SELECT COUNT(id) FROM SWTmain ")
#cur.execute("SELECT id FROM SWTmain WHERE active = (SELECT MAX(id) FROM SWTmain)")
idvalue2 = cur.fetchone()
idvalue = idvalue2[0]
logging.info("The last id number is: {}".format(idvalue))
def checkifpresent(publickey,debug):
conn = create_connection(database)
cur = conn.cursor()
key = (publickey,)
if debug:
logging.info("Checking key")
cur.execute("SELECT * FROM SWTmain WHERE publickey=? ",key)
data = cur.fetchone()
#logging.info("data=")
#logging.info(data)
try:
if data == None:
if debug:
logging.info("Key not found")
logging.info("Returning value")
return False
elif data != None:
if debug:
#logging.info("data=")
#logging.info(data)
logging.info("Key found")
logging.info("Returning value")
return True
except Exception as E:
logging.info('Error : {}'.format(E))
def checkifpresentuser(publickey):
global chatidvar
global telegramkey
global usernode
telegramkey = publickey
debug = False
conn = create_connection(database)
cur = conn.cursor()
key = (publickey,)
#logging.info("publickey=")
#logging.info(publickey)
if debug:
logging.info("Checking key")
cur.execute("SELECT * FROM SWTuser WHERE publickey=? ",key)
data = cur.fetchone()
#logging.info("datauserchek=")
#logging.info(data)
try:
if data == None:
usernode = False
if debug:
logging.info("Key not found")
logging.info("Returning value")
elif data != None:
cur.execute("SELECT * FROM SWTuser WHERE publickey=? ",key)
runningdata = cur.fetchone()
runningstatus = runningdata[2]
chatidvar = runningdata[0]
if debug:
logging.info("Key found")
logging.info("Returning value")
if runningstatus == 1:
usernode = True
#logging.info("usernode=")
#logging.info(usernode)
if runningstatus == 0:
usernode = True
#logging.info("usernode=")
#logging.info(usernode)
#logging.info(key)
except Exception as E:
logging.info('Error : {}'.format(E))
def checkifrunning(var2,debug):
if debug:
logging.info("check if running module activated")
global runningvalue
global pkey
var1 = (var2,)
conn = create_connection(database)
cur = conn.cursor()
if debug:
logging.info("checking db for running status")
cur.execute("SELECT * FROM SWTmain WHERE publickey=? ",var1)
data2 = cur.fetchone()
data = data2[12]
pkey = data2[2]
if debug:
logging.info(data)
try:
if data == "0":
#logging.info("Node Offline with publickey: {}".format(pkey))
runningvalue = 0
elif data == "1":
#logging.info("Node is running with publickey: {}".format(pkey))
runningvalue = 1
#logging.info(var1)
#logging.info("runningvalue=")
#logging.info(runningvalue)
except Exception as E:
logging.info('Error : {}'.format(E))
def singlewrite(var1):
conn = create_connection(database)
try:
lastid()
idval = int(idvalue)
idval = idval + 1
ni = var1
logging.info(ni)
logging.info(idval)
data = (idval), (ni["ip"]), (ni["publicKey"]), (ni["country"]), (ni["countryName"]), (ni["city"]), (ni["platform"]), (ni["countTrust"]), (ni["timeRegistration"]), (ni["timeActive"]), (ni["latitude"]), (ni["longitude"]), (ni["active"]), (ni["totalFee"]), (ni["timesWriter"])
cur = conn.cursor()
cur.execute('INSERT INTO SWTmain VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',data)
keyvar = data[2]
telegramclient.addkey(keyvar)
except Exception as E:
logging.info('Error : {}'.format(E))
else:
conn.commit()
logging.info('data inserted')
def one(conn):
try:
cur = conn.cursor()
cur.execute('INSERT INTO SWTmain VALUES(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)')
except Exception as E:
logging.info('Error : {}'.format(E))
else:
conn.commit()
logging.info('data inserted')
def updatestatus(key,active):
conn = create_connection(database)
try:
cur = conn.cursor()
cur.execute('UPDATE SWTmain SET active= ? WHERE publickey= ? ',(active,key))
conn.commit()
logging.info('data Updated')
except Exception as E:
logging.info('Error : {}'.format(E))
def updateuser(chatid,active,key):
conn = create_connection(database)
try:
cur = conn.cursor()
cur.execute('UPDATE SWTuser SET running= ? WHERE publickey= ? AND chatid=?',(active,key,chatid))
conn.commit()
logging.info('data Updated')
except Exception as E:
logging.info('Error : {}'.format(E))
def alltime_trust():
try:
conn = create_connection(database)
cur = conn.cursor()
cur.execute("SELECT * FROM SWTmain ORDER BY countrust DESC, active DESC LIMIT 10")
data2 = cur.fetchall()
return data2
except Exception as E:
logging.info('Error : {}'.format(E))
def alltime_written():
try:
conn = create_connection(database)
cur = conn.cursor()
cur.execute("SELECT * FROM SWTmain ORDER BY timeswritter DESC, active DESC LIMIT 10")
data2 = cur.fetchall()
return data2
except Exception as E:
logging.info('Error : {}'.format(E))
def fetchrunning():
debug = True
if debug:
logging.info("fetch running")
conn = create_connection(database)
cur = conn.cursor()
try:
cur.execute("SELECT * FROM SWTmain WHERE active=1 ")
data2 = cur.fetchall()
return data2
except Exception as E:
logging.info('Error : {}'.format(E))
def Addhourkey(key,chatid):
debug = True
global userdatafound
conn = create_connection(database)
cur = conn.cursor()
sqlline = "SELECT * FROM SWThour WHERE publickey='{}' AND chatid='{}' ".format(key,chatid)
cur.execute(sqlline)
data = cur.fetchone()
try:
if data == None:
try:
runningstatus = True
data = (chatid), (key), (runningstatus)
cur = conn.cursor()
logging.info(data)
cur.execute('INSERT INTO SWThour VALUES(?,?,?)',data)
conn.commit()
logging.info('data inserted')
userdatafound = False
except Exception as E:
logging.info('Error : {}'.format(E))
if debug:
logging.info("Key not found")
elif data != None:
userdatafound = True
if debug:
logging.info("Key found")
except Exception as E:
logging.info('Error : {}'.format(E))
def checkifpresenthour(publickey):
global chatidvar
global telegramkey
global usernode
telegramkey = publickey
debug = False
conn = create_connection(database)
cur = conn.cursor()
key = (publickey,)
if debug:
logging.info("Checking key")
cur.execute("SELECT * FROM SWThour WHERE publickey=? ",key)
data = cur.fetchone()
try:
if data == None:
usernode = False
if debug:
logging.info("Key not found")
logging.info("Returning value")
elif data != None:
cur.execute("SELECT * FROM SWThour WHERE publickey=? ",key)
runningdata = cur.fetchone()
runningstatus = runningdata[2]
chatidvar = runningdata[0]
if debug:
logging.info("Key found")
logging.info("Returning value")
if runningstatus == "1":
usernode = True
if runningstatus == "0":
usernode = True
except Exception as E:
logging.info('Error : {}'.format(E))
def updatehour(chatid,active,key):
conn = create_connection(database)
try:
cur = conn.cursor()
cur.execute('UPDATE SWThour SET running= ? WHERE publickey= ? AND chatid=?',(active,key,chatid))
conn.commit()
logging.info('data Updated')
except Exception as E:
logging.info('Error : {}'.format(E))
def fetch_hour_nodes():
debug = True
if debug:
logging.info("fetch hour nodes")
conn = create_connection(database)
cur = conn.cursor()
try:
cur.execute("SELECT * FROM SWThour ")
data2 = cur.fetchall()
return data2
except Exception as E:
logging.info('Error : {}'.format(E))
def fetch_user_nodes(chatid):
chatid = int(chatid)
debug = True
if debug:
logging.info("fetch User nodes")
conn = create_connection(database)
cur = conn.cursor()
sqtext = "SELECT * FROM SWThour WHERE chatid={}".format(chatid)
try:
cur.execute(sqtext)
data2 = cur.fetchall()
return data2
except Exception as E:
logging.info('Error : {}'.format(E))
def getdatauser():
conn = create_connection(database)
cur = conn.cursor()
try:
cur.execute("SELECT * FROM SWTmain")
datasql = cur.fetchall()
#logging.info("datasql")
#logging.info(datasql)
return datasql
except Exception as E:
logging.info('Error : {}'.format(E))
def getcountuser():
conn = create_connection(database)
cur = conn.cursor()
try:
cur.execute("SELECT COUNT (*) FROM SWTmain")
countuser = cur.fetchone()
logging.info("countuser")
logging.info(countuser)
return countuser
except Exception as E:
logging.info('Error : {}'.format(E))
def offline_nodes():
conn = create_connection(database)
cur = conn.cursor()
try:
cur.execute("SELECT * FROM SWTmain WHERE `active` = 0;")
data2 = cur.fetchall()
#logging.info("off_data")
#logging.info(data2)
return data2
except Exception as E:
logging.info('Error : {}'.format(E))
def count_offline_nodes():
conn = create_connection(database)
cur = conn.cursor()
try:
cur.execute("SELECT COUNT (*) FROM SWTmain WHERE `active` = 0;")
count_off_node = int(cur.fetchall()[0][0])
#logging.info("count_off_node")
#logging.info(count_off_node)
return count_off_node
except Exception as E:
logging.info('Error : {}'.format(E))
tablewrite()
tablewriteuser()
tablewritehourly()
#Adduserkey("bNF5NLrQHhTpqxaVaSYQDQm85KjWVxLKJFTq34WnswP",530871094)
#print (checkifpresent("EsWnHGtNx8fDFwfLBNR5xwpeVpCt79Bc2uj7ZSpwdYzu",True))
#updateuser(530871094,True,"bNF5NLrQHhTpqxaVaSYQDQm85KjWVxLKJFTq34WnswP")