|
import requests |
|
|
|
from typing import List |
|
from termcolor import colored |
|
|
|
def search_for_stock_videos(query: str, api_key: str, it: int, min_dur: int) -> List[str]: |
|
""" |
|
Searches for stock videos based on a query. |
|
|
|
Args: |
|
query (str): The query to search for. |
|
api_key (str): The API key to use. |
|
|
|
Returns: |
|
List[str]: A list of stock videos. |
|
""" |
|
|
|
|
|
headers = { |
|
"Authorization": api_key |
|
} |
|
|
|
|
|
qurl = f"https://api.pexels.com/videos/search?query={query}&per_page={it}" |
|
|
|
|
|
r = requests.get(qurl, headers=headers) |
|
|
|
|
|
response = r.json() |
|
|
|
|
|
raw_urls = [] |
|
video_url = [] |
|
video_res = 0 |
|
try: |
|
|
|
for i in range(it): |
|
|
|
if response["videos"][i]["duration"] < min_dur: |
|
continue |
|
raw_urls = response["videos"][i]["video_files"] |
|
temp_video_url = "" |
|
|
|
|
|
for video in raw_urls: |
|
|
|
if ".com/external" in video["link"]: |
|
|
|
if (video["width"]*video["height"]) > video_res: |
|
temp_video_url = video["link"] |
|
video_res = video["width"]*video["height"] |
|
|
|
|
|
if temp_video_url != "": |
|
video_url.append(temp_video_url) |
|
|
|
except Exception as e: |
|
print(colored("[-] No Videos found.", "red")) |
|
print(colored(e, "red")) |
|
|
|
|
|
print(colored(f"\t=> \"{query}\" found {len(video_url)} Videos", "cyan")) |
|
|
|
|
|
return video_url |
|
|