Spaces:
Running
Running
File size: 4,551 Bytes
437156d 71518f2 437156d 7bf36e8 437156d bb25da3 437156d e481e40 437156d bb25da3 71518f2 bb25da3 71518f2 7bf36e8 437156d bb25da3 437156d bb25da3 437156d bb25da3 437156d bb25da3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
"""
Search photos using Pexels API.
"""
import logging
import os
import random
from io import BytesIO
from typing import Union, Tuple, Literal
from urllib.parse import urlparse, parse_qs
import requests
from dotenv import load_dotenv
load_dotenv()
REQUEST_TIMEOUT = 12
MAX_PHOTOS = 3
# Only show errors
logging.getLogger('urllib3').setLevel(logging.ERROR)
# Disable all child loggers of urllib3, e.g. urllib3.connectionpool
# logging.getLogger('urllib3').propagate = True
def search_pexels(
query: str,
size: Literal['small', 'medium', 'large'] = 'medium',
per_page: int = MAX_PHOTOS
) -> dict:
"""
Searches for images on Pexels using the provided query.
This function sends a GET request to the Pexels API with the specified search query
and authorization header containing the API key. It returns the JSON response from the API.
[2024-08-31] Note:
`curl` succeeds but API call via Python `requests` fail. Apparently, this could be due to
Cloudflare (or others) blocking the requests, perhaps identifying as Web-scraping. So,
changing the user-agent to Firefox.
https://stackoverflow.com/a/74674276/147021
https://stackoverflow.com/a/51268523/147021
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox#linux
:param query: The search query for finding images.
:param size: The size of the images: small, medium, or large.
:param per_page: No. of results to be displayed per page.
:return: The JSON response from the Pexels API containing search results.
:raises requests.exceptions.RequestException: If the request to the Pexels API fails.
"""
url = 'https://api.pexels.com/v1/search'
headers = {
'Authorization': os.getenv('PEXEL_API_KEY'),
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0',
}
params = {
'query': query,
'size': size,
'page': 1,
'per_page': per_page
}
response = requests.get(url, headers=headers, params=params, timeout=REQUEST_TIMEOUT)
response.raise_for_status() # Ensure the request was successful
return response.json()
def get_photo_url_from_api_response(
json_response: dict
) -> Tuple[Union[str, None], Union[str, None]]:
"""
Return a randomly chosen photo from a Pexels search API response. In addition, also return
the original URL of the page on Pexels.
:param json_response: The JSON response.
:return: The selected photo URL and page URL or `None`.
"""
page_url = None
photo_url = None
if 'photos' in json_response:
photos = json_response['photos']
if photos:
photo_idx = random.choice(list(range(MAX_PHOTOS)))
photo = photos[photo_idx]
if 'url' in photo:
page_url = photo['url']
if 'src' in photo:
if 'large' in photo['src']:
photo_url = photo['src']['large']
elif 'original' in photo['src']:
photo_url = photo['src']['original']
return photo_url, page_url
def get_image_from_url(url: str) -> BytesIO:
"""
Fetches an image from the specified URL and returns it as a BytesIO object.
This function sends a GET request to the provided URL, retrieves the image data,
and wraps it in a BytesIO object, which can be used like a file.
:param url: The URL of the image to be fetched.
:return: A BytesIO object containing the image data.
:raises requests.exceptions.RequestException: If the request to the URL fails.
"""
headers = {
'Authorization': os.getenv('PEXEL_API_KEY'),
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0',
}
response = requests.get(url, headers=headers, stream=True, timeout=REQUEST_TIMEOUT)
response.raise_for_status()
image_data = BytesIO(response.content)
return image_data
def extract_dimensions(url: str) -> Tuple[int, int]:
"""
Extracts the height and width from the URL parameters.
:param url: The URL containing the image dimensions.
:return: A tuple containing the width and height as integers.
"""
parsed_url = urlparse(url)
query_params = parse_qs(parsed_url.query)
width = int(query_params.get('w', [0])[0])
height = int(query_params.get('h', [0])[0])
return width, height
if __name__ == '__main__':
print(
search_pexels(
query='people'
)
)
|