From 9d3d99a595b8cfb82c97c705b74ac727cd080431 Mon Sep 17 00:00:00 2001 From: Dreyfi Date: Sat, 28 Sep 2024 16:25:53 +1000 Subject: [PATCH 1/2] Fix the response 403 from pexels search_videos_pexels - failed to download videos, maybe the network is not available. if you are in China, please use a VPN. --- app/services/material.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/services/material.py b/app/services/material.py index 77bf6b90..b25d0b4d 100644 --- a/app/services/material.py +++ b/app/services/material.py @@ -40,7 +40,10 @@ def search_videos_pexels( video_orientation = aspect.name video_width, video_height = aspect.to_resolution() api_key = get_api_key("pexels_api_keys") - headers = {"Authorization": api_key} + headers = { + "Authorization": api_key, + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" + } # Build URL params = {"query": search_term, "per_page": 20, "orientation": video_orientation} query_url = f"https://api.pexels.com/videos/search?{urlencode(params)}" From 4ec19fd56a35e46477f4937ffba3001018f1f29c Mon Sep 17 00:00:00 2001 From: Dreyfi Date: Mon, 30 Sep 2024 15:48:54 +1000 Subject: [PATCH 2/2] Add headers with user_agent to save_video request --- app/services/material.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/services/material.py b/app/services/material.py index b25d0b4d..1ae6abf9 100644 --- a/app/services/material.py +++ b/app/services/material.py @@ -161,11 +161,15 @@ def save_video(video_url: str, save_dir: str = "") -> str: logger.info(f"video already exists: {video_path}") return video_path + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" + } + # if video does not exist, download it with open(video_path, "wb") as f: f.write( requests.get( - video_url, proxies=config.proxy, verify=False, timeout=(60, 240) + video_url, headers=headers, proxies=config.proxy, verify=False, timeout=(60, 240) ).content )