diff --git a/bot/api/tiktok.py b/bot/api/tiktok.py index 5c57f95..45fbe5d 100644 --- a/bot/api/tiktok.py +++ b/bot/api/tiktok.py @@ -1,10 +1,14 @@ import asyncio import logging +import random import re +import string +from datetime import datetime from typing import AsyncIterator, Optional import httpx from aiogram.types import Message from attr import define, field +from settings import USER_AGENT def retries(times: int): @@ -41,11 +45,22 @@ async def handle_message(self, message: Message) -> AsyncIterator[Optional[bytes async def download_video(self, url: str) -> Optional[bytes]: async with httpx.AsyncClient(headers=self.headers, timeout=30, cookies=self.cookies, follow_redirects=True) as client: - page = await client.get(url) + page = await client.get(url, headers=self._user_agent) tid = page.url.path.rsplit('/', 1)[-1] for vid, link in re.findall(self.regexp_key, page.text): if vid != tid: raise Exception("Retrying") link = link.encode('utf-8').decode('unicode_escape') - if video := await client.get(link): + if video := await client.get(link, headers=self._user_agent): video.raise_for_status() return video.content + + @property + def _user_agent(self) -> dict: + return { + 'User-Agent': USER_AGENT or ( + f"{''.join(random.choices(string.ascii_lowercase, k=random.randint(4,10)))}-" + f"{''.join(random.choices(string.ascii_lowercase, k=random.randint(3,7)))}/" + f"{random.randint(10, 300)} " + f"({datetime.now().replace(microsecond=0).timestamp()})" + ) + } diff --git a/bot/handlers/messages.py b/bot/handlers/messages.py index 4bbfa6d..f9057d7 100644 --- a/bot/handlers/messages.py +++ b/bot/handlers/messages.py @@ -1,9 +1,7 @@ import random -from datetime import datetime from aiogram.types import Message from bot import dp, bot from bot.api import TikTokAPI -from settings import USER_AGENT TikTok = TikTokAPI( @@ -11,8 +9,10 @@ regexp_key=r'"video":{"id":"(.*?)",.*?"downloadAddr":"(.*?)",.*?}', headers={ "Referer": "https://www.tiktok.com/", - "User-Agent": f"{USER_AGENT} ({datetime.now().timestamp()})", - }, cookies={'tt_webid_v2': f"{random.randint(10 ** 18, (10 ** 19) - 1)}"}, + }, + cookies={ + 'tt_webid_v2': f"{random.randint(10 ** 18, (10 ** 19) - 1)}" + }, )