Update .gitignore, DataClass.py, and 19 more files...

오랜만에 서버 정리하고 커밋. 파일 위치를 정리했다.
캘리버 DB 를 열고 정보를 열람. Pupil 을 통해 다운받은 정보를 관리하기 위해 새로운 클래스 추가
This commit is contained in:
2025-08-01 14:57:40 +09:00
parent f1345e2770
commit d5f2d82bc9
18 changed files with 1266 additions and 1079 deletions

View File

@@ -1,134 +1,190 @@
import os
import sys
import sqlite3
import UtilPack as util
import MgrSQLiteDB as MyDB
class MgrCalibreDB:
def __init__(self, path):
self.path = path
def __enter__(self):
pass
def __exit__(self, ex_type, ex_value, traceback):
self.conn.close()
class MgrCalibreDB(MyDB.MgrSQLiteDB):
def init(self):
print(f"{self.path} DB Calibre Init")
# 데이터베이스 연결 (파일이 없으면 새로 생성됨)
self.conn = sqlite3.connect(self.path)
self.conn.create_function("title_sort", 1, self._title_sort )
self.cursor = self.conn.cursor()
#
def _title_sort(self, value):
return 0
def GetTableAll(self, table):
if None == self.cursor:
return None
if None == table or True == util.IsEmptyStr(table):
return None
strSQL = f"SELECT * FROM \"{table}\";"
self.cursor.execute(strSQL)
listRet = self.cursor.fetchall()
return listRet
def GetTableSchm(self, table):
if None == self.cursor:
return None
if None == table or True == util.IsEmptyStr(table):
return None
strSQL = f"PRAGMA table_info (\"{table}\");"
self.cursor.execute(strSQL)
listRet = self.cursor.fetchall()
return listRet
def GetBookListforUI_ArcList(self):
if None == self.cursor:
return None
try:
strSQL = f"Select title, path from books;"
self.cursor.execute(strSQL)
listRet = self.cursor.fetchall()
except sqlite3.Error as e:
listRet = []
print(f"SQLite Erro occurred: {e}")
# 테이블의 마지막 인덱스를 가져온다. 잘못되면 -1
def GetLastIndexID(self, strTableName: str) -> int :
if True == util.IsEmptyStr(strTableName):
return -1
return listRet
strSQL = f"SELECT MAX(id) FROM \'{strTableName}\';"
listMaxID = self.SQLExecute_Get(self.cursor, strSQL)
if listMaxID is None or 0 >= len(listMaxID):
util.DbgOut("Error : Invalid Table Name")
return -1
return listMaxID[0][0] + 1
def UpdateTableValue(self,Table, nID, ColName, Value):
if None == self.cursor:
return
#
def GetBookTitleByID(self, nID: int) -> str:
if 0 >= nID:
return ""
listRet = self.GetValuesOneCondition("books", "title", "id", str(nID))
if 0 >= len(listRet):
return ""
return listRet[0]
#
def GetAuthorByID(self, nID: int ) -> str:
if 0 >= nID:
return ""
listRet = self.GetValuesOneCondition("authors", "name", "id", f"{nID}")
if 0 >= len(listRet):
return ""
return listRet[0]
#
def GetAuthorsByBookID(self, nID: int ) -> list[str]:
if 0 >= nID:
return []
listAuthorID = self.GetValuesOneCondition("books_authors_link", "author", "book", f"{nID}")
print(f"GetAuthorsByBookID : {listAuthorID}")
if 0 >= len(listAuthorID):
return []
listRet = list[str]()
for strAuthorID in listAuthorID:
if True == util.IsEmptyStr(strAuthorID):
continue
if 0 >= nID or True == util.IsEmptyStr(Table) or True == util.IsEmptyStr(ColName):
return
strSQL = f"UPDATE {Table} SET \'{ColName}\' = \'{Value}\' WHERE id = {nID};"
self.cursor.execute(strSQL)
self.conn.commit()
def InsertAuthor(self, strAuthor):
if None == self.cursor:
return
nAuthorID = int(strAuthorID)
strAuthorName = self.GetAuthorByID(nAuthorID)
if True == util.IsEmptyStr(strAuthorName):
continue
if True == util.IsEmptyStr(strAuthor):
listRet.append(strAuthorName)
return listRet
#
def GetDataByBookID(self, nID: int ) -> tuple[str, int, str]:
if 0 >= nID:
return ("", 0, "")
strSQL = f"SELECT format, uncompressed_size, name FROM data WHERE book = {nID};"
listRet = self.SQLExecute_Get(self.cursor, strSQL)
if listRet is None or 0 >= len(listRet):
return ("", 0, "")
return listRet[0]
#
def GetTagByID(self, strTag: str) -> int:
if True == util.IsEmptyStr(strTag):
return -1
listRet = self.GetValuesOneCondition("tags", "id", "name", strTag)
if 0 >= len(listRet):
return -1
return int(listRet[0])
# title, author, cover, ext(path), id
# id, title, has_cover, path, And author
def GetBookListforUI_ArcList(self) -> list[tuple[str, str, str, str, str]]:
strSQL = f"Select id, title, path, has_cover from books;"
listResult = self.SQLExecute_Get(self.cursor, strSQL)
if listResult is None or 0 >= len(listResult):
return []
listRet = list[tuple[str, str, str, str, str]]()
for item in listResult:
nID = item[0]
strTitle = item[1]
strPath = item[2]
nHasCover = item[3]
if 0 >= nID:
continue
listAuthors = self.GetAuthorsByBookID(nID)
strAuthor = ", ".join(listAuthors) if listAuthors else "Unknown"
listRet.append((strTitle, strAuthor, str(nHasCover), strPath, str(nID)))
return listRet
#
def InsertAuthor(self, strAuthor: str):
if True == util.IsEmptyStr(strAuthor):
return
strTableName = "authors"
strSQL = f"SELECT MAX(id) FROM \'{strTableName}\';"
self.cursor.execute(strSQL)
nMaxID = self.cursor.fetchone()[0]
nMaxID += 1
strTableName = "authors"
nMaxID = self.GetLastIndexID(strTableName)
if 0 >= nMaxID:
util.DbgOut("Error : Invalid Table Name")
return
strAuthorSort = strAuthor.replace(" ", ",")
strSQL = f"INSERT OR IGNORE INTO \'{strTableName}\' VALUES ({nMaxID}, \'{strAuthor}\',\'{strAuthorSort}\', \'\');"
self.cursor.execute(strSQL)
self.conn.commit()
self.SQLExecute(self.cursor, strSQL, True)
def InsertTag(self, tag):
if None == self.cursor:
return
if True == util.IsEmptyStr(tag):
#
def InsertTag(self, strTag: str):
if True == util.IsEmptyStr(strTag):
return
strTableName = "tags"
strSQL = f"SELECT MAX(id) FROM \'{strTableName}\';"
self.cursor.execute(strSQL)
nMaxID = self.cursor.fetchone()[0]
nMaxID += 1.
nMaxID = self.GetLastIndexID(strTableName)
if 0 >= nMaxID:
util.DbgOut("Error : Invalid Table Name")
return
strSQL = f"INSERT OR IGNORE INTO \'{strTableName}\' VALUES ({nMaxID}, \'{strAuthor}\', \'\');"
self.cursor.execute(strSQL)
self.conn.commit()
strSQL = f"INSERT OR IGNORE INTO \'{strTableName}\' VALUES ({nMaxID}, \'{strTag}\', \'\');"
self.SQLExecute(self.cursor, strSQL, True)
"""
""" #Example usage
def main():
db = MgrCalibreDB("/Users/minarinari/캘리버 서재/metadata.db")
db.init()
#db.UpdateTableValue("books", 3, "title", "삭막")
db.InsertAuthor("Oyster")
db.InsertAuthor("Rustle")
db.InsertAuthor("ShindoL")
listValues = db.GetTableAll("authors")
listValues = db.GetTableAllData("authors")
print(listValues)
db.InsertTag("female:Bondage")
listValues2 = db.GetTableAllData("Tags")
print(listValues2)
# For Main Loop
if __name__ == '__main__':
main()
"""
#coments table
# 자체 id, book id, 코멘트...<- 여기다 json 을 넣자.
# id, book ,comment
# data table
# 자체 id, book id, 파일 포멧, cbz, 크기, 파일 이름
# id, book, format, uncompressed_size, name
# 테이블 생성
#cursor.execute('''CREATE TABLE IF NOT EXISTS users