
    ǁi4                       d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm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mZmZ dd
lmZ  ej,                  e      Zi 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$d%d&d'd(Zdd)Zdd*Zdd+Zdd,Zdd-Zdd.Z	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd0Z  ejB                  d/1      dd2       Z"dd3Z#e"jI                         dd4       Ze"jI                          ejJ                  d5d6d/d78       ejJ                  d9d:d;<       ejJ                  d=d>d? ejL                  g d@      A       ejJ                  dBdC ejL                  g dD      dEF       ejJ                  dGdHe'dIF       ejJ                  dJdKdL<       ejJ                  dMdNd/dOdPQ       ejJ                  dRdSdT<       ejJ                  dUdVdW ejL                  g dX      A       ejJ                  dYdZdd[\       ejJ                  d]d^d_<       ejJ                  d`dad/db\      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddc                                                                                           Z(e"jI                          ejJ                  d5d6d/dd8       ejJ                  d9d:ded;\       ejJ                  d=d>d? ejL                  g d@      A       ejJ                  dBdC ejL                  g df      g       ejJ                  dhdi ejL                  g dj      g       ejJ                  dkdl ejL                  g dm      g       ejJ                  dndo ejL                  g dp      g       ejJ                  dqdr ejL                  g ds      g       ejJ                  dtdu ejL                  g dv      g       ejJ                  dGdHe'dIF       ejJ                  dJdKdL<       ejJ                  dMdNd/dOdPQ       ejJ                  dRdSdT<       ejJ                  dYdZdd[\       ejJ                  d]d^d_<       ejJ                  d`dad/db\      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddw                                                                                                                       Z)e"jI                          ejJ                  d5d6d/dd8       ejJ                  d9d:ded;\       ejJ                  d=d>d? ejL                  g d@      A       ejJ                  dBdC ejL                  g dx      dyF       ejJ                  dzd{ ejL                  d|d}g      g       ejJ                  dMd~ ejL                  g d      g       ejJ                  dtd ejL                  ddg      g       ejJ                  dGdHe'dIF       ejJ                  dJdKdL<       ejJ                  d]d^d_<       ejJ                  d`dad/db\      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd                                                                                    Z*e"jI                          ejJ                  d5d6d/dd8       ejJ                  d9d:ded;\       ejJ                  d=d>d? ejL                  g d@      A       ejJ                  dBdC ejL                  g dD      dEF       ejJ                  dGdHe'dIF       ejJ                  dJdKdL<       ejJ                  d]d^d_<       ejJ                  d`dad/db\      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd                                                               Z+edk(  r
 e"d       yy)    )annotationsN)ThreadPoolExecutoras_completed)datetime)Path)unquote   )DDGS)_expand_proxy_tb_alias
json_dumps)__version__blackred   green   yellow   blue   magenta   cyan   bright_black   
bright_red	   bright_green
   bright_yellow   bright_blue   bright_magenta   bright_cyan   white   bright_whitec                    |r#|j                  d      r|j                  dd      nd |f\  }}|r|n| d|  dt        j                         d}|dk(  rt	        | d| |       y |dk(  rt        | d| |       y y )N)z.csvz.json.r	   _%Y%m%d_%H%M%Scsvjson)endswithrsplitr   now	_save_csv
_save_json)keywordsdatafunction_namefilenameexts        _/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/duckduckgo_search/cli.py
_save_datar=   (   s    /7H<M<MN_<`HOOC+gkmufvMHc#xM?!H:Qx||~VcFd)eH
e|XJau%t,	hZq&- 
    c                ~    t        | dd      5 }|j                  t        |             d d d        y # 1 sw Y   y xY w)Nwutf-8)encoding)openwriter   )jsonfiler8   files      r<   r6   r6   1   s6    	hg	. %$

:d#$% % %s   3<c                   t        | ddd      5 }|rZ|d   j                         }t        j                  ||t        j                        }|j                          |j                  |       d d d        y # 1 sw Y   y xY w)Nr@    rA   )newlinerB   r   )
fieldnamesquoting)rC   keysr0   
DictWriterQUOTE_MINIMALwriteheader	writerows)csvfiler8   rF   headerswriters        r<   r5   r5   6   sk    	gsB	9 #T1gllnG^^DWcFWFWXF T"# # #s   AA66A?c           	     z   | rt        | d      D ]  \  }}t        j                  | dd dd       t        |j                         d      D ]^  \  }\  }}|r-|dv rd	nd
}|dk(  rdn|}t        j                  | |ddd      }n|}t        j                  |d| dt
        |   d       ` t                 y y )Nr	   startz.	    zN==============================================================================r   r)   )bgfg)contenthrefimagesource	thumbnailurli,  N   detected_languagelanguagerH   z            T)widthinitial_indentsubsequent_indentpreserve_paragraphsz<12)rW   rX   overline)	enumerateclicksechoitems	wrap_textCOLORSinput)r8   iejkvrb   texts           r<   _print_datart   ?   s    d!, 	DAqKK1#WXJ/GH&qwwy: 	W	6Aq#$(b#bChjE&'+>&>
