Spaces:
Sleeping
Sleeping
Update scripts/sync_bailian_models.py
Browse files- scripts/sync_bailian_models.py +39 -14
scripts/sync_bailian_models.py
CHANGED
|
@@ -4,7 +4,7 @@ import sys
|
|
| 4 |
import sqlite3
|
| 5 |
import json
|
| 6 |
import urllib.request
|
| 7 |
-
import subprocess
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
DB_PATH = "/app/server/data/freeapi.db"
|
|
@@ -15,6 +15,7 @@ def get_table_columns(conn, table):
|
|
| 15 |
return [row[1] for row in cursor.fetchall()]
|
| 16 |
|
| 17 |
def ensure_provider_table(conn):
|
|
|
|
| 18 |
cursor = conn.cursor()
|
| 19 |
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name IN ('providers', 'channels')")
|
| 20 |
tables = [row[0] for row in cursor.fetchall()]
|
|
@@ -23,7 +24,6 @@ def ensure_provider_table(conn):
|
|
| 23 |
elif 'channels' in tables:
|
| 24 |
return 'channels'
|
| 25 |
else:
|
| 26 |
-
# 创建 providers 表
|
| 27 |
cursor.execute("""
|
| 28 |
CREATE TABLE providers (
|
| 29 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -73,34 +73,64 @@ def main():
|
|
| 73 |
provider_table = ensure_provider_table(conn)
|
| 74 |
provider_id = get_or_create_bailian_provider(conn, provider_table)
|
| 75 |
|
|
|
|
| 76 |
model_columns = get_table_columns(conn, "models")
|
| 77 |
print(f"models table columns: {model_columns}")
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if not insert_cols:
|
| 81 |
-
print("❌
|
| 82 |
return 1
|
| 83 |
|
| 84 |
placeholders = ','.join(['?' for _ in insert_cols])
|
| 85 |
cursor = conn.cursor()
|
| 86 |
added = 0
|
| 87 |
for model_id in models:
|
| 88 |
-
|
|
|
|
| 89 |
if cursor.fetchone():
|
| 90 |
print(f"⏭️ Already exists: {model_id}")
|
| 91 |
continue
|
|
|
|
| 92 |
values = []
|
| 93 |
for col in insert_cols:
|
| 94 |
-
if col ==
|
| 95 |
values.append(model_id)
|
|
|
|
|
|
|
| 96 |
elif col == 'provider_id':
|
| 97 |
values.append(provider_id)
|
| 98 |
elif col == 'enabled':
|
| 99 |
values.append(1)
|
| 100 |
elif col == 'created_at':
|
| 101 |
values.append(datetime.utcnow().isoformat())
|
| 102 |
-
elif col == '
|
| 103 |
-
values.append(model_id)
|
| 104 |
sql = f"INSERT INTO models ({','.join(insert_cols)}) VALUES ({placeholders})"
|
| 105 |
cursor.execute(sql, values)
|
| 106 |
added += 1
|
|
@@ -110,16 +140,11 @@ def main():
|
|
| 110 |
conn.close()
|
| 111 |
print(f"✅ Sync completed. New models added: {added}")
|
| 112 |
|
| 113 |
-
# 如果有新增模型,立即触发一次备份,确保重启后不丢失
|
| 114 |
if added > 0:
|
| 115 |
print("Triggering immediate backup to persist new models...")
|
| 116 |
backup_script = "/app/scripts/backup_to_dataset.py"
|
| 117 |
if os.path.exists(backup_script):
|
| 118 |
-
|
| 119 |
-
if result.returncode == 0:
|
| 120 |
-
print("Backup completed successfully.")
|
| 121 |
-
else:
|
| 122 |
-
print("Backup failed, but models were added.")
|
| 123 |
else:
|
| 124 |
print("Backup script not found, skipping immediate backup.")
|
| 125 |
|
|
|
|
| 4 |
import sqlite3
|
| 5 |
import json
|
| 6 |
import urllib.request
|
| 7 |
+
import subprocess
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
DB_PATH = "/app/server/data/freeapi.db"
|
|
|
|
| 15 |
return [row[1] for row in cursor.fetchall()]
|
| 16 |
|
| 17 |
def ensure_provider_table(conn):
|
| 18 |
+
# 检查是否存在 providers 或 channels 表,如果没有则创建
|
| 19 |
cursor = conn.cursor()
|
| 20 |
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name IN ('providers', 'channels')")
|
| 21 |
tables = [row[0] for row in cursor.fetchall()]
|
|
|
|
| 24 |
elif 'channels' in tables:
|
| 25 |
return 'channels'
|
| 26 |
else:
|
|
|
|
| 27 |
cursor.execute("""
|
| 28 |
CREATE TABLE providers (
|
| 29 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
| 73 |
provider_table = ensure_provider_table(conn)
|
| 74 |
provider_id = get_or_create_bailian_provider(conn, provider_table)
|
| 75 |
|
| 76 |
+
# 获取 models 表的列
|
| 77 |
model_columns = get_table_columns(conn, "models")
|
| 78 |
print(f"models table columns: {model_columns}")
|
| 79 |
|
| 80 |
+
# 根据实际列名动态构建插入语句
|
| 81 |
+
# 我们期望的映射:模型ID -> 可能的列名
|
| 82 |
+
id_col_candidates = ['model_id', 'id', 'name']
|
| 83 |
+
id_col = None
|
| 84 |
+
for col in id_col_candidates:
|
| 85 |
+
if col in model_columns:
|
| 86 |
+
id_col = col
|
| 87 |
+
break
|
| 88 |
+
if not id_col:
|
| 89 |
+
print("❌ models table missing id column")
|
| 90 |
+
return 1
|
| 91 |
+
|
| 92 |
+
# 其他可用列
|
| 93 |
+
insert_cols = [id_col]
|
| 94 |
+
if 'platform' in model_columns:
|
| 95 |
+
insert_cols.append('platform')
|
| 96 |
+
if 'provider_id' in model_columns:
|
| 97 |
+
insert_cols.append('provider_id')
|
| 98 |
+
if 'enabled' in model_columns:
|
| 99 |
+
insert_cols.append('enabled')
|
| 100 |
+
if 'created_at' in model_columns:
|
| 101 |
+
insert_cols.append('created_at')
|
| 102 |
+
# 如果有 display_name 列,可以用作显示名称
|
| 103 |
+
if 'display_name' in model_columns:
|
| 104 |
+
insert_cols.append('display_name')
|
| 105 |
+
|
| 106 |
if not insert_cols:
|
| 107 |
+
print("❌ No columns to insert")
|
| 108 |
return 1
|
| 109 |
|
| 110 |
placeholders = ','.join(['?' for _ in insert_cols])
|
| 111 |
cursor = conn.cursor()
|
| 112 |
added = 0
|
| 113 |
for model_id in models:
|
| 114 |
+
# 检查是否存在
|
| 115 |
+
cursor.execute(f"SELECT 1 FROM models WHERE {id_col} = ?", (model_id,))
|
| 116 |
if cursor.fetchone():
|
| 117 |
print(f"⏭️ Already exists: {model_id}")
|
| 118 |
continue
|
| 119 |
+
|
| 120 |
values = []
|
| 121 |
for col in insert_cols:
|
| 122 |
+
if col == id_col:
|
| 123 |
values.append(model_id)
|
| 124 |
+
elif col == 'platform':
|
| 125 |
+
values.append('bailian')
|
| 126 |
elif col == 'provider_id':
|
| 127 |
values.append(provider_id)
|
| 128 |
elif col == 'enabled':
|
| 129 |
values.append(1)
|
| 130 |
elif col == 'created_at':
|
| 131 |
values.append(datetime.utcnow().isoformat())
|
| 132 |
+
elif col == 'display_name':
|
| 133 |
+
values.append(model_id) # 或更友好的名称
|
| 134 |
sql = f"INSERT INTO models ({','.join(insert_cols)}) VALUES ({placeholders})"
|
| 135 |
cursor.execute(sql, values)
|
| 136 |
added += 1
|
|
|
|
| 140 |
conn.close()
|
| 141 |
print(f"✅ Sync completed. New models added: {added}")
|
| 142 |
|
|
|
|
| 143 |
if added > 0:
|
| 144 |
print("Triggering immediate backup to persist new models...")
|
| 145 |
backup_script = "/app/scripts/backup_to_dataset.py"
|
| 146 |
if os.path.exists(backup_script):
|
| 147 |
+
subprocess.run(["python3", backup_script])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
else:
|
| 149 |
print("Backup script not found, skipping immediate backup.")
|
| 150 |
|