
    o}g`                     z    d dl 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
 d dlmZ dZ G d d	e          ZdS )
    N)HTTPAdapter)Retry)status_codes)
exceptions)quotez https://trends.google.com/trendsc                      e Zd ZdZdZdZe dZe dZe dZ	e dZ
e dZe d	Ze d
Ze dZe dZe dZe dZdZ	 	 d0dZd Zd ZedfdZ	 	 d1dZd Zd Zd Z	 	 d2d!Zd" Zd# Zd3d%Zd4d'Zd5d*Z d6d,Z!d- Z"d. Z#d/ Z$dS )7TrendReqz
    Google Trends API
    getpostz/api/explorez/api/widgetdata/multilinez/api/widgetdata/multirangez/api/widgetdata/comparedgeoz/api/widgetdata/relatedsearchesz"/hottrends/visualize/internal/dataz/api/topchartsz/api/autocomplete/z/api/explore/pickers/categoryz/api/dailytrendsz/api/realtimetrends)i  i  i  i  en-USh         r   Nc	                 <   d| _         d| _        || _        || _        || _        t                      | _        || _        || _        || _	        || _
        d| _        |pi | _        |                                 | _        t                      | _        t                      | _        t                      | _        t                      | _        t                      | _        d| j        i| _        | j                            | j                            di                      dS )z6
        Initialize default values for params
        z:You have reached your quota limit. Please try again later.Nr   zaccept-languageheaders)	google_rlresultstzhlgeolistkw_listtimeoutproxiesretriesbackoff_factorproxy_indexrequests_argsGetGoogleCookiecookiesdicttoken_payloadinterest_over_time_widgetinterest_by_region_widgetrelated_topics_widget_listrelated_queries_widget_listr   updatepop)	selfr   r   r   r   r   r   r   r    s	            P/var/www/py-google-trends/myenv/lib/python3.11/site-packages/pytrends/request.py__init__zTrendReq.__init__%   s     Vvv,*0b++--!VV)-&)-&*.&&'+/66()473D.229bAABBBBB    c                 "   	 d| j         v rp	 t          t          d t          j        t
           d| j        dd          fd| j        i| j         j        	                                                    S #  Y wxY wt          | j                  dk    rd	| j        | j                 i}nd
}	 t          t          d t          j        t
           d| j        dd          f| j        |d| j         j        	                                                    S # t          j        j        $ rf t          d           t          | j                  dk    r+| j                            | j        | j                            nt          d            Y w xY w)z
        Gets google cookie (used for each and every proxy; once on init otherwise)
        Removes proxy from the list on proxy error
        Tr   c                     | d         dk    S Nr   NID is    r,   <lambda>z*TrendReq.GetGoogleCookie.<locals>.<lambda>K       1 r.   z/explore/?geo=Nr   r   httpsr   c                     | d         dk    S r1   r3   r4   s    r,   r6   z*TrendReq.GetGoogleCookie.<locals>.<lambda>X   r7   r.   )r   r   zProxy error. Changing IP   zNo more proxies available. Bye!)r    r#   filterrequestsr
   BASE_TRENDS_URLr   r   r"   itemslenr   r   r   
ProxyErrorprintremove)r+   proxys     r,   r!   zTrendReq.GetGoogleCookieC   s   
	D...'>'>*HH$'"##,HHA A $A ,A A eegg	!' !' ( ( (
Ht|$$q(($dl43C&DEEEE'>'>*HH$'"##,HHA $ %A A ,	A A
 eegg!' !' ( ( (  *5   45554<((1,,++DL9I,JKKKK?@@@Hs    A'A4 4A8+A(D A6FFc                 x    | j         t          | j                  dz
  k     r| xj         dz  c_         dS d| _         dS )z9
        Increment proxy INDEX; zero on overflow
        r;   r   N)r   r@   r   )r+   s    r,   GetNewProxyzTrendReq.GetNewProxyg   sI     s4<001455! Dr.   c                 P   t          j                    }| j        dk    s| j        dk    rft	          | j        | j        | j        | j        t
          j        t          ddg                    }|                    dt          |                     |j
                            | j
                   t          | j                  dk    rE|                                 | _        |j                            d| j        | j                 i           |t
          j        k    r# |j        |f| j        | j        d|| j        }n" |j        |f| j        | j        d|| j        }|j        d	k    rd
|j
        d         v sd|j
        d         v sd|j
        d         v r7|j        |d         }|                                  t3          j        |          S |j        t6          j        j        k    rt<          j                             |          t<          j!                             |          )a  Send a request to Google and return the JSON response as a Python object
        :param url: the url to which the request will be sent
        :param method: the HTTP method ('get' or 'post')
        :param trim_chars: how many characters should be trimmed off the beginning of the content of the response
            before this is passed to the JSON parser
        :param kwargs: any extra key arguments passed to the request builder (usually query parameters or data)
        :return:
        r   GETPOST)totalreadconnectr   status_forcelistmethod_whitelistzhttps://)max_retriesr9   )r   r"      zapplication/jsonzContent-Typezapplication/javascriptztext/javascriptN)"r=   sessionr   r   r   r	   ERROR_CODES	frozensetmountr   r   r)   r@   r   r!   r"   r   POST_METHODr   r   r    r
   status_codetextrF   jsonloadsr   codestoo_many_requestsr   TooManyRequestsErrorfrom_responseResponseError)	r+   urlmethod
trim_charskwargssretryresponsecontents	            r,   	_get_datazTrendReq._get_datap   s<    <!t2Q664<"&,)-)<+3+?+4eV_+E+E	G G GE
 GGJ > > >???		&&&t|q  //11DLIgt|D4D'EFGGGX)))qvc 44<&*l4 46<4 $ 24 4HH quS =$, = =%=)-);= =H 3&&+= 0,1 ,1(H,<^,LLL!X%5n%EEE mJKK0G:g&&&#|'9'KKK 5CCHMMM*88BBBr.   	today 5-yc                 B   |dvrt          d          || _        |p| j        | _        | j        | j        g ||dd| _        t          |t                    rRt          | j                  D ]<\  }}|||         | j        d}| j        d         d         	                    |           =n;| j        D ]3}||| j        d}| j        d         d         	                    |           4t          j        | j        d                   | j        d<   |                                  dS )	zQCreate the payload for related queries, interest over time and interest by region)r   imagesnewsyoutubefrooglezHgprop must be empty (to indicate web), images, news, youtube, or froogle)comparisonItemcategoryproperty)r   r   req)keywordtimer   rq   rn   N)
ValueErrorr   r   r   r   r$   
isinstancer   	enumerateappendrX   dumps_tokens)	r+   r   cat	timeframer   gpropindexkwkeyword_payloads	            r,   build_payloadzTrendReq.build_payload   sN    DDDghhh?$(''&(cuMM
 
 i&& 	T&t|44 T T	r.0)E:JSWS["\"\"5)*:;BB?SSSST
 l T T.0)DH"U"U"5)*:;BB?SSSS %)Jt/A%/H$I$I5!r.   c                    |                      t          j        t          j        | j        d          d         }d}g | j        dd<   g | j        dd<   |D ]t}|d         dk    r|| _        |d         dk    r|r	|| _        d	}d
|d         v r| j        	                    |           d|d         v r| j        	                    |           udS )zhMakes request to Google to get API tokens for interest over time, interest by region and related queries   )r_   r`   paramsra   widgetsTNid
TIMESERIESGEO_MAPFRELATED_TOPICSRELATED_QUERIES)
rg   r	   GENERAL_URLrU   r$   r(   r'   r%   r&   rw   )r+   widget_dictsfirst_region_tokenwidgets       r,   ry   zTrendReq._tokens   s    ~~$'%	 & 
 

  " /1(+-/'*" 
	@ 
	@Fd||++17.d|y((-?(17.%*"6$<///66v>>> F4L00077???r.   c                    t          j        | j        d                   | j        d         | j        d}|                     t
          j        t
          j        d|          }t          j	        |d         d                   }|j
        r|S t          j        |d                             d	
          d          |d<   |                    dg                                          }|d                             d           }t!          | j                  D ]J\  }}|                    t'          |j                  |||                             d                     ||= Kd|v rb|                    d          }|d                             d           }dg|_        |j        dk    |_        t          j        ||gd          }n|}d|d<   |S )zLRequest data from Google's Interest Over Time section and return a dataframerequesttokenrq   r   r   r   r_   r`   ra   r   defaulttimelineDatars   float64)dtyperc   )unitdatevaluec                     t          j        t          |                               dd                              dd                              d                    S N[r   ],pdSeriesstrreplacesplitxs    r,   r6   z-TrendReq.interest_over_time.<locals>.<lambda>   I    	FFNN3##++C44::3??1A 1A r.   int	isPartialFc                     t          j        t          |                               dd                              dd                              d                    S r   r   r   s    r,   r6   z-TrendReq.interest_over_time.<locals>.<lambda>  sI    AsB''//R88>>sCC:E :E r.   Truer;   axis)rX   rx   r%   r   rg   r	   INTEREST_OVER_TIME_URL
GET_METHODr   	DataFrameemptyto_datetimeastype	set_index
sort_indexapplyrv   r   insertr@   columnsfillnar   concat)	r+   over_time_payloadreq_jsondf	result_dfidxr~   
result_df2finals	            r,   interest_over_timezTrendReq.interest_over_time   s   
 :d<YGHH3G<'	
 
 >>/&$	 " 
 
 \(9-n=>>H 	I^BvJ$5$5I$5$F$F),. . .6
\\6(##..00wK%% 'A 'A B B	 !.. 	 	GC S!233R&s^22599; ; ;#" 5!!BK.. 0E 0E F FJ"-J#-#76#AJ Iy*5A>>>EEE!&E+r.   c                    t          j        | j        d                   | j        d         | j        d}|                     t
          j        t
          j        d|          }t          j	        |d         d                   }|j
        r|S t          j        |d                   }t          |j                  D ]\  }}||                             t          j                  d	         |d
t!          |          z   dz   t!          | j        |                   z   dz   <   ||                             t          j                  d         |d
t!          |          z   dz   t!          | j        |                   z   dz   <   |                    |gd          }i }t          |d         d                   D ]\  }}d|d
t!          |          z   dz   t!          | j        |                   z   dz   <   |d         d         |         |d
t!          |          z   dz   t!          | j        |                   z   dz   <   ||j        d<   |j        dz   |_        |                                }|S )ziRequest data from Google's Interest Over Time section across different time ranges and return a dataframer   r   r   r   r   r   r   
columnDataformattedTimer   z] z dater   z valuer;   r   averagesAverage)rX   rx   r%   r   rg   r	   !MULTIRANGE_INTEREST_OVER_TIME_URLr   r   r   r   json_normalizerv   r   r   r   r   r   droplocr}   r   )	r+   r   r   r   r   r5   columnavg_rowavgs	            r,   multirange_interest_over_timez&TrendReq.multirange_interest_over_time  sL   
 :d<YGHH3G<'	
 
 >>:&$	 " 
 
 \(9-n=>>H 	I%b&677	 #9#455 	4 	4IAvNWXYlN`N`acajNkNkl{N|IcCFFlT)CQ,@,@@7JKOXYZ|OaOabdbkOlOlmtOuIcCFFlT)CQ,@,@@8KL!s33II  3J ?@@ 	p 	pFAsLUGC#a&&L4'#dl1o*>*>>HIMUV_M`akMlmnMoGC#a&&L4'#dl1o*>*>>IJJ#	b#/A-	((**	r.   COUNTRYFc                 V   t                      }| j        dk    r|| j        d         d<   n| j        dk    r|dv r|| j        d         d<   || j        d         d<   t          j        | j        d                   |d<   | j        d         |d<   | j        |d	<   |                     t          j        t          j	        d
