Spaces:
Runtime error
Runtime error
Set user-agent for Pexels API call
Browse files8 unmodified lines91011121314151617181920212223248 unmodified lines3334353637383 unmodified lines4243444546474852 unmodified lines10110210310410510610713 unmodified lines1211221238 unmodified linesfrom urllib.parse import urlparse, parse_qsimport requestsREQUEST_TIMEOUT = 12MAX_PHOTOS = 3# Only show warningslogging.getLogger('urllib3').setLevel(logging.WARNING)# Disable all child loggers of urllib3, e.g. urllib3.connectionpoollogging.getLogger('urllib3').propagate = False8 unmodified linesThis function sends a GET request to the Pexels API with the specified search queryand authorization header containing the API key. It returns the JSON response from the API.: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.3 unmodified linesurl = 'https://api.pexels.com/v1/search'headers = {'Authorization': os.getenv('PEXEL_API_KEY')}params = {'query': query,52 unmodified lines:raises requests.exceptions.RequestException: If the request to the URL fails."""response = requests.get(url, stream=True, timeout=REQUEST_TIMEOUT)response.raise_for_status()image_data = BytesIO(response.content)13 unmodified linesheight = int(query_params.get('h', [0])[0])return width, height8 unmodified lines9101112131415161718192021222324252627288 unmodified lines37383940414243444546474849503 unmodified lines545556575859606152 unmodified lines11411511611711811912012112212312413 unmodified lines1381391401411421431441451461471488 unmodified linesfrom urllib.parse import urlparse, parse_qsimport requestsfrom dotenv import load_dotenvload_dotenv()REQUEST_TIMEOUT = 12MAX_PHOTOS = 3# Only show errorslogging.getLogger('urllib3').setLevel(logging.ERROR)# Disable all child loggers of urllib3, e.g. urllib3.connectionpool# logging.getLogger('urllib3').propagate = True8 unmodified linesThis function sends a GET request to the Pexels API with the specified search queryand 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 toCloudflare (or others) blocking the requests, perhaps identifying as Web-scraping. So,changing the user-agent to Firefox.https://stackoverflow.com/a/74674276/147021https://stackoverflow.com/a/51268523/147021https://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.3 unmodified linesurl = '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,52 unmodified lines: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)13 unmodified linesheight = int(query_params.get('h', [0])[0])return width, heightif __name__ == '__main__':print(search_pexels(query='people'))
helpers/image_search.py+30-5