This repository was archived by the owner on Jan 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFDataExport.py
More file actions
60 lines (46 loc) · 1.97 KB
/
Copy pathFDataExport.py
File metadata and controls
60 lines (46 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sqlite3
import pygsheets
from FDataBase import FDataBase
async def FDataExport():
# === Настройки ===
DATABASE_PATH = "flsite.db"
CREDENTIALS_PATH = "ruin-keepers.json"
SHEET_NAME = "database"
# === Подключение к БД и Google Sheets ===
conn = sqlite3.connect(DATABASE_PATH)
conn.row_factory = sqlite3.Row # Для работы с dict-подобным выводом
db = FDataBase(conn)
gc = pygsheets.authorize(service_file=CREDENTIALS_PATH)
# === Создание или открытие Google Sheet ===
try:
sh = gc.open(SHEET_NAME)
except pygsheets.SpreadsheetNotFound:
sh = gc.create(SHEET_NAME)
# === Функция записи таблицы ===
def export_table(table_name: str, data: list[sqlite3.Row], worksheet_index: int):
# Удаляем лист, если существует
try:
sh.del_worksheet(sh.worksheet('index', worksheet_index))
except Exception:
pass
# Создаём новый лист
ws = sh.add_worksheet(table_name, rows=1000, cols=20, index=worksheet_index)
if not data:
ws.update_value('A1', f"No data in table '{table_name}'")
return
# Подготавливаем заголовки и строки
headers = list(data[0].keys())
rows = [headers] + [list(row) for row in data]
# Обновляем таблицу
ws.update_values('A1', rows)
# === Получаем и экспортируем все таблицы ===
tables = [
("events", db.getEvents()),
("users", conn.cursor().execute("SELECT * FROM users").fetchall()),
("admins", conn.cursor().execute("SELECT * FROM admins").fetchall())
]
for i, (table_name, data) in enumerate(tables):
export_table(table_name, data, i)
print("✅ Экспорт завершён!")
if __name__ == "__main__":
FDataExport()