
    i(                         d Z ddlZddlZddlZddlmZ ddlmZ  ee      j                  dz  Z	e	dz  Z
dZddZd	ed
ededefdZd Zedk(  r e        yy)z
Layer 4: Video Structure Generation

Takes the full Layer 3 analysis output and generates detailed, virality-optimized
video structures for each opportunity. This is a standalone enrichment step.

Usage: python3 generate_video_structures.py
    N)Path)datetimeoutputzanalysis_opportunities.jsona  You are a YouTube virality strategist who has studied thousands of successful explainer videos. You understand retention curves, pattern interrupts, payoff timing, and what makes viewers share content.

You are given the full analysis of a video opportunity. Your ONLY job is to create the optimal video structure - the exact blueprint for how this video should flow from first frame to last.

## THE OPPORTUNITY

**Title:** {title}
**Theme:** {theme}
**The Winning Angle:** {missing_angle}

**Target Audience - The Moment:**
{the_moment}

**What They Know:** {what_they_know}
**What They Think Is Wrong:** {what_is_wrong}
**Click Trigger:** {click_trigger}

**The Core Confusion:**
Primary Question: {primary_question}
Secondary Questions: {secondary_questions}

**Existing Content & Why It Fails:**
{existing_content}

**Opening Hook (from initial analysis):**
{opening_hook}

**Critical Success Factor:**
{critical_success_factor}

**Verdict:** {verdict}
**Reasoning:** {verdict_reasoning}

---

## YOUR TASK

Create a detailed video structure optimized for:
1. **Retention** - Viewers must stay through the whole video
2. **Satisfaction** - The payoff must match or exceed the promise
3. **Shareability** - Include moments that make people want to send this to someone

## STRUCTURE REQUIREMENTS

Create exactly 5-7 sections. For EACH section, provide:

1. **Section Title** - What this part is called (for your reference, not shown to viewer)
2. **Duration** - Exact timing (e.g., "0:00-0:45" or "45 seconds")
3. **Purpose** - What this section accomplishes for retention/engagement
4. **Content** - Exactly what happens in this section, what you say, what you show
5. **The Hook to Next** - How this section creates anticipation for what's coming

## STRUCTURAL PRINCIPLES TO FOLLOW

- **First 30 seconds**: Must reframe the viewer's understanding. Don't just state the topic - create a gap between what they thought and what's true.
- **2-3 minute mark**: This is where most viewers drop off. Place a "holy shit" moment here - a counterintuitive fact, a surprising connection, or a demonstration.
- **Pattern interrupts**: Every 60-90 seconds, change something - pacing, visuals, tone, or introduce a new thread.
- **The payoff must land**: The main insight should feel earned, not given away too early.
- **End with implications**: Don't just conclude - show why this matters beyond the immediate topic.

## RESPOND IN THIS EXACT JSON FORMAT

{{
  "video_structure": {{
    "total_duration": "X:XX - X:XX minutes",
    "retention_strategy": "One sentence on the core retention approach",
    "sections": [
      {{
        "section_number": 1,
        "title": "Section Name",
        "timestamp": "0:00-0:45",
        "duration_seconds": 45,
        "purpose": "What this accomplishes",
        "content": "Detailed description of what happens",
        "visuals": "What the viewer sees",
        "hook_to_next": "How this creates anticipation"
      }}
    ],
    "key_retention_moments": [
      {{
        "timestamp": "2:15",
        "description": "The moment and why it keeps viewers watching"
      }}
    ],
    "share_triggers": [
      "Specific moment or line that makes viewers want to share"
    ]
  }}
}}

Generate the structure now. Be specific. This is a production blueprint, not a vague outline.c                    | |S t        | t              r| S t        | t              rdD ]  }|| v st        | |   |      c S  g }| j	                         D ]B  \  }}|j                  |j                  dd      j                          dt        |              D dj                  |      S t        | t              rdj                  d | dd	 D              S t        |       S )
z)Safely extract text from various formats.N)textvaluecontentdescriptionprimarymain_ z: z | ; c              3   2   K   | ]  }t        |        y wNextract_text).0items     C/home/sietch6/trending-topics-pipeline/generate_video_structures.py	<genexpr>zextract_text.<locals>.<genexpr>   s     @d+@      )

isinstancestrdictr   itemsappendreplacetitlejoinlist)valdefaultkeypartskvs         r   r   r   q   s    
{#s
#tQ 	7Ccz#CHg66	7 IIK 	NDAqLLAIIc3/5578<?:KLM	Nzz%  #tyy@BQ@@@s8O    oppindextotalreturnc                 R   | j                  d| j                  dd            }t        d|dz    d| d|dd	  d
d       t        | j                  dd            }t        | j                  d| j                  dd                  }| j                  di       }t        |t              rd|i}t        |j                  d|j                  dd                  }t        |j                  dd            }t        |j                  d|j                  dd                  }	t        |j                  dd            }
| j                  di       }t        |t              rd|i}t        |j                  dd            }|j                  dg       }t        |t
              rdj                  d |dd D              }nt        |      }t        | j                  dd            }t        | j                  dd            }t        | j                  d d            }| j                  d!d      }t        | j                  d"d            }t        j                  ||||||	|
