Skip to content

Commit

Permalink
Set random cookies tt_webid_v2 on every request
Browse files Browse the repository at this point in the history
  • Loading branch information
captaincolonelfox committed Mar 5, 2022
1 parent bfe6704 commit 732b9d7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 5 additions & 2 deletions bot/api/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ async def wrapper(*args, **kwargs):
@define
class TikTokAPI:
headers: dict = field(converter=dict)
cookies: dict = field(converter=dict)
link: str = field(converter=str)
regexp_key: str = field(converter=str)

Expand All @@ -44,7 +43,7 @@ async def handle_message(self, message: Message) -> AsyncIterator[Optional[bytes
@retries(times=3)
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:
cookies=self._tt_webid_v2, follow_redirects=True) as client:
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):
Expand All @@ -64,3 +63,7 @@ def _user_agent(self) -> dict:
f"({datetime.now().replace(microsecond=0).timestamp()})"
)
}

@property
def _tt_webid_v2(self):
return {'tt_webid_v2': f"{random.randint(10 ** 18, (10 ** 19) - 1)}"}
6 changes: 1 addition & 5 deletions bot/handlers/messages.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import random
from aiogram.types import Message
from bot import dp, bot
from bot.api import TikTokAPI
Expand All @@ -9,10 +8,7 @@
regexp_key=r'"video":{"id":"(.*?)",.*?"downloadAddr":"(.*?)",.*?}',
headers={
"Referer": "https://www.tiktok.com/",
},
cookies={
'tt_webid_v2': f"{random.randint(10 ** 18, (10 ** 19) - 1)}"
},
}
)


Expand Down

0 comments on commit 732b9d7

Please sign in to comment.