iShotaBot/modules/twitter_api.py

161 lines
6.9 KiB
Python
Raw Normal View History

2022-10-03 06:13:37 +00:00
import asyncio
2022-10-02 16:34:28 +00:00
from urllib.parse import urlparse
2022-10-03 06:13:37 +00:00
from concurrent.futures import ThreadPoolExecutor
2022-10-02 16:34:28 +00:00
from pyrogram import Client, filters, ContinuePropagation
from pyrogram.enums import MessageEntityType, ParseMode
from pyrogram.types import Message
2023-01-12 13:19:54 +00:00
from defs.twitter_api import (
twitter_api,
get_twitter_status,
twitter_link,
twitter_media,
twitter_user_link,
get_twitter_user,
)
2022-10-02 16:34:28 +00:00
2023-01-12 13:19:54 +00:00
@Client.on_message(filters.incoming & filters.text & filters.regex(r"twitter.com/"))
2022-10-02 16:34:28 +00:00
async def twitter_share(client: Client, message: Message):
if not message.text:
return
2022-10-03 06:13:37 +00:00
static = "static" in message.text
try:
for num in range(len(message.entities)):
2022-10-02 16:34:28 +00:00
entity = message.entities[num]
if entity.type == MessageEntityType.URL:
2023-01-12 13:19:54 +00:00
url = message.text[entity.offset : entity.offset + entity.length]
2022-10-02 16:34:28 +00:00
elif entity.type == MessageEntityType.TEXT_LINK:
url = entity.url
else:
continue
url = urlparse(url)
2022-10-03 06:13:37 +00:00
if url.hostname and url.hostname in ["twitter.com", "vxtwitter.com"]:
2023-01-12 13:19:54 +00:00
if url.path.find("status") >= 0:
status_id = str(
url.path[url.path.find("status") + 7 :].split("/")[0]
).split("?")[0]
2022-10-13 14:36:43 +00:00
url_json = None
2022-10-03 06:13:37 +00:00
with ThreadPoolExecutor() as executor:
2022-10-13 14:36:43 +00:00
for _ in range(3):
try:
2023-01-12 13:19:54 +00:00
future = client.loop.run_in_executor(
executor, twitter_api.GetStatus, status_id
)
url_json = await asyncio.wait_for(
future, timeout=30, loop=client.loop
)
2022-10-13 14:36:43 +00:00
except Exception as e:
print(e)
return
if url_json:
break
if not url_json:
return
2023-01-12 13:19:54 +00:00
(
text,
user_text,
media_model,
media_list,
quoted_status,
) = get_twitter_status(url_json)
text = f"<b>Twitter Status Info</b>\n\n{text}\n\n{user_text}"
2022-10-02 16:34:28 +00:00
if len(media_model) == 0:
await client.send_message(
2023-01-12 13:19:54 +00:00
message.chat.id,
text,
2022-10-02 16:34:28 +00:00
parse_mode=ParseMode.HTML,
disable_web_page_preview=True,
reply_to_message_id=message.id,
2023-01-12 13:19:54 +00:00
reply_markup=twitter_link(
url_json.id, quoted_status, url_json.user.screen_name
),
2022-10-02 16:34:28 +00:00
)
elif len(media_model) == 1:
2022-10-03 06:13:37 +00:00
if static:
await message.reply_document(
media_list[0],
2022-10-02 16:34:28 +00:00
caption=text,
2022-10-03 06:13:37 +00:00
quote=True,
2022-10-02 16:34:28 +00:00
parse_mode=ParseMode.HTML,
2023-01-12 13:19:54 +00:00
reply_markup=twitter_link(
url_json.id,
quoted_status,
url_json.user.screen_name,
),
2022-10-03 06:13:37 +00:00
)
2023-01-12 13:19:54 +00:00
elif media_model[0] == "photo":
2022-10-03 06:13:37 +00:00
await message.reply_photo(
media_list[0],
caption=text,
parse_mode=ParseMode.HTML,
quote=True,
2023-01-12 13:19:54 +00:00
reply_markup=twitter_link(
url_json.id,
quoted_status,
url_json.user.screen_name,
),
2022-10-02 16:34:28 +00:00
)
2023-01-12 13:19:54 +00:00
elif media_model[0] == "gif":
2022-10-03 06:13:37 +00:00
await message.reply_animation(
media_list[0],
2022-10-02 16:34:28 +00:00
caption=text,
parse_mode=ParseMode.HTML,
2022-10-03 06:13:37 +00:00
quote=True,
2023-01-12 13:19:54 +00:00
reply_markup=twitter_link(
url_json.id,
quoted_status,
url_json.user.screen_name,
),
2022-10-02 16:34:28 +00:00
)
else:
2022-10-03 06:13:37 +00:00
await message.reply_video(
media_list[0],
2022-10-02 16:34:28 +00:00
caption=text,
parse_mode=ParseMode.HTML,
2022-10-03 06:13:37 +00:00
quote=True,
2023-01-12 13:19:54 +00:00
reply_markup=twitter_link(
url_json.id,
quoted_status,
url_json.user.screen_name,
),
2022-10-02 16:34:28 +00:00
)
else:
2023-01-12 13:19:54 +00:00
await client.send_media_group(
message.chat.id,
media=twitter_media(text, media_model, media_list, static),
)
elif url.path == "/":
2022-10-02 16:34:28 +00:00
return
else:
# 解析用户
2023-01-12 13:19:54 +00:00
uid = url.path.replace("/", "")
2022-10-13 14:36:43 +00:00
url_json = None
2022-10-03 06:13:37 +00:00
with ThreadPoolExecutor() as executor:
2022-10-13 14:36:43 +00:00
for _ in range(3):
try:
2023-01-12 13:19:54 +00:00
future = client.loop.run_in_executor(
executor, twitter_api.GetUser, None, uid
)
url_json = await asyncio.wait_for(
future, timeout=30, loop=client.loop
)
2022-10-13 14:36:43 +00:00
except Exception as e:
print(e)
return
if url_json:
break
if not url_json:
return
2022-10-02 16:34:28 +00:00
text, user_username, status_link = get_twitter_user(url_json)
2022-10-03 06:13:37 +00:00
await message.reply_photo(
2023-01-12 13:19:54 +00:00
url_json.profile_image_url_https.replace("_normal", ""),
2022-10-02 16:34:28 +00:00
caption=text,
2022-10-03 06:13:37 +00:00
quote=True,
2023-01-12 13:19:54 +00:00
reply_markup=twitter_user_link(user_username, status_link),
2022-10-02 16:34:28 +00:00
)
2022-10-03 06:13:37 +00:00
except Exception as e:
print(e)
2022-10-02 16:34:28 +00:00
raise ContinuePropagation