o
    �›Xjb
  ã                   @  sŒ   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlZer)	 dZeG dd	„ d	ƒƒZdddd„Zddd„Zd dd„ZdS )!é    )ÚannotationsN)Ú	dataclass)ÚPath)ÚTYPE_CHECKING)Úurljoinz!ContentPipeline/1.0 (+local cron)c                   @  s*   e Zd ZU ded< ded< dZded< dS )ÚFetchedImageÚstrÚpathÚsourceNú
str | NoneÚurl)Ú__name__Ú
__module__Ú__qualname__Ú__annotations__r   © r   r   úH/home/esfera/Documents/content_generation/src/generators/image_source.pyr      s   
 r   é   r   r   Údestr   ÚtimeoutÚintÚreturnúFetchedImage | Nonec           	      C  s  | sdS zt j| |dtidd�}| ¡  |jdd… }W n
 ty&   Y dS w t||jƒ}|s1dS zRt j||dtidd�}| ¡  |j d¡pHd 	d¡d	  
¡ }| d
¡sXW dS t|jƒdk sft|jƒdkriW dS t|ƒ}|jjddd� | |j¡ t|jd|d�W S  ty�   Y dS w )zJTry og:image / twitter:image from the article (editorial / link-back use).Nz
User-AgentT)r   ÚheadersÚallow_redirectsi@ zContent-TypeÚ ú;r   zimage/iˆ  i z )ÚparentsÚexist_okÚnews_og)r	   r
   r   )ÚrequestsÚgetÚ
USER_AGENTÚraise_for_statusÚtextÚ	ExceptionÚ_extract_image_urlr   r   ÚsplitÚlowerÚ
startswithÚlenÚcontentÚ_ext_from_content_typeÚparentÚmkdirÚwrite_bytesr   Úname)	r   r   r   ÚrespÚhtmlÚ	image_urlÚimg_respÚctypeÚextr   r   r   Úfetch_news_image   s@   ÿü
ÿr7   r2   Úbase_urlr   c                 C  sB   g d¢}|D ]}t  || t j¡}|rt|| d¡ ¡ ƒ  S qd S )N)zP<meta[^>]+property=["\']og:image:secure_url["\'][^>]+content=["\']([^"\']+)["\']zP<meta[^>]+content=["\']([^"\']+)["\'][^>]+property=["\']og:image:secure_url["\']zE<meta[^>]+property=["\']og:image["\'][^>]+content=["\']([^"\']+)["\']zE<meta[^>]+content=["\']([^"\']+)["\'][^>]+property=["\']og:image["\']zF<meta[^>]+name=["\']twitter:image["\'][^>]+content=["\']([^"\']+)["\']zF<meta[^>]+content=["\']([^"\']+)["\'][^>]+name=["\']twitter:image["\']é   )ÚreÚsearchÚIr   ÚgroupÚstrip)r2   r8   ÚpatternsÚpatÚmr   r   r   r&   <   s   ÿr&   r5   c                 C  s   ddddddœ}|  | d¡S )Nz.jpgz.pngz.webpz.gif)z
image/jpegz	image/jpgz	image/pngz
image/webpz	image/gif)r!   )r5   Úmappingr   r   r   r,   L   s   ûr,   )r   )r   r   r   r   r   r   r   r   )r2   r   r8   r   r   r   )r5   r   r   r   )Ú
__future__r   r:   Údataclassesr   Úpathlibr   Útypingr   Úurllib.parser   r    r"   r   r7   r&   r,   r   r   r   r   Ú<module>   s    
$