
    Hi,                         S SK r  S SKrS SKrS SKrS SKrS SKrS SKJrJr  S SKJ	r	  Sr
Sr " S S5      rS r\S	:X  a  \" 5         gg)
    N)datetime	timedelta)Path/Eden/DATA/sales.dbz"/Eden/DATA/enterprise_pipeline.logc                       \ rS rSrSrS rS rS rS\4S jr	SS\
4S jjrS	\S\4S
 jrS\S\4S jrSS\4S jjrS\4S jrS rSS jrSrg)EnterprisePipeline   z
High-throughput sales pipeline
- Processes high-value leads first
- Intelligent batching
- Platform-aware rate limiting
- Automatic followups
c                     [         R                  " [        5      U l        SU l        SU l        SU l        [         R                  " S5      U l        g )Ni  
   <   r   )sqlite3connectDB_PATHconndaily_limit
batch_sizebatch_delaydb)selfs    5/home/james-whalen/RECOVERED/EDEN_CODE/f1531703944.py__init__EnterprisePipeline.__init__   s:    OOG,	//"78    c                 f   ^ U(       d  gUR                  5       m/ SQn[        U4S jU 5       5      $ )zFilter out bot accountsF) z[bot]z-botzbot-_botbot_zgithub-actions
dependabotrenovatez
gitlab-botcodecovsnykmergifyimgbotgreenkeeperallcontributorszmend-
sonarcloudstalezsemantic-releaserenovatebotz
pre-commitnetlifyvercelzazure-pipelinescirclecitravisjenkinsdrone	buildkitenoreplyzno-reply
donotreplyc              3   ,   >#    U  H	  oT;   v   M     g 7f)N ).0patternusername_lowers     r   	<genexpr>,EnterprisePipeline.is_bot.<locals>.<genexpr>4   s     ILn,Ls   )lowerany)r   usernamebot_patternsr5   s      @r   is_botEnterprisePipeline.is_bot%   s/    !)	
 ILIIIr   c                     [         R                  " 5       R                  S5      nSU SU 3n[        U5        [	        [
        S5       nUR                  US-   5        S S S 5        g ! , (       d  f       g = f)Nz%H:%M:%S[z] a
)r   nowstrftimeprintopenLOG_PATHwrite)r   msg	timestamplinefs        r   logEnterprisePipeline.log6   sY    LLN++J7	9+Ru%d(C AGGD4K  !  s   A&&
A4returnc                     [         R                  " 5       R                  S5      nU R                  R	                  SU45      R                  5       S   nU$ )zHow many sent today?%Y-%m-%dz;SELECT COUNT(*) FROM outreach_queue WHERE date(sent_at) = ?r   )r   rB   rC   r   executefetchone)r   todaycounts      r   get_today_sent_count'EnterprisePipeline.get_today_sent_count=   sH    ''
3		!!IE8

(*Q r   c           	         U R                   R                  SU45      R                  5       n/ nU HL  n US   (       a  [        R                  " US   5      O0 nUR                  US   US   US   UUS   S.5        MN     U$ !    MW  = f)z,Get leads sorted by value, not yet contacteda  
            SELECT l.id, l.identifier, l.source, l.data, l.score
            FROM leads l
            WHERE l.status = 'new' 
            AND l.score >= 0.6  -- Higher threshold with real-time scoring
            AND l.identifier NOT IN (
                SELECT lead_id FROM outreach_queue WHERE lead_id IS NOT NULL
            )
            ORDER BY l.score DESC
            LIMIT ?
           r            )id
identifiersourcedatascore)r   rQ   fetchalljsonloadsappend)r   limitrowsleadsrowr_   s         r   get_high_value_leads'EnterprisePipeline.get_high_value_leadsE   s    yy   
" X
  xz 	 C
-0Vtzz#a&)a&"%a&!!f  V   s   ABBleadc           
         UR                  S0 5      n[        U[        5      (       a   [        R                  " U5      nUR                  SUR                  SS5      5      nUR                  SUR                  SS5      5      nS[        U5      ;   a  UR                  S5      S	   O
[        U5      nUR                  S
UR                  SUR                  SS5      5      5      nUR                  SS5      n U R                  R                  S5      nUR                  5       n	U	(       a  U	u  pnUR                  S[        U5      5      R                  S[        U5      5      nUR                  S[        U5      5      R                  S[        U5      5      nUS:  d  U
S:X  a  Su  nnOUS:  a  Su  nnOSu  nnU R                  SU
 SU 35        OSU 3nSU SU S3nSu  nn UR                  S 5      UUUUUUS!.$ !   0 n GN= f! [         a1  nU R                  SU 35        SU 3nSU SU S3nSu  nn SnANYSnAff = f)"z5Generate personalized outreach message from templatesr_   userauthorthererepor]   zyour project/urlhtml_url r`         ?zLSELECT type, subject, body FROM outreach_templates ORDER BY RANDOM() LIMIT 1z{repo_name}z
{username}g?enterprise_pitch)sage_enterpriseg     8@gffffff?)sage_professionalg     0@)sage_starterg     b@u   📝 Template: z for zSecurity review for zHi z(,

I noticed potential improvements for z.

Best,
Jamesu   ⚠️ Template error: Nr\   )lead_idrm   rs   subjectmessageproductprice)get
isinstancestrrb   rc   splitr   rQ   rR   replacerL   	Exception)r   rk   r_   rm   rp   	repo_namers   r`   cursortemplatetemplate_typesubject_templatebody_templater|   r}   r~   r   es                     r   generate_outreach$EnterprisePipeline.generate_outreachb   sF   xx#dC  zz$' xx7 ;<xx~ FG+.#d)+;DJJsOB'T	hhudhhz488L"3MNO#&	9WW__%stF(HAI>*22=#i.QYYZfhklphqr'//s9~NVVWcehimenoC<=4F#F%>NGUc\%@NGU%;NGU?=/tfEF0<v%OPY{Zkl!< xx~
 	
M>  	9HH.qc23,YK8GD6!KI;VghG8NGU		9s*   G= 6CH H =H
I'H==Ioutreachc                      [         R                  " US    S[        R                  " 5       R	                  5        3R                  5       5      R                  5       SS R                  5       n[        R                  " 5       R	                  SSS9nU R                  R                  SUS   UR                  S	S
5      UR                  SS
5      UR                  SS
5      UR                  SS
5      UR                  SS
5      [        UR                  SS5      =(       d    S5      U45        U R                  R                  SX1S   45        U R                  R                  5         g! [         a  nU R                  SU 35         SnAgSnAff = f)z&Add to outreach queue and mark as sentr{   _N    seconds)septimespecz
                INSERT INTO outreach_queue
                (lead_id, user, issue_url, subject, message, product, price, status, created_at, sent_at, followup_count)
                VALUES (?, ?, ?, ?, ?, ?, ?, 'queued', ?, NULL, 0)
            rm   ru   rs   r|   r}   r~   r   r   z<UPDATE leads SET status='contacted', updated_at=? WHERE id=?TzError queuing: F)hashlibmd5r   rB   	isoformatencode	hexdigestupperr   rQ   r   floatcommitr   rL   )r   r   outreach_idrB   r   s        r   queue_outreach!EnterprisePipeline.queue_outreach   s]   	!++I&'q)A)A)C(DELLNik#2$uw  ,,.**sY*GCII 
 #VR(UB'Y+Y+Y+hll7A.3!4		 IINy)*
 II 	HHqc*+	s   E"E% %
F/FFc                 ~   U R                  5       nU R                  U-
  nUS::  a   U R                  SU R                   S35        g[        X5      nU R	                  U5      nU(       d  U R                  S5        gSnU H  nU R                  U5      nU R                  UR                  SS5      5      (       a&  U R                  SUR                  SS5       35        M`  U R                  U5      (       d  Mx  U R                  S	US    S
US    SUS   SS  S35        US-  n[        R                  " S5        M     U$ )z#Process a batch of high-value leadsr   zDaily limit reached (z). Waiting for tomorrow.zNo more leads to processrm   ru   u   🤖 Skipped bot: unknownu   ✅ Queued: z | $r   z | r|   N   z...rY   rv   )rU   r   rL   minri   r   r<   r   r   timesleep)	r   r   
sent_today	remainingactual_batchrg   	processedrk   r   s	            r   process_batch EnterprisePipeline.process_batch   sB    ..0
$$z1	>HH,T-=-=,>>VWX :1)),7HH/0	D--d3H{{8<<344-hll69.M-NOP""8,,<(8'9hw>O=PPST\]fTghkikTlSmmpqrQ	

3  r   c                    [         R                  " 5       [        SS9-
  R                  S5      n[         R                  " 5       [        SS9-
  R                  S5      nU R                  R                  SX45      R                  5       nSnU H_  nUu  pgppU R                  R                  SU45        U R                  R                  5         U R                  SUS	-    S
U 35        US	-  nMa     U$ )z.Send followups to leads that haven't respondedrZ   )daysrP      a  
            SELECT id, user, subject, message, price, followup_count
            FROM outreach_queue
            WHERE status = 'sent'
            AND date(sent_at) <= ?
            AND date(sent_at) >= ?
            AND followup_count < 2
            LIMIT 20
        r   zJUPDATE outreach_queue SET followup_count = followup_count + 1 WHERE id = ?u   📧 Followup #rY   z: )	r   rB   r   rC   r   rQ   ra   r   rL   )r   cutoff
old_cutoffrf   	followupsrh   r   rm   r|   r}   r   rT   s               r   process_followups$EnterprisePipeline.process_followups   s     ,,.9!#44>>zJllnya'88BB:N
yy   " !# $,8: 	 	C@C=Kw II\ IIHHuQwir$89NI  r   c                    U R                  S5        U R                  S5        U R                  S5        U R                  5       n[        U R                  S5      5      nU R                  SU SU R                   35        U R                  SU 35        U R                  U R                  5      nU R                  SU S35        U R                  5       nU R                  S	U 35        X4-   $ )
zRun one complete pipeline cyclez2==================================================u   🚀 ENTERPRISE PIPELINE CYCLEi  u   📊 Sent today: rq   u   📊 High-value leads waiting: u   ✅ Processed: z
 new leadsu   📧 Followups sent: )rL   rU   lenri   r   r   r   r   )r   r   pending_highr   r   s        r   	run_cycleEnterprisePipeline.run_cycle   s    12 ..0
444T:;$ZL$2B2B1CDE2<.AB &&t7	?9+Z89 **,	(45$$r   c                    SU-  nU R                  S5        U R                  SU R                   35        U R                  SU R                   35        U R                  SU 35          U R                  5         U R                  S[        US	-  5       S
35        [        R                  " U5        MJ  ! [         a  nU R                  SU 35         SnAN\SnAff = f)z#Run continuously with proper pacingi  u    🏢 Enterprise Pipeline Startedz   Batch size: z   Daily limit: z   Cycles/hour: u   ❌ Cycle error: Nu   💤 Sleeping r   z minutes...)rL   r   r   r   r   intr   r   )r   cycles_per_hourintervalr   s       r   run_continuous!EnterprisePipeline.run_continuous  s    /)35?4??"345#D$4$4#567#O#4562  HH~c(2+&6%7{CDJJx    2,QC0112s   )B2 2
C<CC)r   r   r   r   r   N)2   )r   )   )__name__
__module____qualname____firstlineno____doc__r   r<   rL   r   rU   listri   dictr   boolr   r   r   r   r   __static_attributes__r2   r   r   r   r      s{    9J"!c  :4
d 4
t 4
l!t ! !Fc @3 @%.!r   r   c                  v   SS K n U R                  5       nUR                  SSSS9  UR                  S[        SSS	9  UR                  S
[        SSS	9  UR	                  5       n[        5       nUR                  Ul        UR                  (       a  UR                  5         g UR                  UR                  5        g )Nr   z--once
store_truezRun one cycle)actionhelpz--batchr   z
Batch size)typedefaultr   z--cyclesr   zCycles per hour)argparseArgumentParseradd_argumentr   
parse_argsr   batchr   oncer   r   cycles)r   parserargspipelines       r   mainr   %  s    $$&F
OL
	RlK

a>OPD!#H**Hyy,r   __main__)eden_brain_injectr   rb   r   randomr   r   r   pathlibr   r   rF   r   r   r   r2   r   r   <module>r      sT          ( 
/M! M!`-" zF r   