AA ??#eBRZptD DqgdV,VAYQUV	W G	 r>   c                
   | j                  dd      j                  dd      j                  dd      j                  dd      j                  dd      j                  d	d      j                  d
d      j                  dd      } | S )NfiletyperH   :"'site r.   /\)replace)r7   s    r<   _sanitize_keywordsr   P   sr    R(	b		c				c		c		s		b	  Or>   c           	        	 t        j                  |ddd|      j                  |       }|j                  dk(  rRt	        t
        j                  j                  ||d d       d      5 }|j                  |j                         d d d        y y # 1 sw Y   y xY w# t        $ r;}t        j                  d|  dt        |      j                   d|        Y d }~y d }~ww xY w)Nrandomr    )proxyimpersonateimpersonate_ostimeoutverify   wbzdownload_file url=r{   )primpClientgetstatus_coderC   ospathjoinrD   rY   	Exceptionloggerdebugtype__name__)r^   dir_pathr:   r   r   resprF   exs           r<   _download_filer   ^   s    J||%Xh`bkqrvv
 s"bggll8Xds^<dC )t

4<<() ) #) ) J)#aR0A0A/B!B4HIIJs6   A%B 'BB BB B 	C"1CCTc           
        |r|n| d|  dt        j                         d}t        j                  |d       |dn|}t	        |      5 }g }	t        |d      D ]s  \  }
}|d	k(  r|d
   n|d   }t        |j                  d      d   j                  d      d         }|j                  t        |||
 d| ||      }|	j                  |       u t        j                  t        |	      dddd      5 }t        |	      D ]#  }|j                          |j!                  d       % 	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr.   r/   T)exist_okr    )max_workersr	   rU   imagesr[   rZ   r|   ?r   Downloading2   )lengthlabelshow_percentshow_posrb   )r   r4   r   makedirsr   rg   r   splitsubmitr   appendrh   progressbarlenr   resultupdate)r7   resultsr9   r   threadsr   pathnamer   executorfuturesrn   resr^   r:   fbarfutures                    r<   _download_resultsr   j   sR     8az8<<>R_B`%aDKKt$ObG		0 Hq1 	FAs"/8";#g,VCsyy~b177<Q?@HTaS(;LeU[\ANN1		 w<}4RV^`
 	&w/ 

1	 	 	 s%   B)E42D8'E8E	=EE)chainc                      y)zduckduckgo_search CLI toolN r   r>   r<   clir      s     	r>   c                     	 t                y # t        $ r7} t        j                  t	        |       j
                   d|         Y d } ~ y d } ~ ww xY w)Nz: )r   r   rh   echor   r   )r   s    r<   safe_entry_pointr      sA    1 1

d2h''(2$/001s   
 	A-AAc                 ,    t        t               t        S )N)printr   r   r>   r<   versionr      s    	+r>   z-kz
--keywordsztext search, keywords for query)requiredhelpz-rz--regionz8us-en, ru-ru, etc. -region https://duckduckgo.com/params)r   z-sz--safesearchmoderate)onr   off)defaultr   z-tz--timelimit)dr@   myzday, week, month, year)r   r   z-mz--max_resultszmaximum number of resultsz-oz--outputzGcsv, json or filename.csv|json (save the results to a csv or json file)z-dz
--downloadFz-download results. -dd to set custom directory)is_flagr   r   z-ddz--download-directoryz!Specify custom download directoryz-bz	--backendauto)r   htmllitez-thz	--threadszdownload threads, default=10)r   r   z-pz--proxyz<the proxy to send requests, example: socks5://127.0.0.1:9150z-vz--verifyz"verify SSL when making the requestc           	         t        t        |
      |      j                  | |||||	      }t        |       } |rt	        | |d|       |rt        | |d|
|||       |s|st        |       yyy)z;CLI function to perform a text search using DuckDuckGo API.r   r   )r7   region
safesearch	timelimitbackendmax_resultsrs   )r:   r9   r   r   r   r   N)r
   r   rs   r   r=   r   rt   )r7   r   r   r   r   outputdownloaddownload_directoryr   r   r   r   r8   s                r<   rs   rs      s    8 ,U3FCHH I D "(+H8T6F; '	
 (D #6r>   zkeywords for queryzus-en)DayWeekMonthYear)r   z-sizez--size)SmallMediumLarge	Wallpaperz-cz--color)color
MonochromeRedOrangeYellowGreenBluePurplePinkBrownBlackGrayTealWhitez-typez--type_image)photoclipartgiftransparentlinez-lz--layout)SquareTallWidez-licz--license_image)anyPublicShareShareCommerciallyModifyModifyCommerciallyc                    t        t        |      |      j                  | |||||||||
      }t        |       } |rt	        | |d|       |	rt        | |d||||
       |s|	st        |       yyy)z=CLI function to perform a images search using DuckDuckGo API.r   )
