
    i                     8   d Z ddlZddlmZmZ ddlmZ ddlZddlmZ ddl	m
Z
 ddlmZmZ ddlmZmZ dd	lmZ d
ddddddZdgZdZdZdZ ed       ed       ed       ed      dZdZddiddii dZ	 ddddZ	 g dZ	 dZ	 dZ	 d  Zd! Z d"efd#Z!y)$aV  
Dailymotion (Videos)
~~~~~~~~~~~~~~~~~~~~

.. _REST GET: https://developers.dailymotion.com/tools/
.. _Global API Parameters: https://developers.dailymotion.com/api/#global-parameters
.. _Video filters API: https://developers.dailymotion.com/api/#video-filters
.. _Fields selection: https://developers.dailymotion.com/api/#fields-selection

    N)datetime	timedelta)	urlencode)EngineTraits)SearxEngineAPIException)language_tag
region_tag)getraise_for_httperror)html_to_textzhttps://www.dailymotion.comQ769222z%https://www.dailymotion.com/developerTFJSON)websitewikidata_idofficial_api_documentationuse_official_apirequire_api_keyresultsvideos
      )days      im  )dayweekmonthyearis_created_for_kidstrue)   r   r   false)allow_embeddescriptiontitlecreated_timedurationurlthumbnail_360_urlidz#https://api.dailymotion.com/videos?z2https://www.dailymotion.com/embed/video/{video_id}c                 t   | syt         j                  |d   d      }t         j                  |d   d      }| t        j	                  |d   d      d||d   ddd	t
        d
j                  t              d
}|j                  t        j	                  |d   i              t        |d   j                  d            dkD  r||d<   |j                  d      d   |d<   t        j	                  |d         }|r/t        j                         |z
  }t        j                  |      |d<   t!        |      }t"        |z   |d<   |S )NFsearxng_localeen_USen
safesearchr"   originalpageno	relevance,)
searchfamily_filterthumbnail_ratio	languagespagepassword_protectedprivatesortlimitfields-r   localization_country
time_rangecreated_afterr(   )traits
get_regionget_languagefamily_filter_mapr
   number_of_resultsjoinresult_fieldsupdatesafesearch_paramslensplittime_delta_dictr   now	timestampr   
search_url)queryparams
eng_regioneng_langargs
time_deltarC   	query_strs           */root/searxng/searx/engines/dailymotion.pyrequestr[   b   s>   ''/?(@'JJ""6*:#;TBH *..vl/CWM%x %"((=)D 	KK!%%f\&:B?@
 6"#))#./!3)^$**3/2Y !$$VL%9:J 3 ( 2 2= A_$I*F5MM    c           	         g }| j                         }d|v rt        |d   j                  d            t        |        |j                  dg       D ]  }|d   }|d   }t	        |d         }t        |      dkD  r|d d dz   }t        j                  |d	   d       }t        j                  |j                  d
            }|j                  rt        j                  d|      }nt        j                  d|      }|d   }	|	j                  dd      }	d||||||	d}
|d   rt        j                  |d         |
d<   |j                  |
        |S )Nerrormessagelistr%   r(   r$   i,  z...r&   r'   z%H:%M:%Sz%M:%Sr)   zhttp://zhttps://zvideos.html)templater(   r%   contentpublishedDatelength	thumbnailr#   r*   )video_id
iframe_src)jsonr   r
   r   r   rM   r   fromtimestamptimegmtimetm_hourstrftimereplacerg   formatappend)respr   
search_resresr%   r(   rb   rc   rd   re   items              rZ   responseru      sc   GJ *%j&9&=&=i&HII ~~fb) "G%js=12w<#dsme+G ..s>/BDISWWZ01>>]]:v6F]]7F3F+,	%%i<	 &*"
 }!+!2!2CI!2!FDtE"J Nr\   engine_traitsc           	      4   t        dd      }|j                  st        d      |j                         d   D ]{  }|d   }|dv r	 t	        t
        j                  j                  |            }| j                  j                  |      }|r||k7  rt        d	|d
|d|       m|| j                  |<   } | j                  j                         D cg c]  }|j                  d      d    }}t        dd      }|j                  st        d      |j                         d   D ]C  }|d   }||v st        t
        j                  j                  |            }|| j                  |<   E y# t
        j                  $ r t        d|z         Y Yw xY wc c}w )a1  Fetch locales & languages from dailymotion.

    Locales fetched from `api/locales <https://api.dailymotion.com/locales>`_.
    There are duplications in the locale codes returned from Dailymotion which
    can be ignored::

      en_EN --> en_GB, en_US
      ar_AA --> ar_EG, ar_AE, ar_SA

    The language list `api/languages <https://api.dailymotion.com/languages>`_
    contains over 7000 *languages* codes (see PR1071_).  We use only those
    language codes that are used in the locales.

    .. _PR1071: https://github.com/searxng/searxng/pull/1071

    z#https://api.dailymotion.com/locales   )timeoutz,Response from Dailymotion locales is not OK.r`   locale)en_ENar_AAzERROR: item unknown --> %szCONFLICT: babel z --> z, r@   r   z%https://api.dailymotion.com/languagesz.Response from Dailymotion languages is not OK.codeN)r
   okRuntimeErrorrh   r	   babelLocaleparseUnknownLocaleErrorprintregionsvaluesrN   r   r7   )rv   rq   rt   eng_tagsxng_tagconflictxlocale_lang_lists           rZ   fetch_traitsr      s   $ 4a@D77IJJ		F# 2x.((	!%,,"4"4W"=>H
 !((,,X67"8WUV*1h'2" 2?1F1F1M1M1OPAQPP6BD77KLL		F# 8v,&&#ELL$6$6w$?@H07M##H-	8# '' 	.56	 Qs   (E-F-!FF)"__doc__rj   r   r   urllib.parser   r   searx.enginelib.traitsr   searx.exceptionsr   searx.localesr   r	   searx.networkr
   r   searx.utilsr   about
categoriespagingrH   time_range_supportrO   r/   rL   rG   rJ   rR   rg   r[   ru   r    r\   rZ   <module>r      s  	  ( "  / 4 2 % -"I	 Z
	  !1B3	 
v&v&	 

  

	
 3
 B
 ;*\1h18 18r\   