Skip to content

Commit a1cc2b6

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 680339d + c1cd8b8 commit a1cc2b6

2 files changed

Lines changed: 41 additions & 15 deletions

File tree

README.en.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ _Download videos that are prohibited from being downloaded, perform data analysi
5050

5151
## 🔊 V4 version notes
5252

53-
- If you are interested in writing this project, please add WeChat.`Evil0ctal`Note: Github project reconstruction, everyone can communicate and learn from each other in the group, and do not allow advertisements or illegal things to be made purely friends and technical communication.
54-
- This project uses`X-Bogus`Algorithm and`A_Bogus`The algorithm requests the web APIs of TikTok and TikTok.
53+
- If you are interested in writing this project, please add WeChat`Evil0ctal`Note: Github project reconstruction, everyone can communicate and learn from each other in the group, and do not allow advertisements or illegal things to be made purely friends and technical communication.
54+
- This project uses`X-Bogus`Algorithm and`A_Bogus`The algorithm requests TikTok and TikTok's Web API.
5555
- Due to Douyin's risk control, please go to**Get the Douyin website cookies in the browser and replace them in config.yaml.**
5656
- Please read the document below before asking for an issue, and most solutions to the problem will be included in the document.
5757
- This project is completely free, but please follow it when using it:[Apache-2.0 license](https://github.com/Evil0ctal/Douyin_TikTok_Download_API?tab=Apache-2.0-1-ov-file#readme)
@@ -64,11 +64,11 @@ _Download videos that are prohibited from being downloaded, perform data analysi
6464

6565
> 📦 Out of the box
6666
67-
Simplify the usage process and quickly carry out development work using the encapsulated SDK. All API interfaces are designed according to the RESTful architecture and are described and documented using the OpenAPI specification, with example parameters to ensure that calls are easier.
67+
Simplify the usage process and quickly carry out development work using the encapsulated SDK. All API interfaces are designed according to the RESTful architecture and are described and documented using the OpenAPI specification, accompanied by example parameters to ensure that calls are easier.
6868

6969
> 💰 Cost Advantage
7070
71-
There is no preset package limit, no monthly usage threshold, all consumption is billed instantly based on the actual usage, and the user's daily request volume is charged step by step. At the same time, you can check in in the user's background by checking in every day. , and these free amounts will not expire.
71+
There is no preset package limit, no monthly usage threshold, all consumption is billed instantly based on the actual usage, and is billed step by step based on the user's daily request volume. At the same time, you can check in in the user's background through daily check-in, and these free amounts will not expire.
7272

7373
> ⚡️ Quick support
7474
@@ -101,7 +101,7 @@ Some of the source code of TikHub will be open sourced on Github and will sponso
101101

102102
- [Johnserf-Seed/Tiktokdownload](https://github.com/Johnserf-Seed/TikTokDownload)
103103
- [HFrost0/bilix](https://github.com/HFrost0/bilix)
104-
- [Tairraos/TikDown - \[Updated required\]](https://github.com/Tairraos/TikDown/)
104+
- [Tairraos/TikDown - \[Updated to be\]](https://github.com/Tairraos/TikDown/)
105105

106106
## ⚗️Technology Stack
107107

@@ -149,7 +149,7 @@ Some of the source code of TikHub will be open sourced on Github and will sponso
149149
- Batch analysis on the web side (supports Douyin/TikTok hybrid analysis)
150150
- Download videos or albums online.
151151
- Production[pip package](https://pypi.org/project/douyin-tiktok-scraper/)Easy and quick import of your project
152-
- [iOS shortcuts to quickly call API](https://apps.apple.com/cn/app/%E5%BF%AB%E6%8D%B7%E6%8C%87%E4%BB%A4/id915249334)Implement watermark-free video/picture collection in-app download
152+
- [iOS shortcuts to quickly call API](https://apps.apple.com/cn/app/%E5%BF%AB%E6%8D%B7%E6%8C%87%E4%BB%A4/id915249334)Implement watermark-free videos/pictures in-app download
153153
- Complete API documentation ([Demo/Demo](https://api.douyin.wtf/docs))
154154
- Rich API interfaces:
155155
- TikTok web version API
@@ -185,7 +185,7 @@ Some of the source code of TikHub will be open sourced on Github and will sponso
185185
- [x] Get the user's homepage fan data
186186
- [x] Get user's homepage follow data
187187
- [x] Obtain data on the collection of works by users on the homepage
188-
- [x] Get user home page search data
188+
- [x] Get search data for users' homepage
189189
- [x] Get user homepage playlist data
190190
- [x] Get individual video comment data
191191
- [x] Get comments and response data for specified videos
@@ -314,7 +314,7 @@ Online:<https://api.douyin.wtf/docs>
314314
- <https://github.com/Evil0ctal/Douyin_TikTok_Download_API/blob/30e56e5a7f97f87d60b1045befb1f6db147f8590/crawlers/tiktok/web/config.yaml#L6>
315315
- I turned off the online download function of the demonstration site. Someone downloaded a huge video and it crashed directly on my server. You can right-click to save the video on the web parsing result page...
316316
- The cookies on the demo site are my own and are not guaranteed to be valid for a long time. They only serve as a demonstration. If you deploy it yourself, please get the cookies yourself.
317-
- HTTP 403 error occurs if you need to access the video link returned by TikTok Web API. Please use the API in this project`/api/download`The interface downloads TikTok videos. This interface has been manually closed in the demonstration site, and you need to deploy this project yourself.
317+
- HTTP 403 error will occur if you need to access the video link returned by TikTok Web API. Please use the API in this project`/api/download`The interface downloads TikTok videos. This interface has been manually closed in the demonstration site, and you need to deploy this project yourself.
318318
- There is one here**Video tutorial**You can refer to:**_<https://www.bilibili.com/video/BV1vE421j7NR/>_**
319319

320320
## 💻Deployment (Method 1 Linux)

app/api/endpoints/download.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import aiofiles
55
import httpx
66
import yaml
7-
from fastapi import APIRouter, Request, Query # 导入FastAPI组件
7+
from fastapi import APIRouter, Request, Query, HTTPException # 导入FastAPI组件
88
from starlette.responses import FileResponse
99

1010
from app.api.models.APIResponseModel import ErrorResponseModel # 导入响应模型
@@ -18,7 +18,6 @@
1818
with open(config_path, 'r', encoding='utf-8') as file:
1919
config = yaml.safe_load(file)
2020

21-
2221
async def fetch_data(url: str, headers: dict = None):
2322
headers = {
2423
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
@@ -28,6 +27,26 @@ async def fetch_data(url: str, headers: dict = None):
2827
response.raise_for_status() # 确保响应是成功的
2928
return response
3029

30+
# 下载视频专用
31+
async def fetch_data_stream(url: str, request:Request , headers: dict = None, file_path: str = None):
32+
headers = {
33+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
34+
} if headers is None else headers.get('headers')
35+
async with httpx.AsyncClient() as client:
36+
# 启用流式请求
37+
async with client.stream("GET", url, headers=headers) as response:
38+
response.raise_for_status()
39+
40+
# 流式保存文件
41+
async with aiofiles.open(file_path, 'wb') as out_file:
42+
async for chunk in response.aiter_bytes():
43+
if await request.is_disconnected():
44+
print("客户端断开连接,清理未完成的文件")
45+
await out_file.close()
46+
os.remove(file_path)
47+
return False
48+
await out_file.write(chunk)
49+
return True
3150

3251
@router.get("/download", summary="在线下载抖音|TikTok视频/图片/Online download Douyin|TikTok video/image")
3352
async def download_file_hybrid(request: Request,
@@ -104,11 +123,18 @@ async def download_file_hybrid(request: Request,
104123

105124
# 获取视频文件
106125
__headers = await HybridCrawler.TikTokWebCrawler.get_tiktok_headers() if platform == 'tiktok' else await HybridCrawler.DouyinWebCrawler.get_douyin_headers()
107-
response = await fetch_data(url, headers=__headers)
108-
109-
# 保存文件
110-
async with aiofiles.open(file_path, 'wb') as out_file:
111-
await out_file.write(response.content)
126+
# response = await fetch_data(url, headers=__headers)
127+
128+
success = await fetch_data_stream(url, request, headers=__headers, file_path=file_path)
129+
if not success:
130+
raise HTTPException(
131+
status_code=500,
132+
detail="An error occurred while fetching data"
133+
)
134+
135+
# # 保存文件
136+
# async with aiofiles.open(file_path, 'wb') as out_file:
137+
# await out_file.write(response.content)
112138

113139
# 返回文件内容
114140
return FileResponse(path=file_path, filename=file_name, media_type="video/mp4")

0 commit comments

Comments
 (0)