a
    ‡Äf+  ã                   @   sn  d dl mZ d dlZd dlZd dlmZmZmZmZ d dl	m
Z
 d dlZd dlZd dlmZ d dlmZ d dlZd dlZd dlmZ d dlZeƒ  e d¡Ze d	¡Zed
dZejejd e e¡ZeeddZ de j!d< e
ƒ Z"ddiZ#e"j$dd„ ƒZ$e  %d¡dd„ ƒZ&e j%dddgde"j'dd„ ƒƒZ(dd„ Z)d d!„ Z*d"d#„ Z+d$d%„ Z,d&d'„ Z-ed(krje j.d)d* dS )+é    )Úload_dotenvN)ÚResponseÚFlaskÚrequestÚrender_template)ÚHTTPBasicAuth)ÚBeautifulSoup)ÚOpenAI)ÚStringIOÚBASE_URLÚOPENAI_API_KEYz8sk-proj-g5KUovMAPblK2YVhYFWYT3BlbkFJbRnn3OubcnZrWyodkjQk)Úapi_key)Úlevelz/ai-form-bot/static)Ústatic_url_pathz/ai-form-botÚAPPLICATION_ROOTZadminZsecretc                 C   s0   | t v rt |  |kr| S t d| › ¡ d S d S )Nz Failed authentication for user: )ÚusersÚloggerÚwarning)ÚusernameÚpassword© r   ú /var/www/ai-form-bot/formauto.pyÚverify_password$   s    r   z/favicon.icoc                   C   s   dS )N)Ú éÌ   r   r   r   r   r   Úfavicon,   s    r   ú/ÚGETÚPOST)Úmethodsc            	      C   sþ   t jdkröt jd } | rö| jröt | ¡}|d  ¡ }t j d¡t j d¡t j d¡t j d¡t j d¡t j d	¡t j d
¡t j d¡t j d¡t j d¡t j d¡dœ}t	|ƒ}t
|ƒ}t||ƒ}t||||ƒ}t| ¡ dd}|jjdddd |S tdƒS )Nr   ÚfileÚURLÚcompany_nameÚ	full_nameÚkanaÚemailÚphone_numberÚaddressÚpostal_codeÚ
departmentÚpositionÚsite_urlÚmessage)r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   ztext/csv)ÚmimetypezContent-DispositionÚ
attachmentzform_processing_report.csv)Úfilenamezupload.html)r   ÚmethodÚfilesr/   ÚpdÚread_csvÚtolistÚformÚgetÚget_primary_navigation_linksÚget_forms_dataÚmap_forms_dataÚgenerate_csv_reportr   ÚgetvalueÚheadersÚsetr   )	r    ÚdfÚurlsÚ	user_infoÚprimary_navigation_linksÚ
forms_dataÚmapped_forms_dataZ
report_csvÚresponser   r   r   Úupload_file0   s2    














õ
rE   c           	         s  i }| D ]þ}t  d|› ¡ z¨t |¡}t|jdƒ}dd„ |jdddD ƒ}g }|D ]d‰ ˆ rTt‡ fdd	„d
D ƒƒsTdˆ vrTˆ  d¡r¤t	j
 |ˆ  d¡¡}| |¡ qTˆ  d¡rT| ˆ ¡ qT|||< W q tjjy } z"t  d|› d|› ¡ W Y d }~qd }~0 0 q|S )NzProcessing: úhtml.parserc                 S   s   g | ]}|  d ¡‘qS ©Úhref)r6   )Ú.0Úar   r   r   Ú
<listcomp>[   ó    z0get_primary_navigation_links.<locals>.<listcomp>rJ   TrG   c                 3   s   | ]}ˆ   |¡V  qd S )N)Úendswith)rI   Úext©Úlinkr   r   Ú	<genexpr>^   rL   z/get_primary_navigation_links.<locals>.<genexpr>)z.pdfz.jpgz.jpegz.pngz.gifz.bmpú#r   ÚhttpúError fetching URL ú: )r   ÚinfoÚrequestsr6   r   ÚcontentÚfind_allÚanyÚ
startswithÚosÚpathÚjoinÚlstripÚappendÚ
exceptionsÚRequestExceptionÚerror)	r?   Úprimary_navigation_dictÚurlrD   ÚsoupÚlinksZprimary_linksÚfull_urlÚer   rO   r   r7   T   s&    
"

.r7   c                 C   sŽ  i }|   ¡ D ]z\}}i ||< |D ]b}t d|› ¡ z
t |¡}t|jdƒ}| d¡}g }|D ]Ð}	g }
|	 g d¢¡D ]Z}| d¡rzt|ƒ| d¡dœ}| 	d¡}|rÊ| d¡| d	¡krÊt|ƒ|d< |
 
|¡ qz|	 d
¡|	 dd¡ ¡ |
dœ}|d
 r*|d
  d¡r*tj ||d
  d¡¡|d
< | 
|¡ qd||| |< W q" tjjy„ } z"t d|› d|› ¡ W Y d }~q"d }~0 0 q"q|S )NzProcessing form data for: rF   r5   )ÚinputÚtextareaÚselectÚname)Útagrm   ÚlabelÚforÚidÚactionr0   r6   )rr   r0   Úfieldsr   rT   rU   )Úitemsr   rV   rW   r6   r   rX   rY   ÚstrÚfind_previousr`   Úlowerr[   r\   r]   r^   r_   ra   rb   rc   )rd   rB   Úmain_urlÚprimary_urlsÚprimary_urlrD   rf   ÚformsZform_info_listr5   rs   Zinput_fieldÚ
field_inforo   Ú	form_infori   r   r   r   r8   i   s@    



þ
ý0r8   c           
      C   sz   i }|   ¡ D ]h\}}i ||< |  ¡ D ]N\}}|D ]:}|d }t||ƒ}	|	d r0|	d |d< ||| |<  qnq0q$ qq$q|S )Nrs   Úis_contact_formÚform_mapping)rt   Úmap_fields_with_openai)
rB   r@   rC   rx   Zprimary_formsrz   r{   r5   rs   r   r   r   r   r9   ’   s    
r9   c                 C   s@  dt j| ddd› d|d › d|d › d	|d
 › d|d
 › d|d › d|d › d|d › d|d › d|d › d|d › d|d › d|d › d}tjjjdddd œd!|d œgd"d#}|jd$ jj}t	 
d%|› ¡ tjd&d'| ¡ tjd(}zt  |¡}W nB t jy: } z&t	 d)|› ¡ di d*œW  Y d }~S d }~0 0 |S )+Naa  
    Given the following form fields and user information, identify the corresponding input names in the form and map them to the provided values. Handle checkboxes, radio buttons, and select fields appropriately. Don't leave anything empty, fill all inputs and select the most appropriate option in checkboxes or radio buttons.
    select the option Other for the question that means what you would like to inquire about, or what is the purpose of your inquiry.
    
    Determine if the form is a contact form. If it is, provide the form mapping in the following JSON format:
    {
        "is_contact_form": true,
        "form_mapping": {
            "field_name_1": "value_1",
            "field_name_2": "value_2",
            ...
        }
    }
    If it is not a contact form, return:
    {
        "is_contact_form": false
    }
    
    Form Fields:
    Fé   )Úensure_asciiÚindentu+   
    
    User Information:
    ä¼šç¤¾å: r"   u   
    åå‰ï¼ˆå§“åï¼‰: r#   u   
    ãµã‚ŠãŒãª: r$   u   
    ãƒ•ãƒªã‚¬ãƒŠ: u   
    ãƒ¡ãƒ¼ãƒ«ã‚¢ãƒ‰ãƒ¬ã‚¹: r%   u   
    é›»è©±ç•ªå·: r&   u   
    ä½æ‰€: r'   u   
    éƒµä¾¿ç•ªå·: r(   u   
    éƒ¨ç½²å: r)   u   
    å½¹è·: r*   u   
    ã‚µã‚¤ãƒˆURL: r+   u   
    ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸: r,   zà

    For checkboxes and radio buttons, set the value to "true" for the closest option to the user information, otherwise "false".
    For select fields, choose the most appropriate option based on the user information.
    zgpt-3.5-turboÚsystema  You are a sales representative. Please respond with a JSON object mapping form field names to user information values. For checkboxes and radio buttons, use boolean values. For select fields, provide the value of the most appropriate option. Indicate if the form is a contact form.)ÚrolerX   Úuseriô  )ÚmodelÚmessagesÚ
max_tokensr   zOpenAI Response: z^```json\s*|\s*```$r   )ÚflagszJSON decode error: )r~   r   )ÚjsonÚdumpsÚclientÚchatÚcompletionsÚcreateÚchoicesr,   rX   r   rV   ÚreÚsubÚstripÚ	MULTILINEÚloadsÚJSONDecodeErrorrc   )rs   r@   ÚpromptrD   Zresponse_contentZcleaned_responseZresponse_jsonri   r   r   r   r€   ¥   sV    íêéèçæåäãâá à!ß&þú r€   c                 C   sâ   t ƒ }t |¡}| g d¢¡ | D ]°}| |g ¡}t|ƒ}g }	g }
d }d }||v rª||  ¡ D ]J\}}|	 |¡ |
 t||  |g ¡ƒ¡ |r^|}tj	|d dd} qªq^| ||d 
|	¡d 
tt|
ƒ¡||g¡ q"| d¡ |S )N)zMain URLzNumber of Primary URLs FoundzPrimary URLs with Formsz%Number of Forms Found per Primary URLzPrimary URL with Contact FormzMapped Formrs   F)r‚   z, r   )r
   ÚcsvÚwriterÚwriterowr6   Úlenrt   r`   r‹   rŒ   r^   Úmapru   Úseek)r?   rA   rB   rC   Úoutputrš   rx   ry   Znum_primary_urlsZprimary_urls_with_formsZnum_forms_per_primary_urlZprimary_url_with_contact_formZmapped_formrz   r}   r   r   r   r:   â   s8    
	
ú	
r:   Ú__main__T)Údebug)/Údotenvr   r\   ÚloggingÚflaskr   r   r   r   Úflask_httpauthr   Úpandasr2   rW   Úbs4r   Úopenair	   r‹   r™   Úior
   r’   Úgetenvr   r   r   ÚbasicConfigÚINFOÚ	getLoggerÚ__name__r   ÚappÚconfigÚauthr   r   Úrouter   Úlogin_requiredrE   r7   r8   r9   r€   r:   Úrunr   r   r   r   Ú<module>   sJ   




ÿ

")=*
