
    ip                        U d 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	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mZmZmZ ej,                  rddlmZ dd	lmZ d
ddddddZeeej<                  f   ed<   ddgZ dZ!ddddZ"ee#ef   ed<   	 dZ$	 dedz  deeef   dz  fdZ%dddedz  ddfdZ&d eddddfd!Z'd"d#de(eeej<                  f      fd$Z)d%eddfd&Z*y)'a9  This is the implementation of the Bing-Web engine.  Some of this
implementations are shared by other engines:

- :ref:`bing images engine`
- :ref:`bing news engine`
- :ref:`bing videos engine`

.. note::

   Some functionality (paging and time-range results) are not supported
   since they depend on JavaScript.
    N)parse_qs	urlencodeurlparse)html)EngineTraits)
region_tag)
eval_xpatheval_xpath_getindexeval_xpath_listextract_text)SXNG_Response)OnlineParamszhttps://www.bing.comQ182496z*https://github.com/MicrosoftDocs/bing-docsFHTML)websitewikidata_idofficial_api_documentationuse_official_apirequire_api_keyresultsaboutgeneralwebToffmoderatestrict)r         _safesearch_mapzhttps://www.bing.com/searchengine_regionreturnc                     | r| dk(  ryd| iS )a  API documentation states the ``mkt`` parameter is *the
    recommended primary signal* for locale:

        If known, you are encouraged to always specify the market.
        Specifying the market helps Bing route the request and return an
        appropriate and optimal response.

    The ``mkt`` parameter takes a full ``<language>-<country>`` code.

    This function is shared with :py:mod:`searx.engines.bing_images`,
    :py:mod:`searx.engines.bing_news`, and :py:mod:`searx.engines.bing_videos`.
    clearNmkt )r    s    #/root/searxng/searx/engines/bing.pyget_locale_paramsr'   7   s     MW4=!!    paramsr   c                 X    |r|dk(  ry|j                  d      d   }| d| d| d   d<   y)	a  Override the ``Accept-Language`` header.

    The default header built by :py:class:`~searx.search.processors.online.OnlineProcessor`
    appends ``en;q=0.3`` as a fallback language::

        Accept-Language: de,de-DE;q=0.7,en;q=0.3

    Bing seems to better select the results locale based on the
    ``Accept-Language`` value header.

    This function is shared with :py:mod:`searx.engines.bing_images`,
    :py:mod:`searx.engines.bing_news`, and :py:mod:`searx.engines.bing_videos`.
    r#   N-r   ,z;q=0.9headersAccept-Language)split)r)   r    langs      r&   override_accept_languager1   K   sC     MW4s#A&D.;_AdV6+JF9'(r(   queryc                 2   t         j                  |d   t         j                        }t        ||       | t        j                  |j                  dd      d      d}t        |      }|r|j                  |       t         dt        |       |d<   d|d	<   |S )
zAssemble a Bing-Web request.searxng_locale
safesearchr   r   )qadlt?urlTallow_redirects)
traits
get_region
all_localer1   r   getr'   updatebase_urlr   )r2   r)   r    query_paramslocale_paramss        r&   requestrC   a   s     %%f-=&>@Q@QRMV]3 ##FJJ|Q$?G*L
 &m4MM*j)L"9!:;F5M !%FMr(   respr   c                    g }t        j                  | j                        }t        |d      D ]@  }t	        |ddd      }||j
                  j                  dd      }t        |      }|r|sA|j                  d      rt        t        |      j                        }|j                  d      }|rV|d   }	|	j                  d	      r@|	d
d }
|
dt        |
       dz  z  z  }
t        j                  |
      j                  dd      }t!        |d      }|D ]7  }|j#                  d      D ]!  }|j%                         j'                  |       # 9 t        |      }|j)                  |||d       C |rPdj+                  t!        |d            }t-        j.                  dd|      }|r|j)                  dt1        |      i       |S )zGet response from Bing-Webz4//ol[@id="b_results"]/li[contains(@class, "b_algo")]z.//h2/ar   Nhref zhttps://www.bing.com/ck/a?ua1r   =   zutf-8replace)errorsz.//pz.//span[@class="algoSlug_icon"])r9   titlecontentz!//span[@class="sb_count"]//text()z[^0-9]number_of_results)r   
fromstringtextr   r
   attribr>   r   
startswithr   r   r2   lenbase64urlsafe_b64decodedecoder	   xpath	getparentremoveappendjoinresubint)rD   r   domitemlinkrF   rN   qsu_valuesu_valencodedcontent_elspiconrO   result_len_containers                   r&   responserl   z   s    ')G
//$))
$C%[\ J"4At<<{{vr*T"5 ??78(4.../Bvvc{H ##D)#ABiGss7|ma&788G!33G<CCGT]C^D !v. 	.A AB . ''-.	. {+teHI?JB !wwz#7Z'[\!vvi5IJNN/5I1JKLNr(   engine_traitsc           
      *   ddl m} ddlm}  |       dddddddd	} |d
|d      }|j                  st        d      t        j                  |j                        }ddi}t        |d      D ]	  }t        t        |      j                        d   d   }|dk(  r|| _        5t        j                  j!                  |d      D ]  }	|	j#                  d      d   }	|	 d| }
|j                  |
|
      }
	 t%        t        j&                  j)                  |	d|j+                                     }| j.                  j                  |      }|r||
k7  rt1        d|d|d|
       |
| j.                  |<     y# t        j,                  $ r Y w xY w)zFetch regions from Bing-Web.r   )r>   )gen_useragentzJtext/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8zen-US;q=0.5,en;q=0.31z
keep-alivez	max-age=0)z
User-AgentAcceptr.   DNT
ConnectionzUpgrade-Insecure-RequestszSec-GPCzCache-Controlz$https://www.bing.com/account/general   )r-   timeoutzResponse from Bing is not OK.zzh-hkzen-hkzE//div[@id="region-section-content"]//div[@class="regionItem"]/a/@hrefccr#   T)de_facto_r+   zCONFLICT: babel z --> z, N)searx.networkr>   searx.utilsro   okRuntimeErrorr   rQ   rR   r	   r   r   r2   r=   babel	languagesget_official_languagesr/   r   LocaleparseupperUnknownLocaleErrorregionsprint)rm   r>   ro   r-   rD   ra   map_market_codesrF   cc_taglang_tagmarket_codesxng_tagconflicts                r&   fetch_traitsr      s    ") $o^1"%($	G 5wPQRD77:;;
//$))
$C 	( 3 gh :(4.../5a8W'-M$ >>vPT>U 	:H~~c*1-H%Jax0K*..{KHK%ell&8&8Hflln9]&^_
 %,,00:H{*XxQ\]^.9M!!(+#	:: ++ s   5;E<<FF)+__doc__rV   r^   typingturllib.parser   r   r   r}   babel.languageslxmlr   searx.enginelib.traitsr   searx.localesr   rz   r	   r
   r   r   TYPE_CHECKINGsearx.extended_typesr   searx.search.processorsr   r   dictstrAny__annotations__
categoriesr5   r   r`   r@   r'   r1   rC   listrl   r   r%   r(   r&   <module>r      sC    	  6 6    / $ V V??24 &"NtCJ  

#c3h 
 6( "S4Z "DcNT4I "(K^ KC$J KSW K,3  > 2.? .tDaee,<'= .b4: 4: 4:r(   