modify scripts
This commit is contained in:
@ -1,121 +0,0 @@
|
||||
import sqlite3
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime
|
||||
import src.config.config as config
|
||||
|
||||
# 连接 SQLite 数据库
|
||||
DB_PATH = f"{config.global_share_data_dir}/sqlite/shared.db" # 替换为你的数据库文件
|
||||
|
||||
# 检查 SQLite 版本
|
||||
lower_sqlite_version = False
|
||||
sqlite_version = sqlite3.sqlite_version_info
|
||||
if sqlite_version < (3, 24, 0):
|
||||
lower_sqlite_version = True
|
||||
|
||||
# 获取表的列名和默认值
|
||||
def get_table_columns_and_defaults(cursor, tbl_name):
|
||||
try:
|
||||
cursor.execute(f"PRAGMA table_info({tbl_name})")
|
||||
columns = cursor.fetchall()
|
||||
column_info = {}
|
||||
for col in columns:
|
||||
col_name = col[1]
|
||||
default_value = col[4]
|
||||
column_info[col_name] = default_value
|
||||
return column_info
|
||||
except sqlite3.Error as e:
|
||||
logging.error(f"Error getting table columns: {e}")
|
||||
return None
|
||||
|
||||
# 检查并处理数据
|
||||
def check_and_process_data(cursor, data, tbl_name):
|
||||
column_info = get_table_columns_and_defaults(cursor=cursor, tbl_name=tbl_name)
|
||||
if column_info is None:
|
||||
return None
|
||||
processed_data = {}
|
||||
for col, default in column_info.items():
|
||||
if col == 'id' or col == 'created_at': # 自增主键,不需要用户提供; 创建日期,使用建表默认值
|
||||
continue
|
||||
if col == 'updated_at': # 日期函数,用户自己指定即可
|
||||
processed_data[col] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
if col in data:
|
||||
processed_data[col] = data[col]
|
||||
|
||||
return processed_data
|
||||
|
||||
|
||||
# 插入或更新数据
|
||||
def insert_or_update_common(cursor, conn, data, tbl_name, uniq_key='url'):
|
||||
if lower_sqlite_version:
|
||||
return insert_or_update_common_lower(cursor, conn, data, tbl_name, uniq_key)
|
||||
|
||||
try:
|
||||
processed_data = check_and_process_data(cursor, data, tbl_name)
|
||||
if processed_data is None:
|
||||
return None
|
||||
|
||||
columns = ', '.join(processed_data.keys())
|
||||
values = list(processed_data.values())
|
||||
placeholders = ', '.join(['?' for _ in values])
|
||||
update_clause = ', '.join([f"{col}=EXCLUDED.{col}" for col in processed_data.keys() if col != uniq_key])
|
||||
|
||||
sql = f'''
|
||||
INSERT INTO {tbl_name} ({columns})
|
||||
VALUES ({placeholders})
|
||||
ON CONFLICT ({uniq_key}) DO UPDATE SET {update_clause}
|
||||
'''
|
||||
cursor.execute(sql, values)
|
||||
conn.commit()
|
||||
|
||||
# 获取插入或更新后的 report_id
|
||||
cursor.execute(f"SELECT id FROM {tbl_name} WHERE {uniq_key} = ?", (data[uniq_key],))
|
||||
report_id = cursor.fetchone()[0]
|
||||
return report_id
|
||||
except sqlite3.Error as e:
|
||||
logging.error(f"Error inserting or updating data: {e}")
|
||||
return None
|
||||
|
||||
# 插入或更新数据
|
||||
def insert_or_update_common_lower(cursor, conn, data, tbl_name, uniq_key='url'):
|
||||
try:
|
||||
processed_data = check_and_process_data(cursor, data, tbl_name)
|
||||
if processed_data is None:
|
||||
return None
|
||||
|
||||
columns = ', '.join(processed_data.keys())
|
||||
values = list(processed_data.values())
|
||||
placeholders = ', '.join(['?' for _ in values])
|
||||
|
||||
# 先尝试插入数据
|
||||
try:
|
||||
sql = f'''
|
||||
INSERT INTO {tbl_name} ({columns})
|
||||
VALUES ({placeholders})
|
||||
'''
|
||||
cursor.execute(sql, values)
|
||||
conn.commit()
|
||||
except sqlite3.IntegrityError: # 唯一键冲突,执行更新操作
|
||||
update_clause = ', '.join([f"{col}=?" for col in processed_data.keys() if col != uniq_key])
|
||||
update_values = [processed_data[col] for col in processed_data.keys() if col != uniq_key]
|
||||
update_values.append(data[uniq_key])
|
||||
sql = f"UPDATE {tbl_name} SET {update_clause} WHERE {uniq_key} = ?"
|
||||
cursor.execute(sql, update_values)
|
||||
conn.commit()
|
||||
|
||||
# 获取插入或更新后的 report_id
|
||||
cursor.execute(f"SELECT id FROM {tbl_name} WHERE {uniq_key} = ?", (data[uniq_key],))
|
||||
report_id = cursor.fetchone()[0]
|
||||
return report_id
|
||||
except sqlite3.Error as e:
|
||||
logging.error(f"Error inserting or updating data: {e}")
|
||||
return None
|
||||
|
||||
|
||||
# 测试代码
|
||||
if __name__ == "__main__":
|
||||
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
|
||||
tbl_name_actors = 'javhd_models'
|
||||
print(get_table_columns_and_defaults(cursor, tbl_name_actors))
|
||||
File diff suppressed because it is too large
Load Diff
204
src/db_utils/sqlite_db.py
Normal file
204
src/db_utils/sqlite_db.py
Normal file
@ -0,0 +1,204 @@
|
||||
import sqlite3
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime
|
||||
import src.config.config as config
|
||||
|
||||
default_dbpath = f"{config.global_share_data_dir}/sqlite/shared.db"
|
||||
|
||||
# 数据库基类,封装了通用的操作。
|
||||
class DatabaseHandler:
|
||||
def __init__(self, db_path=None):
|
||||
# 使用传入的 db_path 或默认路径
|
||||
self.DB_PATH = db_path or default_dbpath
|
||||
|
||||
# 验证路径是否存在(可选)
|
||||
if db_path and not os.path.exists(os.path.dirname(db_path)):
|
||||
os.makedirs(os.path.dirname(db_path))
|
||||
|
||||
self.conn = sqlite3.connect(self.DB_PATH, check_same_thread=False)
|
||||
self.cursor = self.conn.cursor()
|
||||
|
||||
# 检查 SQLite 版本
|
||||
self.lower_sqlite_version = False
|
||||
sqlite_version = sqlite3.sqlite_version_info
|
||||
if sqlite_version < (3, 24, 0):
|
||||
self.lower_sqlite_version = True
|
||||
|
||||
def get_table_columns_and_defaults(self, tbl_name):
|
||||
try:
|
||||
self.cursor.execute(f"PRAGMA table_info({tbl_name})")
|
||||
columns = self.cursor.fetchall()
|
||||
column_info = {}
|
||||
for col in columns:
|
||||
col_name = col[1]
|
||||
default_value = col[4]
|
||||
column_info[col_name] = default_value
|
||||
return column_info
|
||||
except sqlite3.Error as e:
|
||||
logging.error(f"Error getting table columns: {e}")
|
||||
return None
|
||||
|
||||
def check_and_process_data(self, data, tbl_name):
|
||||
column_info = self.get_table_columns_and_defaults(tbl_name)
|
||||
if column_info is None:
|
||||
return None
|
||||
processed_data = {}
|
||||
for col, default in column_info.items():
|
||||
if col == 'id' or col == 'created_at': # 自增主键,不需要用户提供; 创建日期,使用建表默认值
|
||||
continue
|
||||
if col == 'updated_at': # 日期函数,用户自己指定即可
|
||||
processed_data[col] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
if col in data:
|
||||
processed_data[col] = data[col]
|
||||
|
||||
return processed_data
|
||||
|
||||
def insert_or_update_common(self, data, tbl_name, uniq_key='url'):
|
||||
if self.lower_sqlite_version:
|
||||
return self.insert_or_update_common_lower(data, tbl_name, uniq_key)
|
||||
|
||||
try:
|
||||
processed_data = self.check_and_process_data(data, tbl_name)
|
||||
if processed_data is None:
|
||||
return None
|
||||
|
||||
columns = ', '.join(processed_data.keys())
|
||||
values = list(processed_data.values())
|
||||
placeholders = ', '.join(['?' for _ in values])
|
||||
update_clause = ', '.join([f"{col}=EXCLUDED.{col}" for col in processed_data.keys() if col != uniq_key])
|
||||
|
||||
sql = f'''
|
||||
INSERT INTO {tbl_name} ({columns})
|
||||
VALUES ({placeholders})
|
||||
ON CONFLICT ({uniq_key}) DO UPDATE SET {update_clause}
|
||||
'''
|
||||
self.cursor.execute(sql, values)
|
||||
self.conn.commit()
|
||||
|
||||
# 获取插入或更新后的 report_id
|
||||
self.cursor.execute(f"SELECT id FROM {tbl_name} WHERE {uniq_key} = ?", (data[uniq_key],))
|
||||
report_id = self.cursor.fetchone()[0]
|
||||
return report_id
|
||||
except sqlite3.Error as e:
|
||||
logging.error(f"Error inserting or updating data: {e}")
|
||||
return None
|
||||
|
||||
def insert_or_update_common_lower(self, data, tbl_name, uniq_key='url'):
|
||||
try:
|
||||
processed_data = self.check_and_process_data(data, tbl_name)
|
||||
if processed_data is None:
|
||||
return None
|
||||
|
||||
columns = ', '.join(processed_data.keys())
|
||||
values = list(processed_data.values())
|
||||
placeholders = ', '.join(['?' for _ in values])
|
||||
|
||||
# 先尝试插入数据
|
||||
try:
|
||||
sql = f'''
|
||||
INSERT INTO {tbl_name} ({columns})
|
||||
VALUES ({placeholders})
|
||||
'''
|
||||
self.cursor.execute(sql, values)
|
||||
self.conn.commit()
|
||||
except sqlite3.IntegrityError: # 唯一键冲突,执行更新操作
|
||||
update_clause = ', '.join([f"{col}=?" for col in processed_data.keys() if col != uniq_key])
|
||||
update_values = [processed_data[col] for col in processed_data.keys() if col != uniq_key]
|
||||
update_values.append(data[uniq_key])
|
||||
sql = f"UPDATE {tbl_name} SET {update_clause} WHERE {uniq_key} = ?"
|
||||
self.cursor.execute(sql, update_values)
|
||||
self.conn.commit()
|
||||
|
||||
# 获取插入或更新后的 report_id
|
||||
self.cursor.execute(f"SELECT id FROM {tbl_name} WHERE {uniq_key} = ?", (data[uniq_key],))
|
||||
report_id = self.cursor.fetchone()[0]
|
||||
return report_id
|
||||
except sqlite3.Error as e:
|
||||
logging.error(f"Error inserting or updating data: {e}")
|
||||
return None
|
||||
|
||||
def insert_task_log(self):
|
||||
return 1
|
||||
|
||||
def update_task_log(self, task_id, task_status):
|
||||
return 1
|
||||
|
||||
def finalize_task_log(self, task_id):
|
||||
return 1
|
||||
|
||||
def close(self):
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
|
||||
|
||||
# javbus 类
|
||||
class JavbusDBHandler(DatabaseHandler):
|
||||
def __init__(self, db_path=None):
|
||||
super().__init__(db_path)
|
||||
self.tbl_name_actors = 'javbus_actors'
|
||||
|
||||
def insert_actor_index(self, data, uncensored=0, from_actor_list=0, from_movie_list=0):
|
||||
data['uncensored'] = uncensored
|
||||
if from_actor_list:
|
||||
data['from_actor_list'] = from_actor_list
|
||||
if from_movie_list:
|
||||
data['from_movie_list'] = from_movie_list
|
||||
try:
|
||||
return self.insert_or_update_common(data, self.tbl_name_actors, uniq_key='href')
|
||||
except sqlite3.Error as e:
|
||||
logging.error(f"Error inserting or updating data: {e}")
|
||||
return None
|
||||
|
||||
def update_actor_detail(self, data, is_full_data=1):
|
||||
try:
|
||||
data['is_full_data'] = is_full_data
|
||||
return self.insert_or_update_common(data, self.tbl_name_actors, uniq_key='href')
|
||||
except sqlite3.Error as e:
|
||||
logging.error(f"Error inserting or updating data: {e}")
|
||||
return None
|
||||
|
||||
def query_actors(self, **filters):
|
||||
try:
|
||||
sql = f"SELECT url, en_name as name FROM {self.tbl_name_actors} WHERE 1=1"
|
||||
params = []
|
||||
|
||||
conditions = {
|
||||
"id": " AND id = ?",
|
||||
"url": " AND href = ?",
|
||||
"en_name": " AND name LIKE ?",
|
||||
"is_full_data": " AND is_full_data = ?",
|
||||
"start_id": " AND id > ?",
|
||||
}
|
||||
|
||||
for key, condition in conditions.items():
|
||||
if key in filters:
|
||||
sql += condition
|
||||
if key == "en_name":
|
||||
params.append(f"%{filters[key]}%")
|
||||
else:
|
||||
params.append(filters[key])
|
||||
|
||||
for key in ["is_full_data_in", "is_full_data_not_in"]:
|
||||
if key in filters:
|
||||
values = filters[key]
|
||||
if values:
|
||||
placeholders = ", ".join(["?"] * len(values))
|
||||
operator = "IN" if key == "is_full_data_in" else "NOT IN"
|
||||
sql += f" AND is_full_data {operator} ({placeholders})"
|
||||
params.extend(values)
|
||||
|
||||
if "order_by" in filters:
|
||||
# 注意:这里 order by 后面直接跟字段名,不能用占位符,否则会被当作字符串处理
|
||||
sql += f" ORDER BY {filters['order_by']} "
|
||||
|
||||
if 'limit' in filters:
|
||||
sql += " LIMIT ?"
|
||||
params.append(filters["limit"])
|
||||
|
||||
self.cursor.execute(sql, params)
|
||||
return [{'url': row[0], 'name': row[1]} for row in self.cursor.fetchall()]
|
||||
except sqlite3.Error as e:
|
||||
logging.error(f"查询 href 失败: {e}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user