|          }t          j        |d         d                   }|j        r|S d|j        v rdnd}d|dg}||                             dg                                          }|d                             d           }	|r$||j        v r||         |	|<   nt#          d           t%          | j                  D ]&\  }
}|	|
                             d          |	|<   |	|
= '|	S )zLRequest data from Google's Interest by Region section and return a dataframer   r   
resolutionUS)DMACITYREGIONincludeLowSearchVolumeGeosrq   r   r   r   r   r   
geoMapDatageoCodecoordinatesgeoNamer   c                     t          j        t          |                               dd                              dd                              d                    S r   r   r   s    r,   r6   z-TrendReq.interest_by_region.<locals>.<lambda>c  r   r.   z(Could not find geo_code column; Skippingr   )r#   r   r&   rX   rx   r   rg   r	   INTEREST_BY_REGION_URLr   r   r   r   r   r   r   r   rB   rv   r   r   )r+   r   inc_low_volinc_geo_coderegion_payloadr   r   
geo_columnr   r   r   r~   s               r,   interest_by_regionzTrendReq.interest_by_region=  s   
 8r>> * *95 X*0I"I"I * *95 -8 	&y1(	* !%
*95!7 !7u"&"@"Iw#wt >>/&!	 " 
 
 \(9-l;<<H 	I #,rz"9"9YY}
j'2[""I;//::<<wK%% 'A 'A B B	 	BRZ''(*:	*%%@AAA !.. 	 	GC%cN11%88IbM#r.   c                    t                      }t                      }| j        D ],}	 |d         d         d         d         d         d         }n# t          $ r d}Y nw xY wt          j        |d                   |d<   |d	         |d	<   | j        |d
<   |                     t          j        t          j	        d|          }	 |d         d         d         d         }t          j        |d          }n# t          $ r d}Y nw xY w	 |d         d         d         d         }t          j        |d          }	n# t          $ r d}	Y nw xY w|	|d||<   .|S )zRequest data from Google's Related Topics section and return a dictionary of dataframes

        If no top and/or rising related topics are found, the value for the key "top" and/or "rising" will be None
        r   restrictioncomplexKeywordsRestrictionrr   r   r   r   rq   r   r   r   r   r   
