|
import requests |
|
|
|
from shortGPT.config.api_db import ApiKeyManager |
|
|
|
|
|
def search_videos(query_string, orientation_landscape=True): |
|
url = "https://api.pexels.com/videos/search" |
|
headers = { |
|
"Authorization": ApiKeyManager.get_api_key("PEXELS") |
|
} |
|
params = { |
|
"query": query_string, |
|
"orientation": "landscape" if orientation_landscape else "portrait", |
|
"per_page": 15 |
|
} |
|
|
|
response = requests.get(url, headers=headers, params=params) |
|
json_data = response.json() |
|
|
|
|
|
|
|
|
|
return json_data |
|
|
|
|
|
def getBestVideo(query_string, orientation_landscape=True, used_vids=[]): |
|
vids = search_videos(query_string, orientation_landscape) |
|
videos = vids['videos'] |
|
|
|
|
|
if orientation_landscape: |
|
filtered_videos = [video for video in videos if video['width'] >= 1920 and video['height'] >= 1080 and video['width']/video['height'] == 16/9] |
|
else: |
|
filtered_videos = [video for video in videos if video['width'] >= 1080 and video['height'] >= 1920 and video['height']/video['width'] == 16/9] |
|
|
|
|
|
sorted_videos = sorted(filtered_videos, key=lambda x: abs(15-int(x['duration']))) |
|
|
|
|
|
for video in sorted_videos: |
|
for video_file in video['video_files']: |
|
if orientation_landscape: |
|
if video_file['width'] == 1920 and video_file['height'] == 1080: |
|
if not (video_file['link'].split('.hd')[0] in used_vids): |
|
return video_file['link'] |
|
else: |
|
if video_file['width'] == 1080 and video_file['height'] == 1920: |
|
if not (video_file['link'].split('.hd')[0] in used_vids): |
|
return video_file['link'] |
|
print("NO LINKS found for this round of search with query :", query_string) |
|
return None |
|
|