r7   r   r   r   sizer   
type_imagelayoutlicense_imager   r   r9   r:   r   N)r
   r   r   r   r=   r   rt   )r7   r   r   r   r   r   r   r   r  r   r   r   r   r   r   r   r8   s                    r<   r   r      s    z ,U3FCJJ# K D "(+H8TFK"'	
 (D #6r>   )r   r@   r   zday, week, monthz-resz--resolutionhighstandartz
--duration)shortmediumlongz--license_videoscreativeCommonyoutubec           
         t        t        |	      |
      j                  | |||||||      }t        |       } |rt	        | |d|       yt        |       y)z=CLI function to perform a videos search using DuckDuckGo API.r   )r7   r   r   r   
resolutiondurationlicense_videosr   videosr  N)r
   r   r  r   r=   rt   )r7   r   r   r   r  r  r  r   r   r   r   r8   s               r<   r  r  )  sd    4 ,U3FCJJ% K 	D "(+H8TFKDr>   c                    t        t        |      |      j                  | ||||      }t        |       } |rt	        | |d|       yt        |       y)z;CLI function to perform a news search using DuckDuckGo API.r   )r7   r   r   r   r   newsr  N)r
   r   r  r   r=   rt   )	r7   r   r   r   r   r   r   r   r8   s	            r<   r  r  T  sX    ( ,U3FCHH&Z9bm I D "(+H8T&IDr>   __main__ddgs)	prog_name)
r7   strr8   list[dict[str, str]]r9   r  r:   
str | NonereturnNone)rE   
str | Pathr8   r  r  r  )rQ   r  r8   r  r  r  )r8   r  r  r  )r7   r  r  r  )r^   r  r   r  r:   r  r   r  r   boolr  r  )NNTN)r7   r  r   r  r9   r  r   r  r   
int | Noner   r  r   r  r  r  )r  r  )r  r  )r7   r  r   r  r   r  r   r  r   r  r   r  r   r  r   r  r   intr   r  r   r  r   r  r  r  )"r7   r  r   r  r   r  r   r  r   r  r   r  r   r  r   r  r  r  r   r  r   r  r   r  r   r  r   r  r   r  r   r  r  r  )r7   r  r   r  r   r  r   r  r  r  r  r  r  r  r   r  r   r  r   r  r   r  r  r  )r7   r  r   r  r   r  r   r  r   r  r   r  r   r  r   r  r  r  ),
__future__r   r0   loggingr   concurrent.futuresr   r   r   pathlibr   urllib.parser   rh   r   duckduckgo_searchr
   utilsr   r   r   r   	getLoggerr   r   rl   r=   r6   r5   rt   r   r   r   groupr   r   commandoptionChoicer  rs   r   r  r  r   r>   r<   <module>r)     s	   " 
  	 ?       # 5  			8	$
w
u
 w
 x	

 v
 y
 v
 ~
 |
 ~
 
 
 
 
 
  !
(.%
#"	J  !  	
    
: 4	 	
1  
 dL46WXdJ%_`dNJ\U\\Jc=dedM5I(JQijdO#4OPdJ%nodL$Dste+2UVdKlellC[6\]e["3QRdI$bcdJ3WX%%% % 	%
 % % % #% % % % % 
% Y d S ^ W u p Q k f a Y %P dL46JKdJ6pqdNJ\U\\Jc=dedM5U(VWgxlell3\&]^		

, g~LELL9k,lmdJ\U\\2L%MN
	e	f
 dO#4OPdJ%nodL$Dste+2UVe["3QRdI$bcdJ3WX--- - 	-
 - - - - - - #- - - - -  !-" 
#- Y d S W u p Q O n- _ X f r L T-` dL46JKdJ6pqdNJ\U\\Jc=dedM_(EL^_fn<5<<8L+MNdL|u||4O'PQf(|u||=My<Y/Z[dO#4OPdJ%nodI$bcdJ3WX  	
        
 Y d p Q \ R O ` f r L > dL46JKdJ6pqdNJ\U\\Jc=dedM5I(JQijdO#4OPdJ%nodI$bcdJ3WX  	
     
 Y d p Q k f r L * z& r>   