-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveLink.py
476 lines (456 loc) · 23.1 KB
/
SaveLink.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
import getpass
import os
import sys
from datetime import date, datetime
import configparser
from pysqlcipher3 import dbapi2 as sqlcipher
import numpy
class color:
OKBLUE = ('\033[94m')
OKGREEN = ('\033[92m')
WARNING = ('\033[93m')
RED = ('\033[91m')
END = ('\033[0m')
botslPrompt = (color.OKGREEN + "sl ~# " + color.END)
db_dir = ('/home/{}/.savelink/'.format(getpass.getuser()))
papka = os.path.isdir(db_dir)
if papka == False:
os.mkdir(db_dir)
botslLogo = (color.OKGREEN + r"""
___ _ ___ _
/ __|__ ___ _____ | | |_ _|_ _ | |__
\__ / _` \ V / -_) | |__ | || ' \| / /
|___\____|\_/\___| |____|___|_||_|_\_\ """ + color.END)
def clearScr() -> None:
os.system('clear')
def dec(string: str) -> str:
length_string = (48 - len(string)) // 2 # 39
decor = str((length_string * '-') + string + (length_string * '-') + '\n')
return decor
def vhod():
global account_pass, conn, cursor
clearScr()
x1 = os.path.isfile(db_dir + 'main.db')
if x1:
name_db = (db_dir + 'main.db')
conn = sqlcipher.connect(name_db)
cursor = conn.cursor()
clearScr()
print(botslLogo)
print(dec(color.RED + 'Sign in' + color.END))
while True:
try:
account_pass = getpass.getpass(color.OKBLUE + 'enter password: ' + color.END)
if account_pass == 'Q':
clearScr()
sys.exit()
cursor.execute("PRAGMA key={}".format(account_pass))
cursor.execute('SELECT COUNT(link) FROM links')
except:
print(color.RED + 'wrong password' + color.END)
else:
break
elif not x1:
print(botslLogo)
print(dec(color.RED + 'Sign up' + color.END))
while True:
password1 = getpass.getpass(color.OKBLUE + 'enter password: ' + color.END)
if password1 == 'Q':
clearScr()
sys.exit()
else:
break
while True:
password2 = getpass.getpass(color.OKBLUE + 'repeat password: ' + color.END)
if password2 == 'Q':
vhod()
else:
break
while True:
if password1 == password2:
account_pass = password1
break
elif password1 != password2:
print(color.RED + 'different passwords' + color.END)
password1 = getpass.getpass(color.OKBLUE + 'enter password: ' + color.END)
password2 = getpass.getpass(color.OKBLUE + 'repeat password: ' + color.END)
if password1 or password2 == 'Q':
vhod()
while True:
u_podtver = input(color.OKBLUE + '[Y/n] add ' + color.END)
if u_podtver == 'n':
vhod()
break
elif u_podtver == 'Y':
# make users db
name_db = (db_dir + 'main.db')
conn = sqlcipher.connect(name_db)
cursor = conn.cursor()
cursor.execute("PRAGMA key={}".format(account_pass))
cursor.execute("""CREATE TABLE links
(id integer primary key, link varchar(255), info varchar(255), category varchar(255), time datetime NOT NULL)
""")
conn.commit()
break
def main():
global account_pass, cursor, conn
clearScr()
cursor.execute("PRAGMA key={}".format(account_pass))
cursor.execute('select count(link) from links')
all_link = cursor.fetchone()
cursor.execute('select category from links GROUP BY category')
all_category = numpy.array(cursor.fetchall(), dtype=str)
print(botslLogo)
print(dec(color.RED + 'options' + color.END))
print(color.RED + '1' + color.END + ')--' + color.OKBLUE + 'Add' + color.END)
print(color.RED + '2' + color.END + ')--' + color.OKBLUE + 'View' + color.END + '(' + str(all_link[0]) + ')')
print(color.RED + '3' + color.END + ')--' + color.OKBLUE + 'Etc' + color.END)
print(color.RED + '4' + color.END + ')--' + color.OKBLUE + 'Categories' + color.END + '(' + str(all_category.size) + ')')
print(color.RED + '5' + color.END + ')--' + color.OKBLUE + 'Exit\n' + color.END)
usercomand = input(botslPrompt)
clearScr()
if usercomand == '1':
clearScr()
print(botslLogo)
print(dec(color.RED + 'Add' + color.END))
while True:
link = input(color.OKBLUE + 'enter the link: ' + color.END)
if link == "Q":
main()
if len(link) > 5:
break
while True:
info = input(color.OKBLUE + 'enter info: ' + color.END)
if info == "Q":
main()
if len(info) > 1:
break
while True:
cursor.execute("PRAGMA key={}".format(account_pass))
cursor.execute('select category from links')
results = numpy.array(cursor.fetchall(), dtype=str)
if results.size > 0:
category_str = ""
for i in results:
category_str += str(i[0] + ', ')
print(color.OKBLUE + "categories: " + color.END + category_str[:-2])
category = input(color.OKBLUE + 'enter category: ' + color.END)
if len(category) == 0:
print(color.RED + 'wrong category' + color.RED)
if category == 'Q':
main()
if len(category) > 0:
break
while True:
usercomand = input(color.OKBLUE + '[Y/n] add link: ' + color.END + link + color.OKBLUE + ' info: ' + color.END + info + color.OKBLUE + ' category: ' + color.END + category +': ')
if usercomand == "Y":
now = date.today()
cursor.execute("PRAGMA key={}".format(account_pass))
cursor.execute("insert into links(link, info, time, category) values (?, ?, ?, ?)", (link, info, now, category))
conn.commit()
main()
break
elif usercomand == "n" or "Q":
main()
break
elif usercomand == '2':
clearScr()
print(botslLogo)
print(dec(color.RED + 'Links' + color.END))
if all_link[0] == 0:
print(color.RED + "no links" + color.END)
while True:
print(color.RED + 'Q)--GO BACK\n' + color.END)
uc = input(botslPrompt)
if uc == 'Q':
main()
break
else:
cursor.execute("PRAGMA key={}".format(account_pass))
cursor.execute('select id, link, info, time, category from links')
results = numpy.array(cursor.fetchall(), dtype=str)
for i in results:
print(color.OKBLUE + 'id: ' + color.END + i[0] + color.OKBLUE +
'\nlink: ' + color.END + i[1] + color.OKBLUE +
'\ninfo: ' + color.END + i[2] + color.OKBLUE +
'\ncategory: ' + color.END + i[4] + color.OKBLUE +
'\ndata: ' + color.END + i[3] + color.OKBLUE + '\n')
while True:
print(color.RED + 'Q)--GO BACK\n' + color.END)
uc = input(botslPrompt)
if uc == 'Q':
main()
break
elif usercomand == '3':
cursor.execute("PRAGMA key={}".format(account_pass))
while True:
clearScr()
print(botslLogo)
print(dec(color.RED + 'Etc' + color.END))
print(color.RED + '1' + color.END + ')--' + color.OKBLUE + 'Search' + color.OKBLUE)
print(color.RED + '2' + color.END + ')--' + color.OKBLUE + 'Delete everything' + color.END)
print(color.RED + '3' + color.END + ')--' + color.OKBLUE + 'Edit' + color.OKBLUE)
print(color.RED + '4' + color.END + ')--' + color.OKBLUE + 'Remove account' + color.OKBLUE)
print(color.RED + '5' + color.END + ')--' + color.OKBLUE + 'Exit\n' + color.END)
usercomand = input(botslPrompt)
if usercomand == '1':
clearScr()
print(botslLogo)
print(dec(color.RED + 'Search' + color.END))
cursor.execute('select count(link) from links')
all_link = cursor.fetchone()
if all_link[0] == 0:
print("no links")
while True:
print(color.RED + '\nQ)--GO BACK\n' + color.END)
uc = input(botslPrompt)
if uc == 'Q':
main()
break
else:
cursor.execute("PRAGMA key={}".format(account_pass))
cursor.execute('select id, link, info, time, category from links')
results = numpy.array(cursor.fetchall(), dtype=str)
info_poisk = input(color.OKBLUE + 'search word: ' + color.END)
print("")
if info_poisk == 'Q':
main()
for i in results:
if info_poisk in i[2] or info_poisk in i[4]:
print(color.OKBLUE + 'id: ' + color.END + i[0] + color.OKBLUE +
'\nlink: ' + color.END + i[1] + color.OKBLUE +
'\ninfo: ' + color.END + i[2] + color.OKBLUE +
'\ncategory: ' + color.END + i[4] + color.OKBLUE +
'\ndata: ' + color.END + i[3] + color.OKBLUE + '\n')
while True:
print(color.RED + 'Q)--GO BACK\n' + color.END)
uc = input(botslPrompt)
if uc == 'Q':
main()
break
elif usercomand == '2':
clearScr()
print(botslLogo)
print(dec(color.RED + 'Delete all' + color.END))
while True:
account_password = getpass.getpass(color.OKBLUE + 'enter password: ' + color.END)
if account_password == 'Q':
main()
if account_pass == account_password:
break
if account_password != account_pass:
print(color.RED + 'wrong password' + color.END)
cursor.execute('DELETE FROM links')
cursor.execute('REINDEX links')
conn.commit()
main()
elif usercomand == '3':
clearScr()
print(botslLogo)
print(dec(color.RED + 'Edit' + color.END))
cursor.execute("PRAGMA key={}".format(account_pass))
cursor.execute('select count(link) from links')
all_link = cursor.fetchone()
if all_link[0] == 0:
print(color.RED + "no links" + color.END)
while True:
print(color.RED + 'Q)--GO BACK\n' + color.END)
uc = input(botslPrompt)
if uc == 'Q':
main()
break
else:
while True:
print(color.OKBLUE + 'show everyone? [Y/n]\n' + color.END)
uc = input(botslPrompt)
if uc == 'Y':
cursor.execute("PRAGMA key={}".format(account_pass))
cursor.execute('select id, link, info, time, category from links')
results = numpy.array(cursor.fetchall(), dtype=str)
print("")
for i in results:
print(color.OKBLUE + 'id: ' + color.END + str(i[0]) + color.OKBLUE + '\nlink: ' + color.END + i[1] + color.OKBLUE + '\ninfo: ' + color.END + i[2] + color.OKBLUE + '\ncategory: ' + color.END + i[4] + color.OKBLUE + '\ndata: ' + color.END + str(i[3]) + '\n')
break
elif uc == 'n':
break
elif uc == 'Q':
main()
break
while True:
link_id = input(color.OKBLUE + 'id for edit: ' + color.END)
if link_id == 'Q':
main()
sql = ('SELECT COUNT(id) FROM links WHERE id = ?')
cursor.execute(sql, (link_id,))
results = cursor.fetchone()
if results[0] == 0:
print(color.RED + 'Id not found' + color.END)
elif results[0] == 1:
break
sql = ('SELECT * FROM links WHERE id = ?')
cursor.execute(sql, (link_id,))
i = cursor.fetchone()
clearScr()
print(botslLogo)
print(dec(color.RED + 'Edit' + color.END))
print(color.OKBLUE + 'id: ' + color.END + str(i[0]) + color.OKBLUE + '\nlink: ' + color.END + i[1] + color.OKBLUE + '\ninfo: ' + color.END + i[2] + color.OKBLUE + '\ncategory: ' + color.END + i[3] + color.OKBLUE + '\ndata: ' + color.END + str(i[4]) + '\n')
while True:
print(color.RED + '1' + color.END + ')--' + color.OKBLUE + 'Remove' + color.OKBLUE)
print(color.RED + '2' + color.END + ')--' + color.OKBLUE + 'Change the link' + color.END)
print(color.RED + '3' + color.END + ')--' + color.OKBLUE + 'Change info' + color.OKBLUE)
print(color.RED + '4' + color.END + ')--' + color.OKBLUE + 'Change category' + color.OKBLUE)
print(color.RED + '5' + color.END + ')--' + color.OKBLUE + 'Exit\n' + color.OKBLUE)
usercomand = input(botslPrompt)
if usercomand == '1':
clearScr()
print(botslLogo)
print(dec(color.RED + 'Remove' + color.END))
usercomand = input(color.OKBLUE + '[Y/n] Remove: ' + color.END + str(i[1]) + color.OKBLUE + ' info: ' + color.END + i[2] + ': ')
if usercomand == "Y":
sql = ("""DELETE FROM links WHERE id = ?""")
cursor.execute(sql, (link_id,))
conn.commit()
main()
break
elif usercomand == 'n' or 'Q':
main()
break
elif usercomand == '2':
clearScr()
print(botslLogo)
print(dec(color.RED + 'Change the link' + color.END))
print(color.OKBLUE + 'id: ' + color.END + str(i[0]) + color.OKBLUE + '\nlink: ' + color.END + i[1] + color.OKBLUE + '\ninfo: ' + color.END + i[2] + color.OKBLUE + '\ncategory: ' + color.END + i[3] + color.OKBLUE + '\ndata: ' + color.END + i[4] + '\n')
while True:
new_link = input(color.OKBLUE + 'enter a new link: ' + color.END)
usercomand = input(color.OKBLUE + '[Y/n] replace ' + color.END + i[1] + color.OKBLUE + ' on the ' + color.END + new_link + ': ')
if usercomand == "Y":
sql = ("""UPDATE links SET link = ? WHERE id = ?""")
cursor.execute(sql, (new_link, link_id))
conn.commit()
main()
break
elif usercomand == 'n' or 'Q':
main()
break
elif usercomand == '3':
clearScr()
print(botslLogo)
print(dec(color.RED + 'Change info' + color.END))
print(color.OKBLUE + 'id: ' + color.END + str(i[0]) + color.OKBLUE + '\nlink: ' + color.END + i[1] + color.OKBLUE + '\ninfo: ' + color.END + i[2] + color.OKBLUE + '\ncategory: ' + color.END + i[3] + color.OKBLUE + '\ndata: ' + color.END + i[4] + '\n')
while True:
new_info = input(color.OKBLUE + 'enter new info: ' + color.END)
usercomand = input(color.OKBLUE + '[Y/n] replace ' + color.END + i[2] + color.OKBLUE + ' on the ' + color.END + new_info + ': ')
if usercomand == "Y":
sql = ("""UPDATE links SET info = ? WHERE id = ?""")
cursor.execute(sql, (new_info, link_id))
conn.commit()
main()
break
elif usercomand == 'n' or 'Q':
main()
break
elif usercomand == '4':
clearScr()
print(botslLogo)
print(dec(color.RED + 'Change category' + color.END))
print(color.OKBLUE + 'id: ' + color.END + str(i[0]) + color.OKBLUE + '\nlink: ' + color.END + i[1] + color.OKBLUE + '\ninfo: ' + color.END + i[2] + color.OKBLUE + '\ncategory: ' + color.END + i[3] + color.OKBLUE + '\ndata: ' + color.END + i[4] + '\n')
while True:
new_category = input(color.OKBLUE + 'enter new category: ' + color.END)
usercomand = input(color.OKBLUE + '[Y/n] replace ' + color.END + i[3] + color.OKBLUE + ' on the ' + color.END + new_category + ': ')
if usercomand == "Y":
sql = ("""UPDATE links SET category = ? WHERE id = ?""")
cursor.execute(sql, (new_category, link_id))
conn.commit()
main()
break
elif usercomand == 'n' or 'Q':
main()
break
elif usercomand == '5':
main()
break
elif usercomand == '4':
clearScr()
print(botslLogo)
print(dec(color.RED + 'Remove account' + color.END))
while True:
usercomand = input(color.OKBLUE + '[Y/n] Remove account? ')
if usercomand == ('Q' or 'n'):
main()
if usercomand == 'Y':
break
else:
print(color.RED + 'wrong command' + color.END)
while True:
account_password = getpass.getpass(color.OKBLUE + 'enter password: ' + color.END)
if account_password == 'Q':
main()
if account_pass == account_password:
break
if account_password != account_pass:
print(color.RED + 'wrong password' + color.END)
cursor.close()
conn.close()
os.remove(db_dir + 'main.db')
vhod()
main()
elif usercomand == '5':
main()
break
elif usercomand == '4':
while True:
clearScr()
print(botslLogo)
print(dec(color.RED + 'Categories' + color.END))
cursor.execute("PRAGMA key={}".format(account_pass))
cursor.execute('select category from links GROUP BY category')
if all_link[0] == 0:
print(color.RED + "no links" + color.END)
while True:
print(color.RED + 'Q)--GO BACK\n' + color.END)
uc = input(botslPrompt)
if uc == 'Q':
main()
break
else:
results = numpy.array(cursor.fetchall(), dtype=str)
item_uc = {j:i[0] for i,j in zip(results,[i + 1 for i in range(len(results))])}
for i in item_uc:
print(color.RED + str(i) + color.END + ')--' + color.OKBLUE + item_uc[i] + color.OKBLUE)
print(color.RED + str(i + 1) + color.END + ')--' + color.OKBLUE + 'Exit' + color.OKBLUE)
usercomand = input('\n' + botslPrompt)
if usercomand == "Q":
main()
break
elif usercomand == str(i + 1):
main()
try:
while True:
clearScr()
print(botslLogo)
print(dec(color.RED + 'Categories: ' + color.END + item_uc[int(usercomand)]))
cursor.execute("PRAGMA key={}".format(account_pass))
sql = ('select * from links where category = ?')
cursor.execute(sql, (item_uc[int(usercomand)],))
results = numpy.array(cursor.fetchall(), dtype=str)
for i in results:
print(color.OKBLUE + 'id: ' + color.END + i[0] + color.OKBLUE +
'\nlink: ' + color.END + i[1] + color.OKBLUE +
'\ninfo: ' + color.END + i[2] + color.OKBLUE +
'\ncategory: ' + color.END + i[3] + color.OKBLUE +
'\ndata: ' + color.END + i[4] + color.OKBLUE + '\n')
print(color.RED + 'Q)--GO BACK\n' + color.END)
uc = input(botslPrompt)
if uc == 'Q':
break
except:
pass
else:
main()
elif usercomand == '5':
clearScr()
sys.exit()
else:
main()
vhod()
main()