Skip to content

Instantly share code, notes, and snippets.

@Fitzy1293
Created December 29, 2022 14:30
Show Gist options
  • Save Fitzy1293/1f0181238fee771cc9f130636d52e4cd to your computer and use it in GitHub Desktop.
Save Fitzy1293/1f0181238fee771cc9f130636d52e4cd to your computer and use it in GitHub Desktop.
#!/bin/env python3
from argparse import ArgumentParser
import os
from urllib.parse import parse_qsl, urlparse
import requests
from random import randint
HEADERS = {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'DNT': '1',
'User-Agent': 'Mozilla/999.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36',
'Accept': '*/*',
'Sec-Fetch-Site': 'same-site',
'Sec-Fetch-Mode': 'no-cors',
'Sec-Fetch-Dest': 'video',
'Referer': 'https://www.tiktok.com/',
'Accept-Language': 'en-US,en;q=0.9,bs;q=0.8,sr;q=0.7,hr;q=0.6',
'sec-gpc': '1',
'Range': 'bytes=0-',
}
def tiktok_html_text(url, cookies):
response = requests.get(url, cookies=cookies, headers=HEADERS)
return response.text
def get_video_url(webpage_minimized_html):
# simply returns the location of the video file on tiktoks cdn
return webpage_minimized_html.split('"playAddr":"')[1].split('"')[0].replace(r'\u0026', '&').replace(r'\u002F', '/')
def download(cdn_url, cookies, out_path):
'''
downloads the video file from the tiktok cdn
'''
url = urlparse(cdn_url)
params = tuple(parse_qsl(url.query))
request = requests.Request(method='GET', url=f'{url.scheme}://{url.netloc}{url.path}', cookies=cookies, headers=HEADERS, params=params)
prepared_request = request.prepare()
session = requests.Session()
response = session.send(request=prepared_request)
response.raise_for_status()
with open(os.path.abspath(out_path), 'wb') as output_file:
output_file.write(response.content)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('url', nargs='*', help='Video url (https://www.tiktok.com/@username/video/1234567890123456789).')
args = parser.parse_args()
web_id = str(randint(10**10, 10**11 - 1))
cookies = {'tt_webid': web_id,'tt_webid_v2': web_id}
for url in args.url:
webpage_minimized_html = tiktok_html_text(url, cookies)
cdn_url = get_video_url(webpage_minimized_html)
user, vidID = url.replace('/video/', '/').split('?')[0].split('@')[1].split('/')
out_path = f'{vidID} {user}.mp4'
print(f'downloading: "{url}"')
print(f'output: "{out_path}"')
print()
print(f'cdn-url: "{cdn_url}"')
print('---')
download(cdn_url, cookies, out_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment