o
    NK&h
                     @   s   d Z dZddlmZmZmZ ddlZddlmZ dddd	d
Z	dee
ee
 f de
fddZ		dde
dee
 defddZ	ddee
 de
defddZdS )z
language detection API
z"Copyright (C) 2020 Nidhal Baccouri    )ListOptionalUnionN)	HTTPErrorz(https://ws.detectlanguage.com/0.2/detectz'Detect Language API Python Client 1.4.0z	Bearer {})z
User-AgentAuthorization)urlheaderstextapi_keyc              
   O   s   |st d| st dz#td }|d ||d< tjtd d| i|d}| d}|W S  tyC } ztd	|j	 |d
}~ww )z
    send a request and return the response body parsed as dictionary

    @param text: target text that you want to detect its language
    @type text: str
    @type api_key: str
    @param api_key: your private API key

    zlyou need to get an API_KEY for this to work. Get one for free here: https://detectlanguage.com/documentationzPlease provide an input textr   r   r   q)jsonr   dataz,Error occured while requesting from server: N)
	Exceptionconfigformatrequestspostr   getr   printargs)r	   r
   r   kwargsr   responsebodye r   L/var/www/html/venv/lib/python3.10/site-packages/deep_translator/detection.pyget_request_body   s&   r   Fdetailedc                 O   s<   t | |}|d}|r|d S |d dd}|r|S dS )aE  
    function responsible for detecting the language from a text

    @param text: target text that you want to detect its language
    @type text: str
    @type api_key: str
    @param api_key: your private API key
    @param detailed: set to True if you want to get detailed
    information about the detection process
    
detectionsr   languageNr   r   )r	   r
   r   r   r   r   r   langr   r   r   single_detection:   s   

r"   	text_listc                 O   s8   t | |}|d}dd |D }|r|S dd |D S )a   
    function responsible for detecting the language from a text

    @param text_list: target batch that you want to detect its language
    @param api_key: your private API key
    @param detailed: set to True if you want to
    get detailed information about the detection process
    r   c                 S      g | ]}|d  qS )r   r   .0objr   r   r   
<listcomp>b       z#batch_detection.<locals>.<listcomp>c                 S   r$   )r   r   r%   r   r   r   r(   f   r)   r    )r#   r
   r   r   r   r   r   resr   r   r   batch_detectionU   s   

r+   )NF)F)__doc____copyright__typingr   r   r   r   requests.exceptionsr   r   strr   boolr"   r+   r   r   r   r   <module>   s@    	
&
