-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
261 lines (216 loc) · 8.59 KB
/
db.py
File metadata and controls
261 lines (216 loc) · 8.59 KB
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
import psycopg2
import urllib.parse as urlparse
import os
import AuxiliaryFunctions as auxf
from datetime import datetime
def close_connection(cursor, connection):
if(connection):
cursor.close()
connection.close()
return True
def connect_to_db():
try:
url = urlparse.urlparse(os.environ['DATABASE_URL'])
dbname = url.path[1:]
user = url.username
password = url.password
host = url.hostname
port = url.port
connection = psycopg2.connect(
dbname=dbname,
user=user,
password=password,
host=host,
port=port
)
return connection
except:
return None
def test():
connection = connect_to_db()
if(connection == None):
return "Error while connecting to PostgreSQL" + str(error)
try:
cursor = connection.cursor()
# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
output = "You are connected to - " + str(record) + "\n\n"
output += "Lista de Tabelas:\n"
cursor.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
for table in cursor.fetchall():
output += str(table) + "\n"
except (Exception, psycopg2.Error) as error :
output = "Error during test() function" + str(error)
finally:
close_connection(cursor, connection)
return output
def limpa_resumo():
try:
connection = connect_to_db()
cursor = connection.cursor()
cursor.execute("DELETE FROM mensagens *;")
connection.commit()
except (Exception, psycopg2.Error) as error :
output = "Error during test() function" + str(error)
finally:
close_connection(cursor, connection)
def setup_resumo(cursor, connection):
#Media guarda o file_id se existir
#Mensagem guarda o text, se existir
try:
#create table if not exists
create_table_query = '''CREATE TABLE IF NOT EXISTS MENSAGENS(
ID SERIAL PRIMARY KEY ,
CHATID TEXT NOT NULL ,
MENSAGEM TEXT ,
MEDIA TEXT ,
DATA TIMESTAMP NOT NULL
);'''
cursor.execute(create_table_query)
#delete old entries
delete_old_entries_query = '''DELETE FROM mensagens WHERE DATA < (now() - '24 hours'::interval);'''
cursor.execute(delete_old_entries_query)
connection.commit()
except (Exception, psycopg2.DatabaseError) as error :
print("Error while creating PostgreSQL resumo table", error)
return False
return True
def setup_transparente(cursor, connection):
# Guarda imagens transparentes pro comando merge
try:
#create table if not exists
create_table_query = '''CREATE TABLE IF NOT EXISTS IMGTRANSPARENTE(
ID SERIAL PRIMARY KEY ,
CHATID TEXT NOT NULL ,
NOME TEXT ,
FILEID TEXT NOT NULL ,
DATA TIMESTAMP NOT NULL
);'''
cursor.execute(create_table_query)
connection.commit()
except (Exception, psycopg2.DatabaseError) as error :
print("Error while creating PostgreSQL transparente_img table", error)
return False
return True
def setup(): #create the tables if necessary, and clean old entries
connection = connect_to_db()
if(connection == None):
print("Error while connecting to PostgreSQL" + str(error))
return False
cursor = connection.cursor()
setup_resumo(cursor, connection)
setup_transparente(cursor, connection)
close_connection(cursor, connection)
return True
def insere_img_transparente(update):
chatid = update.message.chat.id
name = update.message.text
name = name.replace('/insert_ti', '').strip()
fileid = update.message.reply_to_message.document.file_id
if(fileid is None or len(fileid) == 0):
print("Sem imagem para inserir...")
return
if(fileid is None and name is None):
return
try:
connection = connect_to_db()
cursor = connection.cursor()
postgres_insert_query = """ INSERT INTO imgtransparente (CHATID, NOME, FILEID, DATA) VALUES (%s,%s,%s, current_timestamp)"""
record_to_insert = (chatid, name, fileid)
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count = cursor.rowcount
print (count, "Record inserted successfully into imagem_transparente table")
except (Exception, psycopg2.Error) as error :
print("Failed to insert record into imagem_transparente table", error)
finally:
close_connection(cursor, connection)
return True
def get_transparent_image(name):
try:
connection = connect_to_db()
cursor = connection.cursor()
cursor.execute('''SELECT FILEID FROM
IMGTRANSPARENTE
WHERE
NOME = %s''', [name])
fileid = cursor.fetchall()
except (Exception, psycopg2.Error) as error :
fileid = "Error while fetching data from PostgreSQL" + str(error)
finally:
return fileid[0][0]
#For now, ignore stickers
def insere_mensagem(update):
chatid = update.message.chat.id
text = update.message.text
media = update.message.photo
if(media is not None and len(media) > 0):
media = media[0].file_id
else:
media = None
'''else:
media = update.message.sticker
if(media is not None):
media = media.file_id'''
if(media is None and text is None):
return
try:
connection = connect_to_db()
cursor = connection.cursor()
postgres_insert_query = """ INSERT INTO mensagens (CHATID, MENSAGEM, MEDIA, DATA) VALUES (%s,%s,%s, current_timestamp)"""
record_to_insert = (chatid,text, media)
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count = cursor.rowcount
print (count, "Record inserted successfully into mensagens table")
except (Exception, psycopg2.Error) as error :
print("Failed to insert record into mensagens table", error)
finally:
close_connection(cursor, connection)
return True
def printa_mensagens():
try:
output = "Saida: \n"
connection = connect_to_db()
cursor = connection.cursor()
postgreSQL_select_Query = "SELECT * FROM mensagens ORDER BY ID DESC LIMIT 5;"
cursor.execute(postgreSQL_select_Query)
mensagens = cursor.fetchall()
for row in mensagens:
output += "Id = " + str(row[0]) + "\n"
output += "ChatId = " + str(row[1]) + "\n"
output += "Mensagem = " + str(row[2]) + "\n"
output += "Media = " + str(row[3]) + "\n"
output += "Data = " + str(row[4]) + "\n"
output += "\n"
except (Exception, psycopg2.Error) as error :
output = "Error while fetching data from PostgreSQL" + str(error)
finally:
return output
def resumo(chat_id):
chat_id = str(chat_id)
try:
media_id = None
texto_da_msg = "Resumo:\n"
connection = connect_to_db()
cursor = connection.cursor()
cursor.execute('''SELECT * FROM
mensagens
WHERE
CHATID = %s
ORDER BY
RANDOM()
LIMIT 5;''', [chat_id])
mensagens = cursor.fetchall()
for mensagem in mensagens:
if(str(mensagem[2]) != 'None'):
texto_da_msg += "- " + str(mensagem[2]) + "\n"
elif(str(mensagem[3]) != 'None'):
media_id = str(mensagem[3])
except (Exception, psycopg2.Error) as error :
print("Error during db.resumo() function" + str(error))
finally:
close_connection(cursor, connection)
return texto_da_msg, media_id