add logger

This commit is contained in:
2024-03-22 22:18:43 +08:00
parent 76e0ffe58c
commit c3faecf640
2 changed files with 8 additions and 1 deletions
+2
View File
@@ -21,6 +21,7 @@ async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
query = update.inline_query.query query = update.inline_query.query
if query == "": if query == "":
return return
common.logger.info(f"Query: {query}")
async with TGTweet(context.bot_data['client'], query) as tweet: async with TGTweet(context.bot_data['client'], 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)
@@ -30,6 +31,7 @@ async def reply_media(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non
await update.effective_chat.send_action(ChatAction.UPLOAD_PHOTO) await update.effective_chat.send_action(ChatAction.UPLOAD_PHOTO)
# url = update.message.text.split(" ")[0] # url = update.message.text.split(" ")[0]
url = update.message.text url = update.message.text
common.logger.info(f"Receiving url: {url}")
async with TGTweet(context.bot_data['client'], url) as tweet: async with TGTweet(context.bot_data['client'], url) as tweet:
media = list(tweet.pm_media_generator) media = list(tweet.pm_media_generator)
message_sent = await update.effective_message.reply_media_group( message_sent = await update.effective_message.reply_media_group(
+6 -1
View File
@@ -8,7 +8,7 @@ from telegram import (
InputMediaVideo InputMediaVideo
) )
from common import x_url_regex, x_media_regex, x_tco_regex from common import x_url_regex, x_media_regex, x_tco_regex, logger
twimg_url = 'https://pbs.twimg.com/' twimg_url = 'https://pbs.twimg.com/'
vx_api_url = 'https://api.vxtwitter.com/status/{0}/status/{1}' vx_api_url = 'https://api.vxtwitter.com/status/{0}/status/{1}'
@@ -19,6 +19,7 @@ message_raw_text = """{url}
async def fetch_json(session: ClientSession, url: str) -> dict: async def fetch_json(session: ClientSession, url: str) -> dict:
logger.info(f"Fetching {url}")
async with session.get(url) as response: async with session.get(url) as response:
assert response.status == 200, f"Failed to fetch {url}, status code {response.status}" assert response.status == 200, f"Failed to fetch {url}, status code {response.status}"
return await response.json() return await response.json()
@@ -30,6 +31,9 @@ class TweetMedia:
self._thumb: str = thumb self._thumb: str = thumb
self._type: str = media_type self._type: str = media_type
def __str__(self):
return f"url: {self.url}, thumb: {self.thumb}, type: {self.type}"
@property @property
def _uri(self) -> str | None: def _uri(self) -> str | None:
match = x_media_regex.match(self._url) match = x_media_regex.match(self._url)
@@ -114,6 +118,7 @@ class TGTweet(Tweet):
async def __aenter__(self): async def __aenter__(self):
self._tweet: dict = await self._fetch_tweet(self._api_param) self._tweet: dict = await self._fetch_tweet(self._api_param)
super().__init__(*self._init_properties) super().__init__(*self._init_properties)
logger.info(f"Media: {self.media}")
return self return self
async def __aexit__(self, exc_type, exc_val, exc_tb): async def __aexit__(self, exc_type, exc_val, exc_tb):