From 4df161a27dfeef741aae4f613b6d23849b6fbf7c Mon Sep 17 00:00:00 2001 From: Evil0ctal Date: Sun, 6 Feb 2022 20:11:41 -0800 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=90=B1=E6=B7=BB=E5=8A=A0=E4=BA=86TikTo?= =?UTF-8?q?k=20APP=E5=86=85=E7=9F=AD=E9=93=BE=E6=8E=A5=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TikTok_ZH.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/TikTok_ZH.py b/TikTok_ZH.py index a76a9f3..25ce6fb 100644 --- a/TikTok_ZH.py +++ b/TikTok_ZH.py @@ -97,6 +97,22 @@ def loading(url_lists): time.sleep(0.1) +def get_tiktok_url(tiktok_link): + # 校验TikTok链接 + if tiktok_link[:12] == "https://www.": + return tiktok_link + else: + try: + # 从请求头中获取原始链接 + response = requests.get(url=tiktok_link, headers=headers, allow_redirects=False) + new_link = response.headers['Location'] + response = requests.get(url=new_link, headers=headers, allow_redirects=False) + true_link = response.headers['Location'].split("?")[0] + return true_link + except Exception as e: + error_do(e, get_tiktok_url) + + @retry(stop_max_attempt_number=3) def get_video_info(original_url): # 利用官方接口解析链接信息 @@ -172,6 +188,7 @@ def get_video_info(original_url): @retry(stop_max_attempt_number=3) def get_video_info_tiktok(tiktok_url): # 对TikTok视频进行解析 + tiktok_url = get_tiktok_url(tiktok_url) try: tiktok_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.9", @@ -189,7 +206,7 @@ def get_video_info_tiktok(tiktok_url): video_info = result["ItemModule"][author_id] # print("The author_id is: ", author_id) # print(video_info) - # 格式很乱 要忍一下 + # 从网页中获得的视频JSON 格式很乱 要忍一下 return video_info except Exception as e: # 异常捕获 @@ -200,6 +217,7 @@ def get_video_info_tiktok(tiktok_url): def tiktok_nwm(tiktok_url): # 使用第三方API获取无水印视频链接(不保证稳定) try: + tiktok_url = get_tiktok_url(tiktok_url) s = requests.Session() api_url = "https://ttdownloader.com/req/" source = s.get("https://ttdownloader.com/")