o
    hA                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZm	Z	 d dl
mZmZ d dlmZ dd Zdd Zd	d
 ZdededefddZdd Zdd Zdd Zdd Zdd ZdddZdd ZdS )     N)FastMailMessageSchemaConnectionConfig)HTTPExceptionstatus)JSONResponsec                   C   s   d tjtjtj ddS )N    )k)joinrandomchoicesstringascii_uppercasedigits r   r   6/var/www/html/storm_article_api/dependencies/helper.pygenerate_random_text   s   r   c                 C   s   t dd| }|S )Nz[^a-zA-Z0-9]_)resub)input_stringsanitized_stringr   r   r   replace_special_characters   s   r   c                 C   s   dd |  dg D }|S )Nc                 S   s   g | ]}|d  qS )messager   ).0errorr   r   r   
<listcomp>   s    z%get_error_message.<locals>.<listcomp>errors)get)responsemessagesr   r   r   get_error_message   s   r"   	recipientsubjectbodyc              
      s   t tdtdtddtddddd}t|| g|d	d
}t|}z||I d H  W d S  tyE } ztdt|dd }~ww )NSES_ACCESS_KEY_IDSES_SECRET_ACCESS_KEYSES_SENDER_EMAILi  SES_AWS_HOSTFT)MAIL_USERNAMEMAIL_PASSWORD	MAIL_FROM	MAIL_PORTMAIL_SERVERMAIL_STARTTLSMAIL_SSL_TLSUSE_CREDENTIALShtml)r$   
recipientsr%   subtypei  )status_codedetail)	r   osgetenvr   r   send_message	Exceptionr   str)r#   r$   r%   confr   fmer   r   r   
send_email   s0   r?   c                 C   s6   t | }t|W  d   S 1 sw   Y  dS )aj  
    Reads a JSON file and returns its content as a Python dictionary or list,
    depending on the JSON structure.

    Args:
        file_path (str): The path to the JSON file to be read.

    Returns:
        dict or list: The content of the JSON file. The type depends on the
                    structure of the JSON file (object or array at the root).
    N)openjsonload	file_pathfr   r   r   read_json_file0   s   
$rF   c                 C   s4   t | }| W  d    S 1 sw   Y  d S )N)r@   readrC   r   r   r   read_txt_file?   s   
$rH   c                 C   s   t d}|d| } | S )Nz]:\s+"(.*?)"\s+httpz]: http)r   compiler   )textregexr   r   r   parseD   s   
rL   c                 C   sR   | d u rd S i }| d   D ]\}}|| d | d | d | d d||< q|S )Nurl_to_unified_indexurl_to_infotitlesnippets)urlrO   rP   )items)search_resultscitation_dictrQ   indexr   r   r   +_construct_citation_dict_from_search_resultI   s   rV   c                 C   s   t dd|  S )Nz\s+ )r   r   strip)r   r   r   r   normalize_spacesU   s   rY     c                 C   s   |st j}t||| ddS )N)r   r   r5   content)r   HTTP_400_BAD_REQUESTr   )r   r5   r   r   r   errorResponseX   s   r^   c                 C   s   t tjtj| |ddS )N)r   r   datar[   )r   r   HTTP_200_OK)r   r_   r   r   r   successResponsec   s   ra   )rZ   )rA   r7   r   r   r   base64fastapi_mailr   r   r   fastapir   r   fastapi.responsesr   r   r   r"   r;   r?   rF   rH   rL   rV   rY   r^   ra   r   r   r   r   <module>   s(    
