Skip to content

Instantly share code, notes, and snippets.

@HungHT1890
Created August 11, 2024 05:15
Show Gist options
  • Save HungHT1890/e1cea3b8b9c88928f9095b7596ffa333 to your computer and use it in GitHub Desktop.
Save HungHT1890/e1cea3b8b9c88928f9095b7596ffa333 to your computer and use it in GitHub Desktop.
Download Video From TikTok No WarterMarked
class TikTokRegex:
id = r'{"id":"(.*?)","desc":"'
desc = r'","desc":"(.*?)"'
create_time = r'"createTime":"(\d+)"'
author_unique_id = r'"uniqueId":"(.*?)"'
author_nickname = r'"nickname":"(.*?)"'
author_uid = r'"author":{"id":"(\d+)'
video_id = r'"Uri":"(.*?)"'
video_url = r'"UrlList":\[\"(.*?)\"\]'
def downloadVideo(video_url,file_name=None):
"""
Download video from tiktok video url
video_url: str => url tiktok video
file_name(file_path): str => name of file output mp4
Returns :
msg:str download status message
desc:str video description
video_url:str url video need download
download_status: bool
Example:
{
'success': download_status,
'file_name': file_name,
'desc': desc,
'msg': msg
}
"""
desc = '' # video description
msg = '' # download status message
if file_name == None:
file_name = video_url.split('/')[-1] + '.mp4' # file name mp4 output
video_url = video_url.strip() # video url
download_status = False # status download
# crate session for all requests in this function
ss = session()
def extract_data(regex,data):
"extract data using regex"
try:
return findall(regex,data)[0]
except:
return ''
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'accept-language': 'en,vi;q=0.9,vi-VN;q=0.8,fr-FR;q=0.7,fr;q=0.6,en-US;q=0.5,nl;q=0.4',
'cache-control': 'max-age=0',
'priority': 'u=0, i',
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
}
try:
response = ss.get(url=video_url,headers=headers)
if response.status_code == 200:
# extract video description
desc = extract_data(regex=TikTokRegex.desc,data=response.text)
# get all url download video
videos_url = findall(TikTokRegex.video_url,response.text)
# list download url
download_urls = [url for url in videos_url for url in url.split('","')]
# check wartermarked and type file
for url_video in download_urls:
if 'video_mp4' in url_video and 'unwatermarked' in url_video:
download_url = url_video.encode('utf-8').decode('unicode_escape')
# video content
video_response = ss.get(url=download_url)
if video_response.status_code == 200:
# save video content
with open(file_name,'wb') as file_mp4:
file_mp4.write(video_response.content)
download_status = True
msg = 'DOWNLOAD SUCCESS T.ME/HUNGHT1890'
break
else:
msg = f'DOWNLOAD FALSE [STATUS CODE {video_response.status_code}]'
if download_status == False:
msg = 'DOWNLOAD FALSE [NO DOWNLOAD LINK FOUND]'
except Exception as e:
msg = f'DOWNLOAD FALSE [ERROR: {e.__class__.__name__}]'
return {
'success': download_status,
'file_name': file_name,
'desc': desc,
'msg': msg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment