main
 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3import os
 4
 5from glom import glom
 6from loguru import logger
 7from pyrogram.client import Client
 8
 9from config import HISTORY
10from database.d1 import query_d1
11from history.d1 import backup_chat_history_to_d1
12from utils import nowdt, strings_list, to_int
13
14
15async def daily_backup_history_to_d1(client: Client) -> None:
16    """由于D1每日写入有次数限制, 每天晚上最后10分钟备份所有对话的数据.
17
18    start_from 设置为 "oldest" 时, 从数据库中最旧的记录往前备份.
19    """
20    chats = os.getenv("BACKUP_CHAT_HISTORY_DAILY_CHATS", "disabled")
21    hour = int(os.getenv("BACKUP_CHAT_HISTORY_DAILY_HOUR", "23"))
22    minute_start = int(os.getenv("BACKUP_CHAT_HISTORY_DAILY_MINUTE_START", "50"))
23    minute_end = int(os.getenv("BACKUP_CHAT_HISTORY_DAILY_MINUTE_END", "59"))
24    if chats == "disabled":
25        return
26
27    def is_sync_time() -> bool:
28        now = nowdt("UTC")
29        return now.hour == hour and minute_start <= now.minute <= minute_end
30
31    if not HISTORY.D1_ENABLE:
32        return
33    if not is_sync_time():
34        return
35    if chats == "full_table":
36        resp = await query_d1("SELECT * FROM 'chatinfo';", db_name=HISTORY.D1_DATABASE, silent=True)
37        tables = glom(resp, "result.0.results", default=[])
38        cids = [x["chandle"] or int(x["cid"]) for x in tables]
39    else:
40        cids = [to_int(x) for x in strings_list(chats)]
41
42    for cid in cids:
43        if not is_sync_time():  # 每个会话都检查一遍时间
44            break
45        logger.info(f"Backup chat history to D1: {cid}")
46        await backup_chat_history_to_d1(client, cid, hours=99999, start_from="oldest", max_sync=1000)  # 每个会话最大同步1000条