Skip to content

Commit

Permalink
fixing line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
gptjozef authored Feb 9, 2024
1 parent e8f9289 commit f4c722d
Show file tree
Hide file tree
Showing 5 changed files with 710 additions and 693 deletions.
129 changes: 66 additions & 63 deletions Backend/search.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,66 @@
import requests

from typing import List
from termcolor import colored

def search_for_stock_videos(query: str, api_key: str, it: 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.
"""

# Build headers
headers = {
"Authorization": api_key
}

# Build URL
qurl = f"https://api.pexels.com/videos/search?query={query}&per_page={it}"

# Send the request
r = requests.get(qurl, headers=headers)

# Parse the response
response = r.json()

# Parse each video
raw_urls = []
video_url = []
video_res = 0
try:
# loop through each video in the result
for i in range(it):
raw_urls = response["videos"][i]["video_files"]
temp_video_url = ""

# loop through each url to determine the best quality
for video in raw_urls:
# Check if video has a valid download link
if ".com/external" in video["link"]:
# Only save the URL with the largest resolution
if (video["width"]*video["height"]) > video_res:
temp_video_url = video["link"]
video_res = video["width"]*video["height"]

# add the url to the return list if it's not empty
if temp_video_url != "":
video_url.append(temp_video_url)

except Exception as e:
print(colored("[-] No Videos found.", "red"))
print(colored(e, "red"))

# Let user know
print(colored(f"\t=> \"{query}\" found {len(video_url)} Videos", "cyan"))

# Return the video url
return video_url
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.
"""

# Build headers
headers = {
"Authorization": api_key
}

# Build URL
qurl = f"https://api.pexels.com/videos/search?query={query}&per_page={it}"

# Send the request
r = requests.get(qurl, headers=headers)

# Parse the response
response = r.json()

# Parse each video
raw_urls = []
video_url = []
video_res = 0
try:
# loop through each video in the result
for i in range(it):
#check if video has desired minimum duration
if response["videos"][i]["duration"] < min_dur:
continue
raw_urls = response["videos"][i]["video_files"]
temp_video_url = ""

# loop through each url to determine the best quality
for video in raw_urls:
# Check if video has a valid download link
if ".com/external" in video["link"]:
# Only save the URL with the largest resolution
if (video["width"]*video["height"]) > video_res:
temp_video_url = video["link"]
video_res = video["width"]*video["height"]

# add the url to the return list if it's not empty
if temp_video_url != "":
video_url.append(temp_video_url)

except Exception as e:
print(colored("[-] No Videos found.", "red"))
print(colored(e, "red"))

# Let user know
print(colored(f"\t=> \"{query}\" found {len(video_url)} Videos", "cyan"))

# Return the video url
return video_url
Loading

0 comments on commit f4c722d

Please sign in to comment.