toxygen/toxygen/history/database.py

202 lines
6.2 KiB
Python
Raw Normal View History

2016-09-30 20:53:12 +00:00
from sqlite3 import connect
2016-05-15 20:02:05 +00:00
import os.path
2018-05-10 17:47:34 +00:00
import utils.util as util
2016-03-12 11:18:00 +00:00
2016-09-07 21:10:32 +00:00
TIMEOUT = 11
2018-03-11 21:32:46 +00:00
SAVE_MESSAGES = 500
2016-10-22 18:23:03 +00:00
MESSAGE_AUTHOR = {
2016-03-12 15:25:16 +00:00
'ME': 0,
2016-06-04 19:17:32 +00:00
'FRIEND': 1,
'NOT_SENT': 2,
'GC_PEER': 3
2016-03-12 15:25:16 +00:00
}
CONTACT_TYPE = {
'FRIEND': 0,
'GC_PEER': 1,
'GC_PEER_PRIVATE': 2
}
2016-03-12 15:25:16 +00:00
class Database:
2016-04-03 20:51:46 +00:00
def __init__(self, path, toxes):
self._path, self._toxes = path, toxes
self._name = os.path.basename(path)
2016-05-15 20:02:05 +00:00
if os.path.exists(path):
2016-06-05 11:59:36 +00:00
try:
with open(path, 'rb') as fin:
data = fin.read()
if toxes.is_data_encrypted(data):
data = toxes.pass_decrypt(data)
2016-06-05 11:59:36 +00:00
with open(path, 'wb') as fout:
fout.write(data)
except Exception as ex:
util.log('Db reading error: ' + str(ex))
2016-06-05 11:59:36 +00:00
os.remove(path)
# -----------------------------------------------------------------------------------------------------------------
# Public methods
# -----------------------------------------------------------------------------------------------------------------
2016-05-15 20:02:05 +00:00
def save(self):
if self._toxes.has_password():
with open(self._path, 'rb') as fin:
2016-05-15 20:02:05 +00:00
data = fin.read()
data = self._toxes.pass_encrypt(bytes(data))
with open(self._path, 'wb') as fout:
2016-05-15 20:02:05 +00:00
fout.write(data)
2016-03-15 20:35:15 +00:00
def export(self, directory):
new_path = util.join_path(directory, self._name)
with open(self._path, 'rb') as fin:
2016-03-15 20:35:15 +00:00
data = fin.read()
if self._toxes.has_password():
data = self._toxes.pass_encrypt(data)
2016-03-15 20:35:15 +00:00
with open(new_path, 'wb') as fout:
fout.write(data)
2016-03-12 11:18:00 +00:00
def add_friend_to_db(self, tox_id):
db = self._connect()
2016-03-12 11:18:00 +00:00
try:
cursor = db.cursor()
2018-05-15 19:51:42 +00:00
cursor.execute('CREATE TABLE IF NOT EXISTS id' + tox_id + '('
2016-03-12 11:18:00 +00:00
' id INTEGER PRIMARY KEY,'
' author_name TEXT,'
2016-03-12 11:18:00 +00:00
' message TEXT,'
2018-05-16 11:10:24 +00:00
' author_type INTEGER,'
' unix_time REAL,'
2016-03-12 11:18:00 +00:00
' message_type INTEGER'
')')
db.commit()
2016-03-12 16:22:33 +00:00
except:
2016-10-02 17:20:49 +00:00
print('Database is locked!')
2016-03-12 11:18:00 +00:00
db.rollback()
finally:
db.close()
def delete_friend_from_db(self, tox_id):
db = self._connect()
2016-03-12 11:18:00 +00:00
try:
cursor = db.cursor()
cursor.execute('DROP TABLE id' + tox_id + ';')
db.commit()
2016-03-12 16:22:33 +00:00
except:
2016-10-02 17:20:49 +00:00
print('Database is locked!')
2016-03-12 11:18:00 +00:00
db.rollback()
finally:
db.close()
def save_messages_to_db(self, tox_id, messages_iter):
db = self._connect()
2016-03-12 11:18:00 +00:00
try:
cursor = db.cursor()
cursor.executemany('INSERT INTO id' + tox_id +
2018-05-16 11:10:24 +00:00
'(message, author_name, author_type, unix_time, message_type) ' +
'VALUES (?, ?, ?, ?, ?, ?);', messages_iter)
2016-03-12 17:37:54 +00:00
db.commit()
except:
2016-10-02 17:20:49 +00:00
print('Database is locked!')
2016-03-12 17:37:54 +00:00
db.rollback()
finally:
db.close()
def update_messages(self, tox_id, message_id):
db = self._connect()
2016-06-13 21:32:45 +00:00
try:
cursor = db.cursor()
cursor.execute('UPDATE id' + tox_id + ' SET author = 0 '
2018-05-15 19:51:42 +00:00
'WHERE id = ' + str(message_id) + ' AND author = 2;')
2016-06-13 21:32:45 +00:00
db.commit()
except:
2016-10-02 17:20:49 +00:00
print('Database is locked!')
2016-06-13 21:32:45 +00:00
db.rollback()
finally:
db.close()
2016-06-07 09:27:58 +00:00
def delete_message(self, tox_id, unique_id):
db = self._connect()
try:
cursor = db.cursor()
cursor.execute('DELETE FROM id' + tox_id + ' WHERE id = ' + str(unique_id) + ';')
db.commit()
except:
2016-10-02 17:20:49 +00:00
print('Database is locked!')
db.rollback()
finally:
db.close()
2016-03-12 17:37:54 +00:00
def delete_messages(self, tox_id):
db = self._connect()
2016-03-12 17:37:54 +00:00
try:
cursor = db.cursor()
cursor.execute('DELETE FROM id' + tox_id + ';')
2016-03-12 11:18:00 +00:00
db.commit()
2016-03-12 16:22:33 +00:00
except:
2016-10-02 17:20:49 +00:00
print('Database is locked!')
2016-03-12 11:18:00 +00:00
db.rollback()
finally:
db.close()
def messages_getter(self, tox_id):
2018-05-15 19:51:42 +00:00
self.add_friend_to_db(tox_id)
2018-05-03 21:17:48 +00:00
return Database.MessageGetter(self._path, tox_id)
# -----------------------------------------------------------------------------------------------------------------
# Messages loading
# -----------------------------------------------------------------------------------------------------------------
2016-03-12 11:18:00 +00:00
2016-06-22 11:35:22 +00:00
class MessageGetter:
2016-09-07 21:10:32 +00:00
def __init__(self, path, tox_id):
2016-10-01 20:06:15 +00:00
self._count = 0
self._path = path
2016-10-01 20:06:15 +00:00
self._tox_id = tox_id
self._db = self._cursor = None
2016-03-12 15:25:16 +00:00
def get_one(self):
return self.get(1)
2016-03-12 11:18:00 +00:00
2016-03-12 15:25:16 +00:00
def get_all(self):
self._connect()
2016-10-01 20:06:15 +00:00
data = self._cursor.fetchall()
self._disconnect()
2016-10-01 20:06:15 +00:00
self._count = len(data)
return data
2016-03-12 11:18:00 +00:00
2016-03-12 15:25:16 +00:00
def get(self, count):
self._connect()
2016-10-01 20:06:15 +00:00
self.skip()
data = self._cursor.fetchmany(count)
self._disconnect()
2016-10-01 20:06:15 +00:00
self._count += len(data)
return data
def skip(self):
if self._count:
self._cursor.fetchmany(self._count)
def delete_one(self):
2016-10-02 17:20:49 +00:00
if self._count:
self._count -= 1
2018-05-15 19:51:42 +00:00
def _connect(self):
self._db = connect(self._path, timeout=TIMEOUT)
self._cursor = self._db.cursor()
2018-05-16 11:10:24 +00:00
self._cursor.execute('SELECT message, author_type, author_name, unix_time, message_type, id FROM id' +
2018-05-15 19:51:42 +00:00
self._tox_id + ' ORDER BY unix_time DESC;')
def _disconnect(self):
self._db.close()
# -----------------------------------------------------------------------------------------------------------------
# Private methods
# -----------------------------------------------------------------------------------------------------------------
def _connect(self):
return connect(self._path, timeout=TIMEOUT)