|||dd# ||||dd$ %      }	 d&dl	}d&dl
}d'| d(}t        |d)      5 }|j                  |       ddd       t        j                  d*| d+dddd,-      }|j                  |       |j                   d&k7  rt        d.d       y|j"                  j%                         }|j'                  d/      }|j)                  d0      dz   }|d1k(  s|d&k(  rt        d2d       y||| }t+        j,                  |      }d3|v r|d3   }n|}t/        |j                  d4g             } |j                  d5d6      }!t        d7|  d8|! d9d       |S # 1 sw Y   xY w# t        j0                  $ r t        d:d       Y yt*        j2                  $ r}"t        d;|" d       Y d}"~"yd}"~"wt4        $ r}"t        d<|" d       Y d}"~"yd}"~"ww xY w)=z6Generate detailed video structure for one opportunity.suggested_titletopicUnknownz
[   /z] N<   z...Tflushtheme missing_angleangletarget_audience
the_momentmomentwhat_they_knowwhat_they_think_is_wrongwhat_is_wrongclick_triggerconfusion_analysisprimary_questionsecondary_questionsr   c              3   2   K   | ]  }t        |        y wr   r   )r   qs     r   r   z5generate_structure_for_opportunity.<locals>.<genexpr>   s     'RAQ'Rr      existing_contentopening_hookcritical_success_factorverdictverdict_reasoningi  i  )r    r7   r9   r<   r>   r@   rA   rC   rD   rH   rI   rJ   rK   rL   r   z/tmp/layer4_prompt_z.txtwzcat 'z&' | claude --print --model sonnet -p -x   )shellcapture_outputr   timeoutz-    ERROR: Claude returned non-zero exit code{}z$    ERROR: No JSON found in responsevideo_structuresectionstotal_durationunknownu       ✓ Generated z sections ()z    ERROR: Timeoutz    ERROR: JSON parse failed: z    ERROR: )getprintr   r   r   r"   r!   VIRALITY_PROMPTformattempfileosopenwrite
subprocessrununlink
returncodestdoutstripfindrfindjsonloadslenTimeoutExpiredJSONDecodeError	Exception)#r*   r+   r,   r    r7   r9   audiencer<   r>   r@   rA   	confusionrC   secondary_qsrD   rH   rI   rJ   rK   rL   promptr^   r_   prompt_filefresultresponse
json_startjson_endjson_strstructure_data	structuresection_countdurationes#                                      r   "generate_structure_for_opportunityr      s    GG%swww	'BCE	Cay%5":,c
2$? "-.E #'''2:N!OPM ww("-H(C  (+hll<hPR9STUJ!(,,/?"DEN .H(,,WfhjJk!lmM or!BCM ,b1I)S!'3	#IMM2Db$IJ==!6;L,%"ii'RbqAQ'RR*<8 $CGG,>$CD ;<L*3773Lb+QRggi$G$SWW-@"%EF ###%##)/)%40! 7+DS1 $ F"; ,E7$7+s# 	qGGFO	 K= FG
 			+!A$O==&&( ]]3'
>>#&*x1}8FJx0H- .&'89I&IIMM*b9:==!19="=/XJaHPTUU	 	X $$ "40 .qc2$? A3t,sQ   *N7 N*AN7 *AN7 A(N7 *N4/N7 7 P&P&+P  P&P!!P&c                     t        dd       t        dd       t        dd       t        j                         s7t        dt         dd       t        dd       t        j                  d       t        t        d	      5 } t        j                  |       }d d d        j                  d
g       }t        dt        |       dd       d}t        |      D ]=  \  }}t        ||t        |            }|r||d<   |dz  })d|v s.d|vs3|d   dd|d<   ? t        j                         j                         |d<   ||d<   t        t        d      5 } t        j                  || d       d d d        t        dd       t        d| dt        |       dd       t        dt         d       t        dd       t        dd       y # 1 sw Y   7xY w# 1 sw Y   oxY w)Nz<============================================================Tr5   z#LAYER 4: VIDEO STRUCTURE GENERATIONzERROR: z
 not foundz3Run the analysis pipeline first: python3 analyze.pyr2   ropportunitiesz
Loaded z opportunitiesr   rU   r|   )rV   legacylayer4_timestamplayer4_success_countrM      )indentz=
============================================================z
COMPLETE: r3   z structures generatedz	Updated: zG
Next: Run 'python3 generate_dashboard_data.py' to update the dashboard)r[   ANALYSIS_PATHexistssysexitr`   rj   loadrZ   rl   	enumerater   r   now	isoformatdump)ru   datar   success_countir*   r|   s          r   r   r      s   	($	
/t<	($ !j1>C4P	mS	! Qyy| HH_b1M	Ic-()
8E MM* 	X36sAs=?QR	%.C!"QM c!&7s&B69+6FRV)W%&	X  (||~779D	#0D	  
mS	! %Q		$!$% 
/&	J}oQs='9&::O
PX\]	Im_
%T2	($	
T\`a? 0% %s   GGGG$__main__)r8   )__doc__rj   rb   r   pathlibr   r   __file__parent
OUTPUT_DIRr   r\   r   r   intr   r   __name__ r)   r   <module>r      s      
  (^""X-
::Zaz*rD r rS rT rj*bZ zF r)   