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
|
|
|
|
|
|
|
|
from defs.twitter_api import twitter_api, get_twitter_status, twitter_link, twitter_media, twitter_user_link, \
|
|
|
|
get_twitter_user
|
|
|
|
|
|
|
|
|
|
|
|
@Client.on_message(filters.incoming & filters.text &
|
|
|
|
filters.regex(r"twitter.com/"))
|
|
|
|
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:
|
|
|
|
url = message.text[entity.offset:entity.offset + entity.length]
|
|
|
|
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"]:
|
2022-10-02 16:34:28 +00:00
|
|
|
if url.path.find('status') >= 0:
|
2022-10-03 06:13:37 +00:00
|
|
|
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:
|
|
|
|
future = client.loop.run_in_executor(executor, twitter_api.GetStatus, status_id)
|
|
|
|
url_json = await asyncio.wait_for(future, timeout=30, loop=client.loop)
|
|
|
|
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_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}'
|
|
|
|
if len(media_model) == 0:
|
|
|
|
await client.send_message(
|
|
|
|
message.chat.id, text,
|
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
disable_web_page_preview=True,
|
|
|
|
reply_to_message_id=message.id,
|
|
|
|
reply_markup=twitter_link(url_json.id, quoted_status, url_json.user.screen_name)
|
|
|
|
)
|
|
|
|
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,
|
2022-10-03 06:13:37 +00:00
|
|
|
reply_markup=twitter_link(url_json.id, quoted_status, url_json.user.screen_name)
|
|
|
|
)
|
|
|
|
elif media_model[0] == 'photo':
|
|
|
|
await message.reply_photo(
|
|
|
|
media_list[0],
|
|
|
|
caption=text,
|
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
quote=True,
|
2022-10-02 16:34:28 +00:00
|
|
|
reply_markup=twitter_link(url_json.id, quoted_status, url_json.user.screen_name)
|
|
|
|
)
|
|
|
|
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,
|
2022-10-02 16:34:28 +00:00
|
|
|
reply_markup=twitter_link(url_json.id, quoted_status, url_json.user.screen_name)
|
|
|
|
)
|
|
|
|
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,
|
2022-10-02 16:34:28 +00:00
|
|
|
reply_markup=twitter_link(url_json.id, quoted_status, url_json.user.screen_name)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await client.send_media_group(message.chat.id,
|
2022-10-03 06:13:37 +00:00
|
|
|
media=twitter_media(text, media_model, media_list, static))
|
2022-10-02 16:34:28 +00:00
|
|
|
elif url.path == '/':
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
# 解析用户
|
|
|
|
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:
|
|
|
|
future = client.loop.run_in_executor(executor, twitter_api.GetUser, None, uid)
|
|
|
|
url_json = await asyncio.wait_for(future, timeout=30, loop=client.loop)
|
|
|
|
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(
|
2022-10-02 16:34:28 +00:00
|
|
|
url_json.profile_image_url_https.replace('_normal', ''),
|
|
|
|
caption=text,
|
2022-10-03 06:13:37 +00:00
|
|
|
quote=True,
|
2022-10-02 16:34:28 +00:00
|
|
|
reply_markup=twitter_user_link(user_username, status_link)
|
|
|
|
)
|
2022-10-03 06:13:37 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2022-10-02 16:34:28 +00:00
|
|
|
raise ContinuePropagation
|