main
 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3import asyncio
 4import re
 5from datetime import timedelta
 6
 7from loguru import logger
 8from pyrogram.client import Client
 9from pyrogram.types import Message
10
11from config import TZ, cache
12from custom.config import ACCOUNT_NAME, CHANNEL_LILAOSHI_OFFICIAL, CHANNEL_LILAOSHI_PREVIEWED
13from database.r2 import list_cf_r2
14from messages.chat_history import get_history_info_list_via_telegram
15from messages.parser import parse_msg
16from messages.utils import delete_message
17from networking import match_social_media_link
18from utils import nowdt
19
20
21async def handle_lilaoshi(client: Client, message: Message):
22    if ACCOUNT_NAME != "xiaohao" or message.chat.id != CHANNEL_LILAOSHI_OFFICIAL:
23        return
24    info = parse_msg(message, silent=True)
25    if info["entity_urls"] and (matched := await match_social_media_link(info["entity_urls"][0])) and matched["platform"] in ["twitter", "x", "fxtwitter", "fixupx"]:
26        text = f"{matched['url']}\n#set_target_chat={CHANNEL_LILAOSHI_PREVIEWED} #no_show_statistics #no_twitter_comments_provider #set_twitter_provider=fxtwitter-vxtwitter #set_reply_msg_id=-1 #no_show_progress"
27        msg = await client.send_message("@bennydou_bot", text)
28        await asyncio.sleep(3)
29        await delete_message(msg)
30
31
32async def preview_lilaoshi_history_message(client: Client, hours: int = 25):
33    if cache.get("preview_lilaoshi_history_message"):
34        return
35    cache.set("preview_lilaoshi_history_message", 1, ttl=12 * 3600)  # backup every 12 hours
36    r2 = await list_cf_r2(prefix="x.com/whyyoutouzhele/status/")
37    logger.trace(f"Get {len(r2.get('Contents', []))} 李老师 entries from R2")
38    processed_pids = {entry["Key"].removeprefix("x.com/whyyoutouzhele/status/") for entry in r2.get("Contents", [])}
39
40    begin_time = nowdt(TZ) - timedelta(hours=hours)
41    offical_info_list = await get_history_info_list_via_telegram(client, chat_id="lilaoshibushinilaoshi", begin_time=begin_time, limit=999999)
42    for info in offical_info_list:  # old to new
43        if not info["entity_urls"]:
44            continue
45        matched = re.findall(r"https://(:?twitter|x|fxtwitter|fixupx|vxtwitter)\.com/whyyoutouzhele/status/(\d+)", info["entity_urls"][0])
46        pids = [pid for _, pid in matched]
47        for pid in pids:
48            if pid not in processed_pids:
49                url = f"https://x.com/whyyoutouzhele/status/{pid}"
50                logger.info(f"解析李老师频道链接: {url} | {info['text'][:30]}")
51                text = f"{url}\n#set_target_chat={CHANNEL_LILAOSHI_PREVIEWED} #no_show_statistics #no_twitter_comments_provider #set_twitter_provider=fxtwitter-vxtwitter #set_reply_msg_id=-1 #no_show_progress"
52                msg = await client.send_message("@bennydou_bot", text)
53                await asyncio.sleep(3)
54                await delete_message(msg)