main
 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3
 4from loguru import logger
 5from pyrogram.client import Client
 6from pyrogram.types import Chat, Message
 7from pyrogram.types.messages_and_media.message import Str
 8
 9from config import PROXY, cache
10from database.r2 import get_cf_r2, set_cf_r2
11from messages.main import process_message
12from networking import hx_req
13from utils import rand_number
14
15
16async def fafa_twitter_rss(client: Client):
17    """发姐推特RSS."""
18    if cache.get("fafa_twitter_rss"):
19        return
20    cache.set("fafa_twitter_rss", 1, ttl=600)
21    api = "https://api.fxtwitter.com/2/profile/yifaer_chen/statuses"
22    resp = await hx_req(
23        api,
24        proxy=PROXY.TWITTER,
25        timeout=10,
26        check_kv={"code": 200},
27        check_keys=["results"],
28        silent=True,
29    )
30    if error := resp.get("hx_error"):
31        logger.error(error)
32        return
33    posts = sorted(resp["results"], key=lambda x: x["created_timestamp"], reverse=True)[:5]  # latest 5 tweets
34    for tweet in posts[::-1]:  # old to new
35        if not tweet.get("id"):
36            continue
37        post_id = tweet["id"]
38        url = f"https://x.com/yifaer_chen/status/{post_id}"
39        # check if in cache
40        if cache.has(f"fafa-x-{post_id}"):
41            continue
42        # check if in database
43        if await get_cf_r2(key=f"RSS/陈一发儿/x.com/yifaer_chen/status/{post_id}", silent=True):
44            cache.set(f"fafa-x-{post_id}", 1)
45            continue
46        message = Message(id=rand_number(), chat=Chat(id=rand_number()), text=Str(url))
47        await process_message(
48            client,
49            message,
50            target_chat=-1001433673794,
51            reply_msg_id=-1,
52            need_prefix=False,
53            show_progress=False,
54            ytdlp=False,
55            douyin=False,
56            tiktok=False,
57            instagram=False,
58            weibo=False,
59            twitter_comments_provider=False,
60            show_statistics=False,
61            twitter_provider="fxtwitter-vxtwitter",
62        )
63        cache.set(f"fafa-x-{post_id}", 1)
64        await set_cf_r2(key=f"RSS/陈一发儿/x.com/yifaer_chen/status/{post_id}", data={"finished": "1"})