iShotaBot/modules/twitter_api.py

164 lines
4.4 KiB
Python
Raw Normal View History

2023-09-10 05:24:44 +00:00
import contextlib
from typing import Optional
2023-07-16 09:49:21 +00:00
from urllib.parse import urlparse
2023-09-10 05:24:44 +00:00
from pydantic import BaseModel
2023-07-16 09:49:21 +00:00
from pyrogram import Client, filters, ContinuePropagation
2023-09-10 05:36:50 +00:00
from pyrogram.enums import MessageEntityType, ChatType
2023-07-16 09:49:21 +00:00
from pyrogram.types import Message
2023-09-10 04:49:11 +00:00
from defs.fix_twitter_api import (
2023-07-16 09:49:21 +00:00
fetch_tweet,
get_twitter_status,
twitter_link,
twitter_media,
fetch_user,
get_twitter_user,
twitter_user_link,
twitter_medias,
)
2023-09-03 07:55:29 +00:00
from init import bot
2023-09-10 04:58:42 +00:00
from models.apis.fxtwitter.model import FixTweetMedia
2023-07-16 09:49:21 +00:00
2023-09-10 05:24:44 +00:00
class Reply(BaseModel):
cid: int
mid: Optional[int] = None
async def send_single_tweet(reply: Reply, media: FixTweetMedia, text: str, button):
2023-07-16 09:49:21 +00:00
if media.type == "photo":
2023-09-10 05:24:44 +00:00
await bot.send_photo(
reply.cid,
2023-09-10 04:58:42 +00:00
media.url,
2023-07-16 09:49:21 +00:00
caption=text,
reply_markup=button,
2023-09-10 05:24:44 +00:00
reply_to_message_id=reply.mid,
2023-07-16 09:49:21 +00:00
)
elif media.type == "video":
2023-09-10 05:24:44 +00:00
await bot.send_video(
reply.cid,
2023-09-10 04:58:42 +00:00
media.url,
2023-07-16 09:49:21 +00:00
caption=text,
reply_markup=button,
2023-09-10 05:24:44 +00:00
reply_to_message_id=reply.mid,
2023-07-16 09:49:21 +00:00
)
elif media.type == "gif":
2023-09-10 05:24:44 +00:00
await bot.send_animation(
reply.cid,
2023-09-10 04:58:42 +00:00
media.url,
2023-07-16 09:49:21 +00:00
caption=text,
reply_markup=button,
2023-09-10 05:24:44 +00:00
reply_to_message_id=reply.mid,
2023-07-16 09:49:21 +00:00
)
else:
2023-09-10 05:24:44 +00:00
await bot.reply_document(
reply.cid,
2023-09-10 04:58:42 +00:00
media.url,
2023-07-16 09:49:21 +00:00
caption=text,
reply_markup=button,
2023-09-10 05:24:44 +00:00
reply_to_message_id=reply.mid,
2023-07-16 09:49:21 +00:00
)
2023-09-10 05:24:44 +00:00
async def process_status(reply: Reply, status: str):
2023-07-16 09:49:21 +00:00
try:
status = int(status)
except ValueError:
return
tweet = await fetch_tweet(status)
if not tweet:
return
text = get_twitter_status(tweet)
button = twitter_link(tweet)
medias = twitter_medias(tweet)
if len(medias) == 1:
media = medias[0]
2023-09-10 05:24:44 +00:00
await send_single_tweet(reply, media, text, button)
2023-07-16 09:49:21 +00:00
return
media_lists = twitter_media(medias, text)
if media_lists:
2023-09-10 05:24:44 +00:00
await bot.send_media_group(
reply.cid,
media_lists,
reply_to_message_id=reply.mid,
)
2023-07-16 09:49:21 +00:00
else:
2023-09-10 05:24:44 +00:00
await bot.send_message(
reply.cid,
text,
reply_markup=button,
reply_to_message_id=reply.mid,
)
2023-07-16 09:49:21 +00:00
2023-09-10 05:24:44 +00:00
async def process_user(reply: Reply, username: str):
2023-07-16 09:49:21 +00:00
user = await fetch_user(username)
if not user:
return
text = get_twitter_user(user)
button = twitter_user_link(user)
2023-09-10 05:24:44 +00:00
await bot.send_photo(
reply.cid,
2023-07-16 09:49:21 +00:00
user.icon,
caption=text,
reply_markup=button,
2023-09-10 05:24:44 +00:00
reply_to_message_id=reply.mid,
2023-07-16 09:49:21 +00:00
)
2023-09-10 05:24:44 +00:00
async def process_url(url: str, reply: Reply):
2023-09-10 04:49:11 +00:00
url = urlparse(url)
2023-09-10 04:58:42 +00:00
if url.hostname and url.hostname in [
"twitter.com",
"vxtwitter.com",
"fxtwitter.com",
"x.com",
]:
2023-09-10 04:49:11 +00:00
if url.path.find("status") >= 0:
status_id = str(
url.path[url.path.find("status") + 7 :].split("/")[0]
).split("?")[0]
try:
2023-09-10 05:24:44 +00:00
await process_status(reply, status_id)
2023-09-10 04:49:11 +00:00
except Exception as e:
print(e)
elif url.path == "/":
return
else:
# 解析用户
uid = url.path.replace("/", "")
try:
2023-09-10 05:24:44 +00:00
await process_user(reply, uid)
2023-09-10 04:49:11 +00:00
except Exception as e:
print(e)
2023-09-17 14:37:16 +00:00
@bot.on_message(filters.incoming & filters.text & filters.regex(r"(x|twitter).com/"))
2023-07-16 09:49:21 +00:00
async def twitter_share(_: Client, message: Message):
if not message.text:
return
2023-09-10 05:02:37 +00:00
if (
message.sender_chat
and message.forward_from_chat
and message.sender_chat.id == message.forward_from_chat.id
):
# 过滤绑定频道的转发
return
2023-09-10 05:24:44 +00:00
mid = message.id
2023-09-10 05:36:50 +00:00
if message.text.startswith("del") and message.chat.type == ChatType.CHANNEL:
2023-09-10 05:24:44 +00:00
with contextlib.suppress(Exception):
await message.delete()
mid = None
reply = Reply(cid=message.chat.id, mid=mid)
2023-07-16 09:49:21 +00:00
for num in range(len(message.entities)):
entity = message.entities[num]
if entity.type == MessageEntityType.URL:
url = message.text[entity.offset : entity.offset + entity.length]
elif entity.type == MessageEntityType.TEXT_LINK:
url = entity.url
else:
continue
2023-09-10 05:24:44 +00:00
await process_url(url, reply)
2023-07-16 09:49:21 +00:00
raise ContinuePropagation