Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/tweet/__init__.py
2023-09-26 21:11:01 +08:00

75 lines
2.7 KiB
Python

import re
import ujson as json
from config import CFG
from core.builtins import Bot
from core.builtins.message import Image
from core.component import module
from core.dirty_check import check_bool, rickroll
from core.utils.http import download_to_cache, get_url
web_render_local = CFG.get_url('web_render_local')
t = module('tweet', developers=['Dianliang233'], desc='{tweet.help.desc}', alias=['x'])
@t.handle('<tweet> {{tweet.help}}')
async def _(msg: Bot.MessageSession, tweet: str):
if tweet.isdigit():
tweet_id = tweet
else:
match = re.search(r"status/(\d+)", tweet)
if match:
tweet_id = match.group(1)
else:
await msg.finish(msg.locale.t('tweet.message.error'))
res = await get_url(f'https://react-tweet.vercel.app/api/tweet/{tweet_id}')
res_json = json.loads(res)
if not res_json['data']:
await msg.finish(msg.locale.t('tweet.message.not_found'))
elif res_json['data']['__typename'] != "Tweet":
rickroll(msg)
else:
if await check_bool(res_json['data']['text'], res_json['data']['user']['name'],
res_json['data']['user']['screen_name']):
rickroll(msg)
else:
css = '''
main {
justify-content: start !important;
}
main > div {
margin: 0 !important;
border: 0 !important;
}
article {
padding: .75rem 1rem;
}
footer {
display: none;
}
#__next > div {
height: auto;
padding: 0;
}
a[href^="https://twitter.com/intent/follow"],
a[href^="https://help.twitter.com/en/twitter-for-websites-ads-info-and-privacy"],
div[class^="tweet-replies"],
button[aria-label="Copy link"],
a[aria-label="Reply to this Tweet on Twitter"],
span[class^="tweet-header_separator"] {
display: none;
}
'''
pic = await download_to_cache(web_render_local + 'element_screenshot', method='POST', headers={
'Content-Type': 'application/json',
}, post_data=json.dumps(
{'url': f'https://react-tweet-next.vercel.app/light/{tweet_id}', 'css': css, 'mw': False,
'element': 'article'}), request_private_ip=True)
await msg.finish(
[Image(pic), f"https://twitter.com/{res_json['data']['user']['screen_name']}/status/{tweet_id}"])