-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
35 lines (25 loc) · 1.23 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import requests
import re
LINKS = re.compile("playAddr\":\"(.*?)\"") # changes frequently, may not work soon
USER_AGENT = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
COOKIES = {}
def main(url: str) -> bytes:
"""
:param url: tiktok video url (https://www.tiktok.com/@author/video/1111111111111111111)
:return: video bytes
"""
global COOKIES
vid_page = requests.get(url, headers=USER_AGENT, cookies=COOKIES)
if not COOKIES: # yeap, tiktok need page reload after setting cookies in first time. even in normal browser
COOKIES = vid_page.cookies
vid_page = requests.get(url, headers=USER_AGENT, cookies=COOKIES)
links = LINKS.findall(vid_page.text)
if not links:
raise Exception("can't find links. maybe tiktok change page structure. post issue pls")
download_link = links[0].encode().decode('unicode-escape') # unescape unicode
vid_file = requests.get(download_link, headers={'Referer': url}, cookies=COOKIES)
return vid_file.content
if __name__ == '__main__':
vid = main("https://www.tiktok.com/@sinyaba/video/6867949158263721221")
with open('dude.mp4', 'wb') as f:
f.write(vid)