
    o}g                         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mZ d dlmZ ded	ed
efdZdeded
efdZded
e
j        fdZ	 	 	 ddedededededededed
e
j        fdZdS )    )date	timedelta)partial)sleep)
monthrangeN)ResponseError)TrendReqyearmonthreturnc                 L    t          | |t          | |          d                   S )zGiven a year and a month returns an instance of the date class
    containing the last day of the corresponding month.

    Source: https://stackoverflow.com/questions/42950/get-last-day-of-the-month-in-python
       )r   r   )r
   r   s     R/var/www/py-google-trends/myenv/lib/python3.11/site-packages/pytrends/dailydata.pyget_last_date_of_monthr      s%     eZe44Q7888    startstopc                 \    |                      d           d|                     d           S )zGiven two dates, returns a stringified version of the interval between
    the two dates which is used to retrieve data for a specific time frame
    from Google Trends.
    z%Y-%m-%d )strftime)r   r   s     r   convert_dates_to_timeframer      s0    
 nnZ((FF4==+D+DFFFr   	timeframec                 B   d\  }}|s	  ||           d}ns# t           $ rf}t          |           t          ddd|z  z    d           t          dd|z  z              |dz  }|d	k    rt          d
           Y d}~nY d}~nd}~ww xY w||                                 S )z>Attempts to fecth data and retries in case of a ResponseError.)r   F)r   TzTrying again in <      z	 seconds.r      z'Failed after 3 attemps, abort fetching.N)r   printr   interest_over_time)pytrendsbuild_payloadr   attemptsfetchederrs         r   _fetch_datar$      s     Hg 	MI.... GG  	 	 	#JJJAR!h,%6AAABBB"q8|#$$$MH!||?@@@ ||||	   &&(((s    
BABBUST      @word
start_year	start_mon	stop_yearstop_mongeoverbose	wait_timec                    t          ||d          }t          ||          }	t          dd          }
t          |
j        | gd|d          }t          |
|t          ||	                    }i }|}||	k     r|t          |j        |j                  }t          ||          }|rt          |  d|            t          |
||          ||<   |t          d	          z   }t          |           ||	k     |t          j        |                                                              d
g          }|                    |dd          }||  d                             d           ||  d         dz  |d<   ||  d         |j        z  || <   |S )a  Given a word, fetches daily search volume data from Google Trends and
    returns results in a pandas DataFrame.

    Details: Due to the way Google Trends scales and returns data, special
    care needs to be taken to make the daily data comparable over different
    months. To do that, we download daily data on a month by month basis,
    and also monthly data. The monthly data is downloaded in one go, so that
    the monthly values are comparable amongst themselves and can be used to
    scale the daily data. The daily data is scaled by multiplying the daily
    value by the monthly search volume divided by 100.
    For a more detailed explanation see http://bit.ly/trendsscaling

    Args:
        word (str): Word to fetch daily data for.
        start_year (int): the start year
        start_mon (int): start 1st day of the month
        stop_year (int): the end year
        stop_mon (int): end at the last day of the month
        geo (str): geolocation
        verbose (bool): If True, then prints the word and current time frame
            we are fecthing the data for.

    Returns:
        complete (pd.DataFrame): Contains 4 columns.
            The column named after the word argument contains the daily search
            volume already scaled and comparable through time.
            The column f'{word}_unscaled' is the original daily data fetched
            month by month, and it is not comparable across different months
            (but is comparable within a month).
            The column f'{word}_monthly' contains the original monthly data
            fetched at once. The values in this column have been backfilled
            so that there are no NaN present.
            The column 'scale' contains the scale used to obtain the scaled
            daily data.
    r   zen-USih  )hltzr    )kw_listcatr,   gprop:)days	isPartial)columns	_unscaled_monthly)lsuffixrsuffixT)inplaced   scale)r   r   r	   r   r    r$   r   r
   r   r   r   r   pdconcatvaluesdropjoinffillr@   )r'   r(   r)   r*   r+   r,   r-   r.   
start_date	stop_dater   r    monthlyresultscurrentlast_date_of_monthr   dailycompletes                      r   get_daily_datarO   0   s   Z j)Q//J&y(;;I 7s+++HH2%)Fs"F F FM (M3J	JJL LG GG
I

3GL'-PP.w8JKK	 	)T''I''(((&x	JJ$ya'8'8'88i I

 Ignn&&'',,k],CCEzz';
zKKH %%d%333 D!2!2!23c9HW0001HNBHTNOr   )r%   Tr&   )datetimer   r   	functoolsr   timer   calendarr   pandasrA   pytrends.exceptionsr   pytrends.requestr	   intr   strr   	DataFramer$   boolfloatrO    r   r   <module>r]      s   $ $ $ $ $ $ $ $                       - - - - - - % % % % % %9 9S 9T 9 9 9 9Gd G$ G3 G G G G)C )BL ) ) ) )0 !!%$'O O O OO  O 	O
 O O "O -/LO O O O O Or   