rankedListrankedKeyword_)sepNr;   )risingtop)r#   r'   KeyErrorrX   rx   r   rg   r	   RELATED_QUERIES_URLr   r   r   )
r+   related_payloadresult_dictrequest_jsonr~   r   top_listdf_toprising_list	df_risings
             r,   related_topicszTrendReq.related_topicsr  s    &&ff ; $	C $	CL!),];022;==>@@GI    &*ZY0G%H%HOE"'3G'<OG$$(GOD! ~~0*&	 &  H#I.|<Q?P*8===   
!&y1,?B?S-ksCCC		 ! ! ! 			! *36BBKOOs5   &AAA0C22D D0D66EEc                    t                      }t                      }| j        D ]8}	 |d         d         d         d         d         d         }n# t          $ r d}Y nw xY wt          j        |d                   |d<   |d	         |d	<   | j        |d
<   |                     t          j        t          j	        d|          }	 t          j        |d         d         d         d                   }|ddg         }n# t          $ r d}Y nw xY w	 t          j        |d         d         d         d                   }|ddg         }n# t          $ r d}Y nw xY w||d||<   :|S )zRequest data from Google's Related Queries section and return a dictionary of dataframes

        If no top and/or rising related queries are found, the value for the key "top" and/or "rising" will be None
        r   r   r   rr   r   r   r   rq   r   r   r   r   r   r   r   queryNr;   )r   r   )r#   r(   r   rX   rx   r   rg   r	   r   r   r   r   )r+   r   r   r   r~   r   top_df	rising_dfs           r,   related_querieszTrendReq.related_queries  s    &&ff < &	C &	CL!),];022;==>@@GI    &*ZY0G%H%HOE"'3G'<OG$$(GOD! ~~0*&	 &  HY'5a8IK K' 23   
