Skip to content

Commit

Permalink
Set random user-agent for every request
Browse files Browse the repository at this point in the history
  • Loading branch information
captaincolonelfox committed Jan 20, 2022
1 parent a1a0518 commit 4d7e53e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 17 additions & 2 deletions bot/api/tiktok.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()})"
)
}
8 changes: 4 additions & 4 deletions bot/handlers/messages.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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(
link='tiktok.com',
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)}"
},
)


Expand Down

0 comments on commit 4d7e53e

Please sign in to comment.