Skip to content

Commit 5fc771f

Browse files
author
wayne
committed
修复pexels无法下载视频的问题
1 parent 7163a80 commit 5fc771f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

services/resource/pexels_service.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
from services.resource.resource_service import ResourceService
3232
from tools.utils import must_have_value
3333

34-
3534
# 获取当前脚本的绝对路径
3635
script_path = os.path.abspath(__file__)
3736

@@ -44,9 +43,13 @@
4443
workdir = os.path.join(script_dir, "../../resource")
4544
workdir = os.path.abspath(workdir)
4645

46+
HEADERS = {
47+
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36',
48+
}
49+
4750

4851
def download_video(video_url, save_path):
49-
response = requests.get(video_url, stream=True)
52+
response = requests.get(video_url, headers=HEADERS, stream=True)
5053
if response.status_code == 200:
5154
with open(save_path, 'wb') as file:
5255
for chunk in response.iter_content(chunk_size=8192):
@@ -62,8 +65,9 @@ def __init__(self):
6265
self.API_KEY = my_config['resource']['pexels']['api_key']
6366
must_have_value(self.API_KEY, "请设置pexels密钥")
6467
self.headers = {
68+
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36',
6569
'Authorization': self.API_KEY
66-
}
70+
}
6771

6872
def match_videos(self, video_data, audio_length,
6973
exact_match=False) -> tuple[list[Any], int | Any]:
@@ -93,7 +97,8 @@ def match_videos(self, video_data, audio_length,
9397
if i == 0:
9498
total_length = total_length + video_duration
9599
else:
96-
total_length = total_length + video_duration - float(self.video_transition_effect_duration)
100+
total_length = total_length + video_duration - float(
101+
self.video_transition_effect_duration)
97102
matching_videos.append(video_url)
98103
i = i + 1
99104
break
@@ -106,7 +111,8 @@ def match_videos(self, video_data, audio_length,
106111
if i == 0:
107112
total_length = total_length + video_duration
108113
else:
109-
total_length = total_length + video_duration - float(self.video_transition_effect_duration)
114+
total_length = total_length + video_duration - float(
115+
self.video_transition_effect_duration)
110116
matching_videos.append(video_url)
111117
i = i + 1
112118
break
@@ -145,7 +151,7 @@ def handle_video_resource(self, query, audio_length, per_page=10, exact_match=Fa
145151
def main():
146152
query = input("Enter search keyword: ")
147153
resource_service = PexelsService()
148-
resource_service.handle_video_resource(query, 100, 1080, False)
154+
resource_service.handle_video_resource(query, 100, 1080, False)
149155

150156

151157
if __name__ == "__main__":

0 commit comments

Comments
 (0)