!LY'5a8IK K	%w&89		 ! ! ! 			! '-	BBKOOs5   &AAA6C88DD6EEEunited_statesc                     |                      t          j        t          j                  |         }t	          j        |          }|S )zFRequest data from Google's Hot Searches section and return a dataframe)r_   r`   )rg   r	   TRENDING_SEARCHES_URLr   r   r   )r+   pnr   r   s       r,   trending_searcheszTrendReq.trending_searches  sI     >>.& " 
 
  L**	r.   r   c                     d|d| j         d} | j        dt          j        t          j        d|d| j        d         d         d         d	         }t          j        d
 |D                       }|j        dddf         S )zERequest data from Google Daily Trends section and returns a dataframe   z-180)nsr   r   r   r   r   r   trendingSearchesDaysr   trendingSearchesc              3   &   K   | ]}|d          V  dS )titleNr3   ).0trends     r,   	<genexpr>z*TrendReq.today_searches.<locals>.<genexpr>  s&       F FEw F F F F F Fr.   Nr   r3   )	r   rg   r	   TODAY_SEARCHES_URLr   r    r   r   iloc)r+   r   formsr   r   s        r,   today_searcheszTrendReq.today_searches  s    "F$'BB!4> 
+&	
 

  
 
  ,- ./0 1CD L F FX F F FFF	~aaae$$r.   all,  c                   
 d}||k     r|}d}||k     r|dz
  }d|d| j         |dd||dd
}|                     t          j        t          j        d	|
          d         d         }ddg

