90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
import sqlite3
|
|
from pathlib import Path
|
|
|
|
DB_PATH = Path("facebook_marketplace.db")
|
|
|
|
def get_connection():
|
|
conn = sqlite3.connect(DB_PATH)
|
|
conn.row_factory = sqlite3.Row
|
|
return conn
|
|
|
|
def create_tables():
|
|
conn = get_connection()
|
|
cursor = conn.cursor()
|
|
|
|
# Table quản lý tài khoản Facebook
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS accounts (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
email TEXT NOT NULL UNIQUE,
|
|
password TEXT NOT NULL,
|
|
is_active INTEGER DEFAULT 1,
|
|
login_at INTEGER DEFAULT NULL
|
|
)
|
|
''')
|
|
|
|
# Table quản lý sản phẩm
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS products (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT,
|
|
price REAL,
|
|
url TEXT,
|
|
status INTEGER DEFAULT 0, -- 0 = draft, 1 = published
|
|
images TEXT,
|
|
created_at INTEGER,
|
|
category TEXT DEFAULT NULL,
|
|
condition TEXT DEFAULT NULL,
|
|
brand TEXT,
|
|
description TEXT,
|
|
tags TEXT DEFAULT NULL, -- lưu dưới dạng JSON string
|
|
sku TEXT UNIQUE,
|
|
location TEXT DEFAULT NULL
|
|
)
|
|
""")
|
|
|
|
# Table quản lý sản phẩm đã listed bởi account
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS listed (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
account_id INTEGER NOT NULL,
|
|
product_id INTEGER NOT NULL,
|
|
listed_at INTEGER,
|
|
status TEXT DEFAULT 'pending', -- 'pending' hoặc 'listed'
|
|
UNIQUE(account_id, product_id),
|
|
FOREIGN KEY(account_id) REFERENCES accounts(id) ON DELETE CASCADE,
|
|
FOREIGN KEY(product_id) REFERENCES products(id) ON DELETE CASCADE
|
|
)
|
|
""")
|
|
|
|
# Tạo bảng settings với thêm cột type
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS settings (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
key TEXT NOT NULL UNIQUE,
|
|
value TEXT,
|
|
type TEXT DEFAULT 'text', -- NEW: loại giá trị (text, boolean, number)
|
|
created_at INTEGER DEFAULT (strftime('%s','now')),
|
|
updated_at INTEGER DEFAULT (strftime('%s','now'))
|
|
)
|
|
''')
|
|
|
|
# --- Default settings với type ---
|
|
default_settings = [
|
|
("AUTO_LISTING", "true", "boolean"), # Bật/tắt tự động listing
|
|
("MAX_CONCURRENT_LISTING", "2", "number"), # Số lượng listing tối đa chạy song song
|
|
("LISTING_INTERVAL_SECONDS", "5", "number"), # Thời gian nghỉ giữa các listing
|
|
("MAX_CONCURRENT_LISTING", "2", "number"), # Số lượng worker chạy đồng thời
|
|
]
|
|
|
|
for key, value, typ in default_settings:
|
|
cursor.execute(
|
|
"INSERT OR IGNORE INTO settings (key, value, type) VALUES (?, ?, ?)",
|
|
(key, value, typ)
|
|
)
|
|
|
|
|
|
|
|
conn.commit()
|
|
conn.close()
|