import sqlite3

def init_db():
    DATABASE = 'platforms.db'
    with sqlite3.connect(DATABASE) as conn:
        conn.execute('''CREATE TABLE IF NOT EXISTS platforms
                        (id INTEGER PRIMARY KEY AUTOINCREMENT,
                         name TEXT NOT NULL,
                         image TEXT NOT NULL,
                         description TEXT NOT NULL,
                         link TEXT NOT NULL)''')

        # 检查是否已有数据存在
        cursor = conn.execute('SELECT COUNT(*) FROM platforms')
        count = cursor.fetchone()[0]
        if count == 0:
            # 插入默认平台数据
            default_platforms = [
                ('Twitch', 'https://example.com/twitch.jpg', 'Twitch是一个视频直播平台', 'http://14.17.80.242:18080/live/stream.flv'),
                ('YouTube', 'https://example.com/youtube.jpg', 'YouTube是一个视频分享平台', 'http://14.17.80.242:18080/live/stream.flv'),
                ('Facebook Live', 'https://example.com/facebook.jpg', 'Facebook Live是Facebook的直播平台', 'http://14.17.80.242:18080/live/stream.flv'),
                ('Douyu', 'https://example.com/douyu.jpg', '斗鱼是中国的一个视频直播平台', 'http://14.17.80.242:18080/live/stream.flv'),
                ('Huya', 'https://example.com/huya.jpg', '虎牙是中国的一个视频直播平台', 'http://14.17.80.242:18080/live/stream.flv'),
                ('Bilibili', 'https://example.com/bilibili.jpg', 'Bilibili是中国的一个视频分享平台', 'http://14.17.80.242:18080/live/stream.flv')
            ]
            
            conn.executemany('INSERT INTO platforms (name, image, description, link) VALUES (?, ?, ?, ?)', default_platforms)