fd|D             }t          j        |          }	|	S )zORequest data from Google Realtime Search Trends section and returns a dataframer  rP   r;   r  3000r   )
r  r   r   r   rz   fifsrirssortr   r   storySummariestrendingStoriesentityNamesr  c                 T    g | ]#fd                                  D             $S )c                 *    i | ]}|v ||         S r3   r3   )r  keytswanted_keyss     r,   
<dictcomp>zBTrendReq.realtime_trending_searches.<locals>.<listcomp>.<dictcomp>  s)    PPPSK=O=ORW=O=O=Or.   )keys)r  r  r   s    @r,   
<listcomp>z7TrendReq.realtime_trending_searches.<locals>.<listcomp>  s;    eeeUWPPPPP		PPPeeer.   )r   rg   r	   REALTIME_TRENDING_SEARCHES_URLr   r   r   )r+   r   rz   countri_valuers_valuer  r   
final_jsonr   r   s             @r,   realtime_trending_searchesz#TrendReq.realtime_trending_searches  s     8H 8QwH"EUXadmu  G  RS  T  T>>7&	 " 
 

 
 ./ %g.eeee[ceee
L,,	r.   GLOBALc                 .   	 t          |          }n#  t          d          xY w||||dd}|                     t          j        t          j        d|          }	 t          j        |d         d         d                   }n# t          $ r d	}Y nw xY w|S )
zDRequest data from Google's Top Charts section and return a dataframezaThe date must be a year with format YYYY. See https://github.com/GeneralMills/pytrends/issues/355F)r   r   r   r   isMobiler   r   	topChartsr   	listItemsN)	r   rt   rg   r	   TOP_CHARTS_URLr   r   r   
IndexError)r+   r   r   r   r   chart_payloadr   r   s           r,   
top_chartszTrendReq.top_charts  s    	ut99DD	usu u u  "T#%*, , >>'& 	 " 
 
	h{3A6{CDDBB 	 	 	BBB		s    #&B BBc                     t          |          }d| j        i}|                     t          j        |z   |t          j        d          d         d         }|S )zNRequest data from Google's Keyword Suggestion dropdown and return a dictionaryr   r   r_   r   r`   ra   r   topics)r   r   rg   r	   SUGGESTIONS_URLr   )r+   rr   kw_param
parametersr   s        r,   suggestionszTrendReq.suggestions/  sd     >>DG_
>>(83&	 " 
 

 
  r.   c                 r    d| j         i}|                     t          j        |t          j        d          }|S )zKRequest available categories data from Google's API and return a dictionaryr   r   r4  )r   rg   r	   CATEGORIES_URLr   )r+   r   r   s      r,   
categorieszTrendReq.categories>  s@     >>'&	 " 
 
 r.   c                      t          d          )Na0  This method has been removed for incorrectness. It will be removed completely in v5.
If you'd like similar functionality, please try implementing it yourself and consider submitting a pull request to add it to pytrends.
          
There is discussion at:
https://github.com/GeneralMills/pytrends/pull/542)NotImplementedError)r+   argsrb   s      r,   get_historical_interestz TrendReq.get_historical_interestK  s    !5
 
 	
r.   )r   r   r   r   r   r   r   N)r   rh   r   r   )r   FF)r   )r   )r   r  r  )r   r  r*  )%__name__
__module____qualname____doc__r   rU   r>   r   r   r   r   r   r   r/  r6  r;  r  r$  rR   r-   r!   rF   rg   r   ry   r   r   r   r   r   r   r  r)  r2  r9  r<  r@  r3   r.   r,   r	   r	      s7         JK$222K /JJJ+:(V(V(V% /LLL,MMM.RRR'777N(<<<O'FFFN+===(7%L%L%L"&KKM<@C C C C<" " "H! ! ! %/1 0C 0C 0C 0Cd HJ   <  :3 3 3j( ( (V DI(-3 3 3 3j. . .`0 0 0d   % % % %$ $ $ $L   4    
 
 
 
 
r.   r	   )rX   pandasr   r=   requests.adaptersr   $requests.packages.urllib3.util.retryr   r   pytrendsr   urllib.parser   r>   objectr	   r3   r.   r,   <module>rK     s          ) ) ) ) ) ) 6 6 6 6 6 6 ! ! ! ! ! !             5@	
 @	
 @	
 @	
 @	
v @	
 @	
 @	
 @	
 @	
r.   