
    	Ih                         d dl mZmZ d dlmZ d dlmZmZmZ d dl	m
Z
 d dl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 d d	lmZ d dlZd
ZdZddZ edg       eeg      d               Zy)    )api_viewpermission_classes)AllowAny)KeywordGroupsCompetitors)JsonResponseN)settings)ThreadPoolExecutoras_completed)datetime)date2f6d117df33msh3da512288c98c0cp17197ajsn973cb4ee4f5f   c           
         d}t         dd}| dd}t        d|dz         D ]M  }	 t        j                  |||d      }|j	                          |j                         j                  d	d
      c S  y
# t        j                  j                  $ r6}t        d| d|  d|        t        j                  d|z         Y d }~d }~wt        $ r}t        d| d|  d|        Y d }~ y
d }~ww xY w)Nz&https://ahrefs2.p.rapidapi.com/trafficzahrefs2.p.rapidapi.com)zx-rapidapi-keyzx-rapidapi-hostexact)urlmode   <   )headersparamstimeouttrafficMonthlyAvgr   z	[Attempt z] z	 failed:    z] Invalid JSON for : )RAPID_API_KEYrangerequestsgetraise_for_statusjson
exceptionsRequestExceptionprinttimesleep
ValueError)site_urlmax_attemptsr   r   r   attemptrespes           1/var/www/html/api/serp/keyword_traffic_tracker.pyget_traffic_monthly_avgr/   G   s    
2C'3G w/FL1,- 
		<<WVRPD!!#99;??#6::	
  ""33 	$IgYb
)A3?@JJq7{## 	IgY&9(2aSIJ	s$   AA11C*,B??C*C%%C*GETc                 f   	 t        t        j                  j                  d      j                  d            }d}g }t	        t
              5 }|D ci c]#  }|j                  t        |j                        |% }}t        |      D ]r  }||   }	 |j                         }t        j                  j                  |j                        j                  |       |d	z  }t        d
|j                   d|        t 	 ddd       t%        dd||d      S c c}w # t         $ rM}	t        d|j                   d|j                   d|	        |j#                  |j                         Y d}	~	d}	~	ww xY w# 1 sw Y   wxY w# t         $ r&}	t        d|	        t%        ddd      cY d}	~	S d}	~	ww xY w)z
    Cron endpoint: fetches traffic for every keyword with a ranked_url
    in parallel threads, then updates the `traffic` field.
    T)ranked_url__isnull )
ranked_urlr   )max_workers)id)trafficr   zUpdated r   z[Error] updating z (ID z): NtruezTraffic update completed)statusmessageupdated
failed_idsz[Cron Error] zGroup processing completed)r9   r:   )listr   objectsexcluder   TRAFFIC_THREAD_LIMITsubmitr/   r4   r   resultfilterr6   updater%   keyword	Exceptionappendr	   )
requestkeywordsupdated_countr<   executorkwfuture_to_kwfuturer7   r-   s
             r.   keywordTrafficrO   \   s   &ZOOGtG4GrG*
 
,@A 	-X #  7GKL 
 '|4 	-!&)-$mmoGOO**bee*4;;G;L!Q&MHRZZL7)<=	-	-$ 1$$	
  	! ! --bjj\ruugSLM%%bee,,-	- 	-2  Z	qc"	#;WXY	YZst   AF E5(D<E5A*D<E5?F E5	E2%AE-(E5-E22E55E>:F 	F0
F+%F0+F0)   )rest_framework.decoratorsr   r   rest_framework.permissionsr   serp.modelsr   r   r   django.httpr	   r   r"   osdjango.confr
   concurrent.futuresr   r   r   r   r&   r   r@   r/   rO        r.   <module>rZ      sl    B / 4 4 $   	   ?    E l* 
5'XJ+Z   +ZrY   