首次提交
This commit is contained in:
commit
1d26ce5a3d
74 files changed
+3632
No files matched your search
@@ -0,0 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@File : __init__.py.py
|
||||
@Author : Shuaikang Zhou
|
||||
@Time : 2023/1/5 0:10
|
||||
@IDE : Pycharm
|
||||
@Version : Python3.10
|
||||
@comment : ···
|
||||
"""
|
||||
from .micro_msg import MicroMsg
|
||||
from .misc import Misc
|
||||
from .msg import Msg
|
||||
from .sns import Sns
|
||||
|
||||
misc_db = Misc()
|
||||
msg_db = Msg()
|
||||
micro_msg_db = MicroMsg()
|
||||
sns_db = Sns()
|
||||
|
||||
|
||||
__all__ = ['misc_db', 'micro_msg_db', 'msg_db', "sns_db"]
|
||||
@@ -0,0 +1,137 @@
|
||||
import os.path
|
||||
import sqlite3
|
||||
import threading
|
||||
|
||||
lock = threading.Lock()
|
||||
db_path = "./app/Database/Msg/MicroMsg.db"
|
||||
|
||||
|
||||
def singleton(cls):
|
||||
_instance = {}
|
||||
|
||||
def inner():
|
||||
if cls not in _instance:
|
||||
_instance[cls] = cls()
|
||||
return _instance[cls]
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
def is_database_exist():
|
||||
return os.path.exists(db_path)
|
||||
|
||||
@singleton
|
||||
class MicroMsg:
|
||||
def __init__(self):
|
||||
self.DB = None
|
||||
self.cursor = None
|
||||
self.open_flag = False
|
||||
self.init_database()
|
||||
|
||||
def init_database(self):
|
||||
if not self.open_flag:
|
||||
if os.path.exists(db_path):
|
||||
self.DB = sqlite3.connect(db_path, check_same_thread=False)
|
||||
# '''创建游标'''
|
||||
self.cursor = self.DB.cursor()
|
||||
self.open_flag = True
|
||||
if lock.locked():
|
||||
lock.release()
|
||||
|
||||
def get_contact(self):
|
||||
if not self.open_flag:
|
||||
return None
|
||||
try:
|
||||
lock.acquire(True)
|
||||
sql = '''SELECT UserName, Alias, Type, Remark, NickName, PYInitial, RemarkPYInitial, ContactHeadImgUrl.smallHeadImgUrl, ContactHeadImgUrl.bigHeadImgUrl,ExTraBuf,COALESCE(ContactLabel.LabelName, 'None') AS labelName
|
||||
FROM Contact
|
||||
INNER JOIN ContactHeadImgUrl ON Contact.UserName = ContactHeadImgUrl.usrName
|
||||
LEFT JOIN ContactLabel ON Contact.LabelIDList = ContactLabel.LabelId
|
||||
WHERE (Type!=4 AND VerifyFlag=0)
|
||||
AND NickName != ''
|
||||
ORDER BY
|
||||
CASE
|
||||
WHEN RemarkPYInitial = '' THEN PYInitial
|
||||
ELSE RemarkPYInitial
|
||||
END ASC
|
||||
'''
|
||||
self.cursor.execute(sql)
|
||||
result = self.cursor.fetchall()
|
||||
except sqlite3.OperationalError:
|
||||
# 解决ContactLabel表不存在的问题
|
||||
# lock.acquire(True)
|
||||
sql = '''
|
||||
SELECT UserName, Alias, Type, Remark, NickName, PYInitial, RemarkPYInitial, ContactHeadImgUrl.smallHeadImgUrl, ContactHeadImgUrl.bigHeadImgUrl,ExTraBuf,"None"
|
||||
FROM Contact
|
||||
INNER JOIN ContactHeadImgUrl ON Contact.UserName = ContactHeadImgUrl.usrName
|
||||
WHERE (Type!=4 AND VerifyFlag=0)
|
||||
AND NickName != ''
|
||||
ORDER BY
|
||||
CASE
|
||||
WHEN RemarkPYInitial = '' THEN PYInitial
|
||||
ELSE RemarkPYInitial
|
||||
END ASC
|
||||
'''
|
||||
self.cursor.execute(sql)
|
||||
result = self.cursor.fetchall()
|
||||
finally:
|
||||
lock.release()
|
||||
from app.DataBase import msg_db
|
||||
return msg_db.get_contact(result)
|
||||
|
||||
def get_contact_by_username(self, username: object) -> object:
|
||||
if not self.open_flag:
|
||||
return None
|
||||
try:
|
||||
lock.acquire(True)
|
||||
sql = '''
|
||||
SELECT UserName, Alias, Type, Remark, NickName, PYInitial, RemarkPYInitial, ContactHeadImgUrl.smallHeadImgUrl, ContactHeadImgUrl.bigHeadImgUrl,ExTraBuf,ContactLabel.LabelName
|
||||
FROM Contact
|
||||
INNER JOIN ContactHeadImgUrl ON Contact.UserName = ContactHeadImgUrl.usrName
|
||||
LEFT JOIN ContactLabel ON Contact.LabelIDList = ContactLabel.LabelId
|
||||
WHERE UserName = ?
|
||||
'''
|
||||
self.cursor.execute(sql, [username])
|
||||
result = self.cursor.fetchone()
|
||||
except sqlite3.OperationalError:
|
||||
# 解决ContactLabel表不存在的问题
|
||||
# lock.acquire(True)
|
||||
sql = '''
|
||||
SELECT UserName, Alias, Type, Remark, NickName, PYInitial, RemarkPYInitial, ContactHeadImgUrl.smallHeadImgUrl, ContactHeadImgUrl.bigHeadImgUrl,ExTraBuf,"None"
|
||||
FROM Contact
|
||||
INNER JOIN ContactHeadImgUrl ON Contact.UserName = ContactHeadImgUrl.usrName
|
||||
WHERE UserName = ?
|
||||
'''
|
||||
self.cursor.execute(sql, [username])
|
||||
result = self.cursor.fetchone()
|
||||
finally:
|
||||
lock.release()
|
||||
|
||||
return result
|
||||
|
||||
def get_chatroom_info(self, chatroomname):
|
||||
'''
|
||||
获取群聊信息
|
||||
'''
|
||||
if not self.open_flag:
|
||||
return None
|
||||
try:
|
||||
lock.acquire(True)
|
||||
sql = '''SELECT ChatRoomName, RoomData FROM ChatRoom WHERE ChatRoomName = ?'''
|
||||
self.cursor.execute(sql, [chatroomname])
|
||||
result = self.cursor.fetchone()
|
||||
finally:
|
||||
lock.release()
|
||||
return result
|
||||
|
||||
def close(self):
|
||||
if self.open_flag:
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.open_flag = False
|
||||
self.DB.close()
|
||||
finally:
|
||||
lock.release()
|
||||
|
||||
def __del__(self):
|
||||
self.close()
|
||||
@@ -0,0 +1,74 @@
|
||||
import os.path
|
||||
import sqlite3
|
||||
import threading
|
||||
|
||||
lock = threading.Lock()
|
||||
DB = None
|
||||
cursor = None
|
||||
db_path = "./app/Database/Msg/Misc.db"
|
||||
|
||||
|
||||
# db_path = './Msg/Misc.db'
|
||||
|
||||
|
||||
def singleton(cls):
|
||||
_instance = {}
|
||||
|
||||
def inner():
|
||||
if cls not in _instance:
|
||||
_instance[cls] = cls()
|
||||
return _instance[cls]
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
@singleton
|
||||
class Misc:
|
||||
def __init__(self):
|
||||
self.DB = None
|
||||
self.cursor = None
|
||||
self.open_flag = False
|
||||
self.init_database()
|
||||
|
||||
def init_database(self):
|
||||
if not self.open_flag:
|
||||
if os.path.exists(db_path):
|
||||
self.DB = sqlite3.connect(db_path, check_same_thread=False)
|
||||
# '''创建游标'''
|
||||
self.cursor = self.DB.cursor()
|
||||
self.open_flag = True
|
||||
if lock.locked():
|
||||
lock.release()
|
||||
|
||||
def get_avatar_buffer(self, userName):
|
||||
if not self.open_flag:
|
||||
return None
|
||||
sql = '''
|
||||
select smallHeadBuf
|
||||
from ContactHeadImg1
|
||||
where usrName=?;
|
||||
'''
|
||||
if not self.open_flag:
|
||||
self.init_database()
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.cursor.execute(sql, [userName])
|
||||
result = self.cursor.fetchall()
|
||||
if result:
|
||||
return result[0][0]
|
||||
finally:
|
||||
lock.release()
|
||||
return None
|
||||
|
||||
def close(self):
|
||||
if self.open_flag:
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.open_flag = False
|
||||
self.DB.close()
|
||||
finally:
|
||||
lock.release()
|
||||
|
||||
def __del__(self):
|
||||
self.close()
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
import os.path
|
||||
import random
|
||||
import sqlite3
|
||||
import threading
|
||||
import traceback
|
||||
db_path = "./app/Database/Msg/MSG.db"
|
||||
lock = threading.Lock()
|
||||
|
||||
|
||||
def is_database_exist():
|
||||
return os.path.exists(db_path)
|
||||
|
||||
|
||||
def singleton(cls):
|
||||
_instance = {}
|
||||
|
||||
def inner():
|
||||
if cls not in _instance:
|
||||
_instance[cls] = cls()
|
||||
return _instance[cls]
|
||||
|
||||
return inner
|
||||
@singleton
|
||||
class Msg:
|
||||
def __init__(self):
|
||||
self.DB = None
|
||||
self.cursor = None
|
||||
self.open_flag = False
|
||||
self.init_database()
|
||||
|
||||
def init_database(self, path=None):
|
||||
global db_path
|
||||
if not self.open_flag:
|
||||
if path:
|
||||
db_path = path
|
||||
if os.path.exists(db_path):
|
||||
self.DB = sqlite3.connect(db_path, check_same_thread=False)
|
||||
# '''创建游标'''
|
||||
self.cursor = self.DB.cursor()
|
||||
self.open_flag = True
|
||||
if lock.locked():
|
||||
lock.release()
|
||||
|
||||
def get_contact(self, contacts):
|
||||
"""这里查了一遍聊天记录,根据聊天记录最后一条按时间
|
||||
对联系人进行排序
|
||||
"""
|
||||
if not self.open_flag:
|
||||
return None
|
||||
try:
|
||||
lock.acquire(True)
|
||||
sql = '''select StrTalker, MAX(CreateTime) from MSG group by StrTalker'''
|
||||
self.cursor.execute(sql)
|
||||
res = self.cursor.fetchall()
|
||||
finally:
|
||||
lock.release()
|
||||
res = {StrTalker: CreateTime for StrTalker, CreateTime in res}
|
||||
contacts = [list(cur_contact) for cur_contact in contacts]
|
||||
for i, cur_contact in enumerate(contacts):
|
||||
if cur_contact[0] in res:
|
||||
contacts[i].append(res[cur_contact[0]])
|
||||
else:
|
||||
contacts[i].append(0)
|
||||
contacts.sort(key=lambda cur_contact: cur_contact[-1], reverse=True)
|
||||
return contacts
|
||||
|
||||
def get_messages_calendar(self, username_):
|
||||
sql = '''
|
||||
SELECT strftime('%Y-%m-%d',CreateTime,'unixepoch','localtime') as days
|
||||
from (
|
||||
SELECT MsgSvrID, CreateTime
|
||||
FROM MSG
|
||||
WHERE StrTalker = ?
|
||||
ORDER BY CreateTime
|
||||
)
|
||||
group by days
|
||||
'''
|
||||
if not self.open_flag:
|
||||
print('数据库未就绪')
|
||||
return None
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.cursor.execute(sql, [username_])
|
||||
result = self.cursor.fetchall()
|
||||
finally:
|
||||
lock.release()
|
||||
return [date[0] for date in result]
|
||||
|
||||
def close(self):
|
||||
if self.open_flag:
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.open_flag = False
|
||||
self.DB.close()
|
||||
finally:
|
||||
lock.release()
|
||||
|
||||
def __del__(self):
|
||||
self.close()
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
import os.path
|
||||
import random
|
||||
import sqlite3
|
||||
import threading
|
||||
import traceback
|
||||
from typing import Optional
|
||||
|
||||
db_path = "./app/Database/Msg/Sns.db"
|
||||
lock = threading.Lock()
|
||||
|
||||
|
||||
def is_database_exist():
|
||||
return os.path.exists(db_path)
|
||||
|
||||
|
||||
def singleton(cls):
|
||||
_instance = {}
|
||||
|
||||
def inner():
|
||||
if cls not in _instance:
|
||||
_instance[cls] = cls()
|
||||
return _instance[cls]
|
||||
|
||||
return inner
|
||||
|
||||
@singleton
|
||||
class Sns:
|
||||
def __init__(self):
|
||||
self.DB = None
|
||||
self.cursor = None
|
||||
self.open_flag = False
|
||||
self.init_database()
|
||||
|
||||
def init_database(self, path=None):
|
||||
global db_path
|
||||
if not self.open_flag:
|
||||
if path:
|
||||
db_path = path
|
||||
if os.path.exists(db_path):
|
||||
self.DB = sqlite3.connect(db_path, check_same_thread=False)
|
||||
# '''创建游标'''
|
||||
self.cursor = self.DB.cursor()
|
||||
self.open_flag = True
|
||||
if lock.locked():
|
||||
lock.release()
|
||||
|
||||
def get_messages_in_time(self, start_time, end_time):
|
||||
if not self.open_flag:
|
||||
return None
|
||||
try:
|
||||
lock.acquire(True)
|
||||
sql = '''select UserName, Content from FeedsV20 where CreateTime>=?
|
||||
and CreateTime<=? order by CreateTime desc'''
|
||||
self.cursor.execute(sql, [start_time, end_time])
|
||||
res = self.cursor.fetchall()
|
||||
finally:
|
||||
lock.release()
|
||||
|
||||
return res
|
||||
|
||||
def get_cover_url(self) -> Optional[str]:
|
||||
if not self.open_flag:
|
||||
return None
|
||||
try:
|
||||
lock.acquire(True)
|
||||
sql = '''select StrValue from SnsConfigV20 where Key="6" '''
|
||||
self.cursor.execute(sql)
|
||||
result = self.cursor.fetchall()
|
||||
if result:
|
||||
return result[0][0]
|
||||
finally:
|
||||
lock.release()
|
||||
return None
|
||||
|
||||
|
||||
def close(self):
|
||||
if self.open_flag:
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.open_flag = False
|
||||
self.DB.close()
|
||||
finally:
|
||||
lock.release()
|
||||
|
||||
def __del__(self):
|
||||
self.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pass
|
||||
Reference in new issue
Block a user