o
    giX                     @   sN   d Z ddlZddlZddlZddlZddlmZ ddlm	Z	 G dd dZ
dS )z
This factory implementation provides access to the unsplash API for retrieving images.
This class expects `keyring.get_password("unsplash", "access_key")` to have been set.
    N)Decimal)Imagec                	   @   s@   e Zd ZdZedeje deje	 deje	 de
fddZdS )	Unsplashz
    This factory implementation provides access to the unsplash API for retrieving images.
    This class expects `keyring.get_password("unsplash", "access_key")` to have been set.
    keywordswidthheightreturnc                 C   sT  d}|dur|dur|| }d dd | D dd }tjd}|dus*J dd}d}d||f }tj|W}	t|		 
 d	 D ]C}
d
|
vrMqFd|
vrRqFd|
vrWqFd|
d vr^qFt|
d
 }t|
d }|| }|durt|| }|du s||k r|
}qF|du r|
}qFW d   n1 sw   Y  |dusJ t|d d ||dS )a3  
        This function returns the best-matching Image (in terms of its dimensions) for a given list of keywords
        :param keywords:    the keywords to be searched
        :param width:       the desired width
        :param height:      the desired height
        :return:            an Image
        N c                 S   s   g | ]}|d  qS )+ ).0kr   r   Y/home/ubuntu/.local/lib/python3.10/site-packages/borb/pdf/canvas/layout/image/unsplash.py
<listcomp>6   s    z&Unsplash.get_image.<locals>.<listcomp>UNSPLASH_API_KEYz(UNSPLASH_API_KEY not found in os.environzChttps://api.unsplash.com/search/photos?page=1&query=%s&client_id=%sresultsr   r   urlsregular)r   r   )joinosenvirongeturllibrequesturlopenjsonloadsreaddecoder   absr   )r   r   r   Rkeyword_strunsplash_access_key	min_delta	min_imageurlresponseresultwhrdeltar   r   r   	get_image$   sN   
zUnsplash.get_imageN)__name__
__module____qualname____doc__staticmethodtypingListstrOptionalr   r   r-   r   r   r   r   r      s    r   )r1   r   r   r3   urllib.requestr   decimalr   "borb.pdf.canvas.layout.image.imager   r   r   r   r   r   <module>   s   