· 7 years ago · Oct 05, 2018, 04:28 AM
1#!/usr/bin/python3
2from tkinter import *
3import os
4import sqlite3
5
6def text_to_array(text):
7 ret = []
8 with_spaces = text.split(',')
9 for i in with_spaces:
10 j = i.strip(' ')
11 j = j.strip('\n')
12 ret.append(j)
13 return ret
14
15def create_tables():
16 c = conn.cursor()
17 sql = '''
18 CREATE TABLE IF NOT EXISTS profiles
19 (
20 id INTEGER PRIMARY KEY AUTOINCREMENT,
21 name TEXT
22 )
23 '''
24 c.execute(sql)
25 conn.commit()
26
27def insert_profile(name):
28 c = conn.cursor()
29 sql = '''
30 INSERT OR IGNORE INTO profiles(name) VALUES(?);
31 '''
32 c.execute(sql, (name,))
33 conn.commit()
34
35def insert_profiles(profiles):
36 c = conn.cursor()
37 sql = '''
38 INSERT OR IGNORE INTO profiles(name) Values(?);
39 '''
40 c.executemany(sql, profiles)
41 conn.commit()
42
43def get_profiles():
44 profiles = []
45 c = conn.cursor()
46 for row in c.execute("SELECT name FROM profiles"):
47 profiles.append(row[0])
48 print(len(profiles))
49 return profiles
50
51def import_profiles():
52 blacklist = get_list('blacklist.txt')
53 blacklist_tuples = []
54 for name in blacklist:
55 blacklist_tuples.append((name,),)
56 insert_profiles(blacklist_tuples)
57
58def db_get_filtered():
59 current_text = my_gui.input.get("1.0", END)
60 current = text_to_array(current_text)
61 blacklist = get_profiles()
62 ret = []
63 for name in current:
64 if name not in blacklist:
65 ret.append(name)
66 return ret
67
68def export_profiles():
69 profiles = get_profiles()
70 for name in profiles:
71 os.system('echo "' + name + "," + '" >> export.csv')
72
73class MyFirstGUI:
74 def __init__(self, master):
75 self.master = master
76 self.buttons = []
77 master.title("Blacklist filterers")
78 #create_tables()
79 #get_profiles()
80 #import_profiles()
81 #export_profiles()
82 self.init_controls()
83
84 def init_controls(self):
85 text = Text(self.master, height=1, width=5)
86 text.pack(side=LEFT)
87 self.input = text
88
89 button = Button(self.master, text="go", command = lambda: self.populate())
90 button.pack(side=LEFT)
91 self.go_button = button
92
93 def populate(self):
94 for button in self.buttons:
95 button.destroy()
96
97 candidates = db_get_filtered()
98 self.input.delete("1.0", END)
99 for c in candidates:
100 self.add_button(c)
101
102
103 def add_button(self, name):
104 button = Button(self.master, text=name, command = lambda: self.blacklist(name, button))
105 button.pack()
106 self.buttons.append(button)
107
108
109 def blacklist(self, name, button):
110 payload = name + ", "
111 #os.system('echo "' + payload + '" >> blacklist.txt')
112 insert_profile(name)
113 button.destroy()
114
115root = Tk()
116conn = sqlite3.connect("blacklist.db");
117create_tables()
118my_gui = MyFirstGUI(root)
119root.mainloop()