Compare commits

..
8 Commits
Author SHA1 Message Date
YoursFunny a6325d5bc8 fix check instance 2024-06-20 15:46:47 +08:00
YoursFunny 6c0e581135 fix regex match group 2024-06-20 15:40:18 +08:00
YoursFunny 494fc50446 fix missing aexit 2024-06-20 15:29:30 +08:00
YoursFunny a4e568667c fix type import error 2024-06-20 15:25:17 +08:00
YoursFunny 8231eeb20e refactor tweet, better type hint 2024-06-20 15:13:09 +08:00
YoursFunny 74f42a2df3 fix status code check 2024-06-20 02:51:20 +08:00
YoursFunny ae1f5a8c8c rename 2024-06-20 02:48:06 +08:00
YoursFunny 38adbf0cb8 use cached property 2024-06-20 02:40:35 +08:00
5 changed files with 81 additions and 96 deletions
+1 -1
View File
@@ -3,4 +3,4 @@ __pycache__/
cert/ cert/
data/ data/
docker-compose.yml docker-compose.yml
x.py utils/x.py
+8 -7
View File
@@ -10,7 +10,7 @@ from telegram.ext import (ApplicationBuilder, CallbackQueryHandler, CommandHandl
InlineQueryHandler, MessageHandler, PicklePersistence, filters) InlineQueryHandler, MessageHandler, PicklePersistence, filters)
import common import common
from tweet import TelegramTweet from utils.tweet import Telegram
if TYPE_CHECKING: if TYPE_CHECKING:
from telegram import Chat, Message, Update from telegram import Chat, Message, Update
@@ -36,7 +36,7 @@ async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
if query == "": if query == "":
return return
logger.info(f"Query: {query}") logger.info(f"Query: {query}")
async with TGTweet(query) as tweet: async with Telegram(query) as tweet:
result = list(tweet.inline_query_generator) result = list(tweet.inline_query_generator)
await update.inline_query.answer(result) await update.inline_query.answer(result)
@@ -45,13 +45,13 @@ async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
async def url_media(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def url_media(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
url = update.message.text url = update.message.text
logger.info(f"Receiving url: {url}") logger.info(f"Receiving url: {url}")
async with TGTweet(url) as tweet: async with Telegram(url) as tweet:
media = list(tweet.pm_media_generator) media = list(tweet.message_media_generator)
message_to_send = await update.effective_message.reply_media_group( message_to_send = await update.effective_message.reply_media_group(
media, media,
caption=tweet.message_text, caption=tweet.message_text,
reply_to_message_id=update.message.message_id, reply_to_message_id=update.message.message_id,
) if not tweet.is_single_gif else await update.effective_message.reply_animation( ) if not isinstance(media[0], tuple) else await update.effective_message.reply_animation(
media[0][0], media[0][0],
caption=tweet.message_text, caption=tweet.message_text,
reply_to_message_id=update.message.message_id, reply_to_message_id=update.message.message_id,
@@ -204,7 +204,7 @@ async def post_init(application: Application) -> None:
DESCRIPTION = "A bot to fetch tweets from Twitter." DESCRIPTION = "A bot to fetch tweets from Twitter."
await application.bot.set_my_description(DESCRIPTION) await application.bot.set_my_description(DESCRIPTION)
await application.bot.set_my_short_description(DESCRIPTION) await application.bot.set_my_short_description(DESCRIPTION)
TGTweet.init_client() Telegram.init_client()
async def post_stop(application: Application) -> None: async def post_stop(application: Application) -> None:
@@ -212,7 +212,7 @@ async def post_stop(application: Application) -> None:
async def post_shutdown(application: Application) -> None: async def post_shutdown(application: Application) -> None:
await TGTweet.close_client() await Telegram.close_client()
def main(): def main():
@@ -226,6 +226,7 @@ def main():
.post_stop(post_stop) .post_stop(post_stop)
.post_shutdown(post_shutdown) .post_shutdown(post_shutdown)
.concurrent_updates(True) .concurrent_updates(True)
.http_version('2')
.build() .build()
) )
View File
+55 -88
View File
@@ -2,7 +2,7 @@ from __future__ import annotations
import html import html
from functools import cached_property from functools import cached_property
from typing import TYPE_CHECKING from typing import Generator, TYPE_CHECKING
from uuid import uuid4 from uuid import uuid4
from httpx import AsyncClient from httpx import AsyncClient
@@ -12,7 +12,7 @@ from telegram import (InlineQueryResultMpeg4Gif, InlineQueryResultPhoto, InlineQ
from common import get_logger, x_media_regex, x_tco_regex, x_url_regex from common import get_logger, x_media_regex, x_tco_regex, x_url_regex
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Generator, TypedDict from .types import TweetInfo, TypeInlineQueryResult, TypeMessageMediaResult
logger = get_logger(__name__) logger = get_logger(__name__)
@@ -36,7 +36,7 @@ async def close_client(_client: AsyncClient) -> None:
async def fetch_json(_client: AsyncClient, url: str) -> dict: async def fetch_json(_client: AsyncClient, url: str) -> dict:
logger.info(f"Fetching {url}") logger.info(f"Fetching {url}")
response = await _client.get(url) response = await _client.get(url)
assert response.status_code == response.is_success, f"Failed to fetch {url}, status code {response.status_code}" assert response.is_success, f"Failed to fetch {url}, status code {response.status_code}"
return response.json() return response.json()
@@ -49,12 +49,11 @@ class TweetMedia:
self._type: str = media_type self._type: str = media_type
def __str__(self): def __str__(self):
return f"Media[url: {self.url} thumb: {self.thumb} type: {self.type}]" return f"TweetMedia(url={self.url}, thumb={self.thumb}, type={self.type})"
@cached_property @cached_property
def _uri(self) -> str | None: def _uri(self) -> str | None:
match = x_media_regex.match(self._url) if match := x_media_regex.match(self._url):
if match:
return match.group(2).removesuffix('.jpg').removesuffix('.png') return match.group(2).removesuffix('.jpg').removesuffix('.png')
return None return None
@@ -135,16 +134,6 @@ class Tweet:
return self._sensitive return self._sensitive
class TGTweet(Tweet):
class TweetInfo(TypedDict):
tweetID: str
user_name: str
user_screen_name: str
text: str
media_extended: list[dict]
possibly_sensitive: bool
class ProcessTweet: class ProcessTweet:
__slots__ = ('_httpx_client', '_url', '_tweet') __slots__ = ('_httpx_client', '_url', '_tweet')
@@ -169,7 +158,7 @@ class ProcessTweet:
async def _fetch_tweet(self) -> TweetInfo: async def _fetch_tweet(self) -> TweetInfo:
match = x_url_regex.match(self._url) match = x_url_regex.match(self._url)
assert match, f"Invalid URL: {self._url}" assert match, f"Invalid URL: {self._url}"
auther_id, tweet_id = match.group() auther_id, tweet_id = match.groups()
return await fetch_json(self._httpx_client, vx_api_url.format(auther_id, tweet_id)) return await fetch_json(self._httpx_client, vx_api_url.format(auther_id, tweet_id))
@property @property
@@ -190,83 +179,38 @@ class ProcessTweet:
class TelegramTweet: class TelegramTweet:
__slots__ = ('_httpx_client', '_url', '_tweet', '__dict__')
def __init__(self, url: str): def __init__(self, httpx_client: AsyncClient, url: str):
self._httpx_client: AsyncClient = httpx_client
self._url: str = url self._url: str = url
self._api_param: tuple[str] = self._tweet_id
self._is_single_gif: bool = False
assert self._api_param
async def __aenter__(self): async def __aenter__(self):
self._tweet: dict = await self._fetch_tweet(self._api_param) async with ProcessTweet(self._httpx_client, self._url) as tweet:
super().__init__(*self._init_properties) self._tweet = tweet
return self return self
async def __aexit__(self, exc_type, exc_val, exc_tb): async def __aexit__(self, exc_type, exc_val, exc_tb):
pass pass
@classmethod @cached_property
def init_client(cls) -> None: def url(self) -> str:
cls._httpx_client = create_client() return self._tweet.url
@classmethod @cached_property
async def close_client(cls) -> None:
await close_client(cls._httpx_client)
async def _fetch_tweet(self, api_param: tuple[str]) -> dict:
return await fetch_json(self._httpx_client, vx_api_url.format(*api_param))
@property
def _tweet_id(self) -> tuple[str] | None:
match = x_url_regex.match(self._url)
if match:
return match.groups()
return None
@property
def _tweet_text(self) -> str:
match = x_tco_regex.search(self._tweet['text'])
return self._tweet['text'][:match.start()].strip(" ") if match else self._tweet['text']
@property
def _tweet_media(self) -> list[TweetMedia]:
return [
TweetMedia(
url=x['url'],
thumb=x['thumbnail_url'],
media_type=x['type']
)
for x in self._tweet['media_extended']
]
@property
def _init_properties(self) -> tuple:
id = self._tweet['tweetID']
author = self._tweet['user_name']
author_id = self._tweet['user_screen_name']
text = self._tweet_text
media = self._tweet_media
sensitive = self._tweet['possibly_sensitive']
return id, author, author_id, text, media, sensitive
@property
def is_single_gif(self) -> bool:
return self._is_single_gif
@property
def message_text(self) -> str: def message_text(self) -> str:
tweet = self._tweet
return message_raw_text.format( return message_raw_text.format(
url=self.url, url=tweet.url,
author_url=self.author_url, author_url=tweet.author_url,
author=html.escape(self.author), author=html.escape(tweet.author),
text=html.escape(self.text) text=html.escape(tweet.text)
) )
@property @property
def inline_query_generator(self) -> Generator[ def inline_query_generator(self) -> Generator[TypeInlineQueryResult, None, None]:
InlineQueryResultPhoto | InlineQueryResultVideo | InlineQueryResultMpeg4Gif, None, None tweet = self._tweet
]: for tweet_media in tweet.media:
for tweet_media in self.media:
logger.info(str(tweet_media)) logger.info(str(tweet_media))
if tweet_media.type == "image": if tweet_media.type == "image":
yield InlineQueryResultPhoto( yield InlineQueryResultPhoto(
@@ -281,7 +225,7 @@ class TelegramTweet:
video_url=tweet_media.url, video_url=tweet_media.url,
mime_type="video/mp4", mime_type="video/mp4",
thumbnail_url=tweet_media.thumb, thumbnail_url=tweet_media.thumb,
title=self.text, title=tweet.text,
caption=self.message_text caption=self.message_text
) )
elif tweet_media.type == "gif": elif tweet_media.type == "gif":
@@ -293,26 +237,49 @@ class TelegramTweet:
) )
@property @property
def pm_media_generator(self) -> Generator[InputMediaPhoto | InputMediaVideo | tuple[str, bool], None, None]: def message_media_generator(self) -> Generator[TypeMessageMediaResult, None, None]:
for tweet_media in self.media: tweet = self._tweet
for tweet_media in tweet.media:
logger.info(str(tweet_media)) logger.info(str(tweet_media))
if tweet_media.type == "image": if tweet_media.type == "image":
yield InputMediaPhoto( yield InputMediaPhoto(
media=tweet_media.url, media=tweet_media.url,
has_spoiler=self.sensitive has_spoiler=tweet.sensitive
) )
elif tweet_media.type == "video": elif tweet_media.type == "video":
yield InputMediaVideo( yield InputMediaVideo(
media=tweet_media.url, media=tweet_media.url,
has_spoiler=self.sensitive, has_spoiler=tweet.sensitive,
thumbnail=tweet_media.thumb thumbnail=tweet_media.thumb
) )
elif tweet_media.type == "gif": elif tweet_media.type == "gif":
if len(self.media) == 1: if len(tweet.media) == 1:
self._is_single_gif = True yield tweet_media.url, tweet.sensitive
yield tweet_media.url, self.sensitive
yield InputMediaVideo( yield InputMediaVideo(
media=tweet_media.url, media=tweet_media.url,
has_spoiler=self.sensitive, has_spoiler=tweet.sensitive,
thumbnail=tweet_media.thumb thumbnail=tweet_media.thumb
) )
class Telegram:
_httpx_client: AsyncClient
def __init__(self, url: str):
self._url = url
async def __aenter__(self):
if x_url_regex.match(self._url):
async with TelegramTweet(self._httpx_client, self._url) as tweet:
return tweet
async def __aexit__(self, exc_type, exc_val, exc_tb):
pass
@classmethod
def init_client(cls) -> None:
cls._httpx_client = create_client()
@classmethod
async def close_client(cls) -> None:
await close_client(cls._httpx_client)
+17
View File
@@ -0,0 +1,17 @@
from typing import TypedDict
from telegram import InlineQueryResultMpeg4Gif, InlineQueryResultPhoto, InlineQueryResultVideo, InputMediaPhoto, \
InputMediaVideo
TypeInlineQueryResult = InlineQueryResultMpeg4Gif | InlineQueryResultPhoto | InlineQueryResultVideo
InputMediaAnimation = tuple[str, bool]
TypeMessageMediaResult = InputMediaPhoto | InputMediaVideo | InputMediaAnimation
class TweetInfo(TypedDict):
tweetID: str
user_name: str
user_screen_name: str
text: str
media_extended: list[dict]
possibly_sensitive: bool