-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_connector.py
More file actions
101 lines (81 loc) · 3.4 KB
/
sql_connector.py
File metadata and controls
101 lines (81 loc) · 3.4 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
from datetime import datetime
import psycopg2 as psycopg2
import configparser
class DbHelper(object):
config = configparser.ConfigParser()
config.sections()
config.read('db.ini')
def __init__(self, config):
self.host = config['DB_CONNECTION']['host']
self.port = config['DB_CONNECTION']['port']
self.database = config['DB_CONNECTION']['database']
self.user = config['DB_CONNECTION']['user']
self.password = config['DB_CONNECTION']['password']
def create_connection(self):
return psycopg2.connect(host=self.host,
port=self.port,
database=self.database,
user=self.user,
password=self.password)
def insertMessage (self, user_id, message_id, message_text, notifi_datetime):
connection = self.create_connection()
cursor = connection.cursor()
with connection:
cursor.execute("""INSERT INTO messages(
user_id, message_id, message_text, notifi_datetime, status)
VALUES (?, ?, ?, ?, 0)""", (user_id, message_id, message_text, notifi_datetime))
connection.commit()
def getMessageById (self, uid):
connection = self.create_connection()
cursor = connection.cursor()
with connection:
cursor.execute("""SELECT * FROM messages where id =?""", (str(uid)))
return cursor.fetchone()
def getActiveMessages (self):
connection = self.create_connection()
cursor = connection.cursor()
with connection:
cursor.execute("""SELECT * FROM messages where status = 0""")
return cursor.fetchall()
def setMessageSent (self, db_msg_id):
connection = self.create_connection()
cursor = connection.cursor()
with connection:
cursor.execute("""UPDATE messages SET status = 1 where id = ?""", (str(db_msg_id),))
connection.commit()
def getConfig (self):
connection = self.create_connection()
cursor = connection.cursor()
with connection:
cursor.execute("""select value from public.settings where "name" = 'token' or "name" = 'proxy';""")
records = cursor.fetchall()
token = records[0][0]
proxy = records[1][0]
return token, proxy
def getGMT(self, id):
connection = self.create_connection()
cursor = connection.cursor()
with connection:
cursor.execute("SELECT GMT FROM GMT WHERE ID=?", (id,))
try:
return cursor.fetchone()[0]
except TypeError:
return '0'
def get_show_time(self, uid, tg_server_time):
tg_server_time = datetime.datetime.utcfromtimestamp(int(tg_server_time))
connection = self.create_connection()
cursor = connection.cursor()
with connection:
cursor.execute("SELECT GMT FROM GMT WHERE ID=?", (uid,))
try:
gmt_val = cursor.fetchone()[0]
except TypeError:
gmt_val = '0'
if gmt_val.count('+') == 1:
hours = int(gmt_val.replace('+', ''))
return tg_server_time + datetime.timedelta(hours=hours)
elif gmt_val.count('-') == 1:
hours = int(gmt_val.replace('-', ''))
return tg_server_time - datetime.timedelta(hours=hours)
else:
return tg_server_time