-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
217 lines (198 loc) · 6.24 KB
/
database.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
import sqlite3
database = 'database.sqlite'
def init_db():
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("""
CREATE table user (
id integer primary key,
user_id text
);
""")
cursor.execute("""
CREATE table post (
id integer primary key,
text_post text,
files text,
status text,
post_time text,
public text
);
""")
cursor.execute("""
CREATE table last_post (
id integer primary key,
user_id text,
post_id integer
);
""")
cursor.execute("""
CREATE table public (
id integer primary key,
user_id text,
public_id text,
name text
);
""")
connect.close()
def delete_public(user_id,public_id):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("DELETE FROM public WHERE user_id='"+str(user_id)+"' and public_id='"+str(public_id)+"'")
connect.commit()
connect.close()
def delete_post(post_id):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("DELETE FROM post WHERE id='"+str(post_id)+"'")
connect.commit()
connect.close()
def get_queue_posts(public_id):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM post WHERE status='non_posted' and public='"+str(public_id)+"'")
posts = cursor.fetchall()
if posts:
for i in range(0,len(posts)):
posts[i] = list(posts[i])
connect.close()
return posts
def add_public(user_id,public,name):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT id FROM public")
try:
new_id = str(cursor.fetchall()[-1][0] + 1)
except:
new_id = 1
cursor.execute("insert into public values ("+str(new_id)+",'"+user_id+"','"+public+"','"+name+"')")
connect.commit()
connect.close()
return new_id
def get_public(user_id):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM public WHERE user_id='"+str(user_id)+"'")
result = cursor.fetchall()
connect.close()
print(result)
if result:
for i in range(0,len(result)):
result[i] = list(result[i])
return result
def update_last_post(post_id,user_id):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM last_post WHERE user_id='"+user_id+"'")
try:
post = cursor.fetchall()[0][0]
cursor.execute("UPDATE last_post SET post_id="+str(post_id)+" where user_id='"+user_id+"'")
connect.commit()
except:
cursor.execute("SELECT id FROM last_post")
try:
new_id = str(cursor.fetchall()[-1][0] + 1)
except:
new_id = 1
cursor.execute("insert into last_post values ("+str(new_id)+",'"+user_id+"',"+str(post_id)+")")
connect.commit()
connect.close()
def get_last_post(user_id):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT post_id FROM last_post WHERE user_id='"+user_id+"'")
post_id = cursor.fetchall()[-1][0]
connect.close()
return post_id
def add_new_post(data):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT id FROM post")
try:
new_id = str(cursor.fetchall()[-1][0] + 1)
except:
new_id = 1
try:
photo = data['parts'][0]["payload"]["fileId"]
if data['parts'][0]["type"] == "voice":
photo = "voice" + photo
try:
text = data['parts'][0]["payload"]["caption"]
except:
text = ""
cursor.execute("insert into post values ("+str(new_id)+",'"+text+"','"+photo+"','non_posted','null','public')")
except:
photo = ""
cursor.execute("insert into post values ("+str(new_id)+",'"+data['text']+"','"+photo+"','non_posted','null','public')")
connect.commit()
connect.close()
update_last_post(new_id,data["chat"]["chatId"])
return new_id
def check_user(user_id):
result = {}
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM user WHERE user_id='" + str(user_id)+"'")
res = cursor.fetchall()
connect.close()
try:
if res[0]:
result["not_exist"] = False
result["id"] = res[0][1]
except:
result["not_exist"] = True
return result
def check_post(post_id):
result = {}
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM post WHERE id='" + str(post_id)+"'")
res = cursor.fetchall()
connect.close()
result["public"] = res[0][5]
return result
def add_user(user_id):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT id FROM user")
try:
new_id = str(cursor.fetchall()[-1][0] + 1)
except:
new_id = 1
cursor.execute("insert into user values ("+str(new_id)+",'"+str(user_id)+"')")
connect.commit()
connect.close()
def update_time(post_time,post_id):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("UPDATE post SET post_time='"+str(post_time)+"' where id="+str(post_id))
connect.commit()
connect.close()
def update_post(post_id):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("UPDATE post SET status='posted' where id="+str(post_id))
connect.commit()
connect.close()
def update_public(post_id,public):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("UPDATE post SET public='"+public+"' where id="+str(post_id))
connect.commit()
connect.close()
def get_posts(time):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM post WHERE status='non_posted' and post_time='"+str(time)+"'")
posts = cursor.fetchall()
connect.close()
return posts
def get_db(table):
connect = sqlite3.connect(database)
cursor = connect.cursor()
cursor.execute("SELECT * FROM "+table)
result = cursor.fetchall()
connect.close()
return result
if __name__ == '__main__':
init_db()