mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-09-24 23:42:18 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91cad86325
|
||
|
|
cfdb05e476
|
||
|
|
6df6a950c8
|
||
|
|
78d185723b
|
@@ -2,6 +2,13 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
try:
|
||||||
|
import uvloop, asyncio
|
||||||
|
|
||||||
|
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
|
||||||
|
except ImportError:
|
||||||
|
uvloop = None
|
||||||
|
|
||||||
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
||||||
ADMIN = [int(i) for i in os.getenv("BOT_ADMIN").split(",")]
|
ADMIN = [int(i) for i in os.getenv("BOT_ADMIN").split(",")]
|
||||||
|
|
||||||
@@ -23,4 +30,7 @@ logging.basicConfig(
|
|||||||
level=os.getenv("LOG_LEVEL", "WARNING"),
|
level=os.getenv("LOG_LEVEL", "WARNING"),
|
||||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||||
)
|
)
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
def get_logger(name: str) -> logging.Logger:
|
||||||
|
return logging.getLogger(name)
|
||||||
|
|||||||
@@ -1,16 +1,21 @@
|
|||||||
import html
|
import html
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from aiohttp import ClientSession
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
|
||||||
from telegram import Chat, InlineKeyboardButton, InlineKeyboardMarkup, Message, Update
|
|
||||||
from telegram.constants import ChatAction, ChatType, ParseMode
|
from telegram.constants import ChatAction, ChatType, ParseMode
|
||||||
from telegram.ext import (Application, ApplicationBuilder, CallbackQueryHandler, CommandHandler, ContextTypes, Defaults,
|
from telegram.ext import (ApplicationBuilder, CallbackQueryHandler, CommandHandler, Defaults,
|
||||||
InlineQueryHandler, MessageHandler, PicklePersistence, filters)
|
InlineQueryHandler, MessageHandler, PicklePersistence, filters)
|
||||||
|
|
||||||
import common
|
import common
|
||||||
from common import logger
|
|
||||||
from tweet import TGTweet
|
from tweet import TGTweet
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from telegram import Chat, Message, Update
|
||||||
|
from telegram.ext import Application, ContextTypes
|
||||||
|
|
||||||
|
logger = common.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def send_action(action):
|
def send_action(action):
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
@@ -197,7 +202,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.set_session(ClientSession())
|
TGTweet.init_client()
|
||||||
|
|
||||||
|
|
||||||
async def post_stop(application: Application) -> None:
|
async def post_stop(application: Application) -> None:
|
||||||
@@ -205,7 +210,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_session()
|
await TGTweet.close_client()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
+2
-1
@@ -1,2 +1,3 @@
|
|||||||
python-telegram-bot[webhooks]~=21.3
|
python-telegram-bot[webhooks]~=21.3
|
||||||
aiohttp[speedups]~=3.9.3
|
httpx[http2]~=0.27.0
|
||||||
|
uvloop~=0.19.0; sys_platform != 'win32'
|
||||||
@@ -1,16 +1,14 @@
|
|||||||
import html
|
import html
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
|
|
||||||
from aiohttp import ClientSession
|
from httpx import AsyncClient
|
||||||
from telegram import (
|
from telegram import (InlineQueryResultMpeg4Gif, InlineQueryResultPhoto, InlineQueryResultVideo, InputMediaPhoto,
|
||||||
InlineQueryResultPhoto,
|
InputMediaVideo)
|
||||||
InlineQueryResultVideo,
|
|
||||||
InlineQueryResultMpeg4Gif,
|
from common import get_logger, x_media_regex, x_tco_regex, x_url_regex
|
||||||
InputMediaPhoto,
|
|
||||||
InputMediaVideo
|
logger = get_logger(__name__)
|
||||||
)
|
|
||||||
|
|
||||||
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/{0}/status/{1}'
|
vx_api_url = 'https://api.vxtwitter.com/{0}/status/{1}'
|
||||||
@@ -20,11 +18,19 @@ message_raw_text = """{url}
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
async def fetch_json(session: ClientSession, url: str) -> dict:
|
def create_client() -> 'AsyncClient':
|
||||||
|
return AsyncClient(http2=True)
|
||||||
|
|
||||||
|
|
||||||
|
async def close_client(_client: 'AsyncClient') -> None:
|
||||||
|
await _client.aclose()
|
||||||
|
|
||||||
|
|
||||||
|
async def fetch_json(_client: 'AsyncClient', url: str) -> dict:
|
||||||
logger.info(f"Fetching {url}")
|
logger.info(f"Fetching {url}")
|
||||||
async with session.get(url) as response:
|
response = await _client.get(url)
|
||||||
assert response.status == 200, f"Failed to fetch {url}, status code {response.status}"
|
assert response.status_code == response.is_success, f"Failed to fetch {url}, status code {response.status_code}"
|
||||||
return await response.json()
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
class TweetMedia:
|
class TweetMedia:
|
||||||
@@ -119,7 +125,7 @@ class Tweet:
|
|||||||
|
|
||||||
|
|
||||||
class TGTweet(Tweet):
|
class TGTweet(Tweet):
|
||||||
_session: ClientSession
|
_httpx_client: AsyncClient
|
||||||
|
|
||||||
def __init__(self, url: str):
|
def __init__(self, url: str):
|
||||||
self._url: str = url
|
self._url: str = url
|
||||||
@@ -136,15 +142,15 @@ class TGTweet(Tweet):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_session(cls, session: ClientSession) -> None:
|
def init_client(cls) -> None:
|
||||||
cls._session = session
|
cls._httpx_client = create_client()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def close_session(cls) -> None:
|
async def close_client(cls) -> None:
|
||||||
await cls._session.close()
|
await close_client(cls._httpx_client)
|
||||||
|
|
||||||
async def _fetch_tweet(self, api_param: tuple[str]) -> dict:
|
async def _fetch_tweet(self, api_param: tuple[str]) -> dict:
|
||||||
return await fetch_json(self._session, vx_api_url.format(*api_param))
|
return await fetch_json(self._httpx_client, vx_api_url.format(*api_param))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _tweet_id(self) -> tuple[str] | None:
|
def _tweet_id(self) -> tuple[str] | None:
|
||||||
|
|||||||
Reference in New Issue
Block a user