31
31
from services .resource .resource_service import ResourceService
32
32
from tools .utils import must_have_value
33
33
34
-
35
34
# 获取当前脚本的绝对路径
36
35
script_path = os .path .abspath (__file__ )
37
36
44
43
workdir = os .path .join (script_dir , "../../resource" )
45
44
workdir = os .path .abspath (workdir )
46
45
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
+
47
50
48
51
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 )
50
53
if response .status_code == 200 :
51
54
with open (save_path , 'wb' ) as file :
52
55
for chunk in response .iter_content (chunk_size = 8192 ):
@@ -62,8 +65,9 @@ def __init__(self):
62
65
self .API_KEY = my_config ['resource' ]['pexels' ]['api_key' ]
63
66
must_have_value (self .API_KEY , "请设置pexels密钥" )
64
67
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' ,
65
69
'Authorization' : self .API_KEY
66
- }
70
+ }
67
71
68
72
def match_videos (self , video_data , audio_length ,
69
73
exact_match = False ) -> tuple [list [Any ], int | Any ]:
@@ -93,7 +97,8 @@ def match_videos(self, video_data, audio_length,
93
97
if i == 0 :
94
98
total_length = total_length + video_duration
95
99
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 )
97
102
matching_videos .append (video_url )
98
103
i = i + 1
99
104
break
@@ -106,7 +111,8 @@ def match_videos(self, video_data, audio_length,
106
111
if i == 0 :
107
112
total_length = total_length + video_duration
108
113
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 )
110
116
matching_videos .append (video_url )
111
117
i = i + 1
112
118
break
@@ -145,7 +151,7 @@ def handle_video_resource(self, query, audio_length, per_page=10, exact_match=Fa
145
151
def main ():
146
152
query = input ("Enter search keyword: " )
147
153
resource_service = PexelsService ()
148
- resource_service .handle_video_resource (query , 100 , 1080 , False )
154
+ resource_service .handle_video_resource (query , 100 , 1080 , False )
149
155
150
156
151
157
if __name__ == "__main__" :
0 commit comments