o
    sg1!                     @   s   d Z ddlZddlZddlZddlmZ ddlZddlZddlmZ ddl	m
Z
mZ dddd	Zdd
lmZ G dd dZedkrVe Zejdddd ejdddd dS dS )zconvert svg to png    N)datetime)svg2png)ImageUnidentifiedImageErrorzNMozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0*z
keep-alive)z
User-AgentzAccept-Encoding
Connection)STOCK_IMAGE_FILENAMEc                   @   s\   e Zd ZdZeZdd ZddedefddZedd	d
Z	ddedefddZ
dddZdS )DownloadImagez:
    Download image from url and save it to file_name
    c                 C   s   d S )N )selfr
   r
   0/var/www/html/XCapMarket/utils/download_image.py__init__   s   zDownloadImage.__init__Turl	file_namec           
   
      sz  || j kr| j }t|}|  ||fS ztdd|dd |dd  }tdd|dd |dd  }tdd|dd |dd  }|drW| |||W S z
tj	|d	t
d
}W n> tjjyx   tdt d| Y n( tjjy   tdt d| | j }t|}|  ||f Y W S w d| d }tt|j}|d}td|jd}t||}|d}|j|dd |  |  ||fW S  ty   | j }t|}|  ||f Y S  tjjy   | j }t|}|  ||f Y S  ty< }	 z"tdt d|	 | j }t|}|  ||fW  Y d}	~	S d}	~	ww )
        Get image from url and save it to file_name
        Input: url, file_name to save image as, picture_background_dark if is dark or not (true, false)
        OUtput: image file, file_name
        z\s_Nz[^a-zA-Z0-9_] __.svgd   timeoutheaders(%s: Download Images: Read timeout for %s%Y-%m-%d %H:%M:%Sz,%s: Download Images: Connection error for %s	images/%sz.jpgRGBA   r   r   RGB2   qualityz/%s: Download Images: Error downloading image %s)r   r   opencloseresubendswithsvg_imgrequestsgetrequest_headers
exceptionsReadTimeoutloggingerrorr   nowstrftimeConnectionErrorioBytesIOcontentconvertnewsizealpha_compositesaver   MissingSchema	Exception)
r   r   r   picture_background_darkstock_image_filenameimgresponse
backgroundr:   er
   r
   r   	get_image    s~   

"""







zDownloadImage.get_imagec                 C   sf   d}|du rd}zt |||dddd}||fW S  tjjy2   | j}t|}|  ||f Y S w )z4For SVG images, convert to PNG and save to file_nameNFblack   `   433333O@r   write_tobackground_colorscaledpioutput_height)r   
defusedxmlcommonEntitiesForbiddenr   r   r$   r%   )clsr   r   r>   rK   r@   r?   r
   r
   r   r)   r   s&   
	
zDownloadImage.svg_imgc           
   	   C   s  zdt dd| }|drDd}|du rd}zt|||dd	d
d}||fW W S  tjjyC   | j}t	|}|
  ||f Y W S w z
tj|dtd}W n( tjjyv   tdt d| | j}t	|}|
  ||f Y W S w t	t|j}|d}td|jd}t||}	|	d}	|	j|dd |	
  |
  |	|fW S  ty   | j}t	|}|
  ||f Y S  tjjy   | j}t	|}|
  ||f Y S w )r   r    r   r   NFrE   rF   rG   rH   rI   r   r   r   r   r   r   r    r!   r"   )r&   r'   r(   r   rO   rP   rQ   r   r   r$   r%   r*   r+   r,   r-   r.   r/   r0   r   r1   r2   r4   r5   r6   r7   r8   r9   r:   r;   r   r<   )
r   r   r   r>   rK   r@   r?   rA   rB   r:   r
   r
   r   
_get_image   sn   
	






zDownloadImage._get_imagerF   c                 C   sz   |d }|d }t |}d}|j|kr||j }n
|j|kr$||j }|t|j| t|j| f}|  ||fS )z2resize image to max_width and max_height in inchesrG   rF   )r   r$   widthheightresizeintr%   )r   
image_path	max_width
max_heightr@   rescaler
   r
   r   resize_image   s   



"zDownloadImage.resize_imageN)T)rF   rF   )__name__
__module____qualname____doc__r   r   strrD   classmethodr)   rT   r]   r
   r
   r
   r   r	      s    RFr	   __main__z5https://asset.brandfetch.io/idLdViRnHy/idXqSF1uBx.svgzscale_logo.pngT)r>   gGz?g?)rZ   r[   )ra   r4   r/   r&   r   rO   r*   cairosvgr   PILr   r   r,   configs.configr   r	   r^   download_imagerD   r]   r
   r
   r
   r   <module>   s2     O