
    _jm                        S r SSKJrJrJrJr  SSKJrJrJ	r	J
r
  SSKJr  SSKJrJrJrJr  SS\S	\\   S
\\\4   4S jjrSS\S\S	\\   S
\4S jjr SSSSSS.S\S\S\\   S\\   S\S   S\\   S\\   S
\	4S jjjrS\S\S
\
4S jrS\S\S
\
4S jr SSSSSS.S\S\S\\   S\\   S\S   S\\   S\\   S
\	4S jjjrS\S\S
\
4S jrg)z.
Scraping functionality for Firecrawl v2 API.
    )OptionalDictAnyLiteral   )ScrapeOptionsDocumentBrowserExecuteResponseBrowserDeleteResponse)normalize_document_input)
HttpClienthandle_response_errorprepare_scrape_optionsvalidate_scrape_optionsNurloptionsreturnc                     U (       a  U R                  5       (       d  [        S5      eSU R                  5       0nUb1  [        U5      nUb#  [        U5      nU(       a  UR	                  U5        U$ )z
Prepare a scrape request payload for v2 API.

Args:
    url: URL to scrape
    options: ScrapeOptions (snake_case) to convert and include
    
Returns:
    Request payload dictionary with camelCase fields
zURL cannot be emptyr   )strip
ValueErrorr   r   update)r   r   request_data	validatedoptss        P/home/rurouni/.local/lib/python3.13/site-packages/firecrawl/v2/methods/scrape.py_prepare_scrape_requestr      sd     ciikk.//$)399;#7L+G4	 ))4D##D)    clientc                 H   [        X5      nU R                  SU5      nUR                  (       d  [        US5        UR	                  5       nUR                  S5      (       d  [        UR                  SS5      5      eUR                  S0 5      n[        U5      n[        S0 UD6$ )a  
Scrape a single URL and return the document.

The v2 API returns: { success: boolean, data: Document }
We surface just the Document to callers.

Args:
    client: HTTP client instance
    url: URL to scrape
    options: Scraping options (snake_case)
    
Returns:
    Document
z
/v2/scrapescrapesuccesserrorUnknown error occurreddata )	r   postokr   jsonget	Exceptionr   r	   )r   r   r   payloadresponsebodydocument_data
normalizeds           r   r    r    )   s     &c3G{{<1H;;h1==?D88I*BCDDHHVR(M)-8J!j!!r   nodepromptlanguagetimeoutoriginjob_idcoder2   r3   )pythonr0   bashr4   r5   c                   U(       a  UR                  5       (       d  [        S5      eU=(       a    UR                  5       nU=(       a    UR                  5       nU(       d  U(       d  [        S5      eSU0n	U(       a  X)S'   U(       a  X9S'   Ub  XYS'   Ub  XiS'   U R                  SU S	3U	5      n
U
R                  (       d  [	        U
S
5        U
R                  5       nUR                  S5      (       d  [        UR                  SS5      5      e[        U5      nSU;   a  SU;  a  US   US'   SU;   a  SU;  a  US   US'   SU;   a  SU;  a  US   US'   [        S0 UD6$ )a  
Interact with the scrape-bound browser session for a scrape job.

Either ``code`` or ``prompt`` must be provided.  When ``prompt`` is given
the server runs an AI agent that translates the natural-language instruction
into browser actions.

Args:
    client: HTTP client instance
    job_id: Scrape job ID
    code: Code to execute (optional if prompt is provided)
    prompt: Natural-language instruction for the browser agent (optional if code is provided)
    language: Programming language ("python", "node", or "bash")
    timeout: Execution timeout in seconds (1-300)
    origin: Optional request origin tag

Returns:
    BrowserExecuteResponse with execution output
Job ID cannot be emptyz*Either 'code' or 'prompt' must be providedr3   r7   r2   r4   r5   /v2/scrape/	/interactzinteract with scrape browserr!   r"   r#   exitCode	exit_codeliveViewUrllive_view_urlinteractiveLiveViewUrlinteractive_live_view_urlr%   )
r   r   r&   r'   r   r(   r)   r*   dictr
   )r   r6   r7   r2   r3   r4   r5   has_code
has_promptr-   r,   r+   r/   s                r   interactrG   H   s]   : 122$

H*FLLNJJEFF 	HD VX!YX{{[	:DAH;;h(FGmmoG;;y!!G-EFGGgJZKz$A",Z"8
;
"j'H&0&?
?#:-2MU_2_2<=U2V
./!/J//r   c                 P   U(       a  UR                  5       (       d  [        S5      eU R                  SU S35      nUR                  (       d  [	        US5        UR                  5       n[        U5      nSU;   a  SU;  a  US   US'   SU;   a  SU;  a  US   US'   [        S	0 UD6$ )
z
Stop the interaction session for a scrape job.

Args:
    client: HTTP client instance
    job_id: Scrape job ID

Returns:
    BrowserDeleteResponse
r;   r<   r=   zstop interactionsessionDurationMssession_duration_mscreditsBilledcredits_billedr%   )r   r   deleter'   r   r(   rD   r   )r   r6   r,   r+   r/   s        r   stop_interactionrN      s     122}}{6()<=H;;h(:;mmoGgJj(-B*-T,67J,K
()*$)9)K'1/'B
#$ .:..r   c                     [        X5      $ z(Deprecated alias for stop_interaction().rN   r   r6   s     r   stop_interactive_browserrS          
 F++r   c          
           [        U UUUUUUS9$ )z Deprecated alias for interact().r1   )rG   )r   r6   r7   r2   r3   r4   r5   s          r   scrape_executerV      s&      r   c                     [        X5      $ rP   rQ   rR   s     r   delete_scrape_browserrX      rT   r   )N)__doc__typingr   r   r   r   typesr   r	   r
   r   utils.normalizer   utilsr   r   r   r   strr   r    intrG   rN   rS   rV   rX   r%   r   r   <module>r`      s   0 /  7 f f x/F RVWZ\_W_R` 2": "C "(=2I "U] "D ?0
 !28! ?0?0?0 3-?0
 SM?0 ./?0 c]?0 SM?0 ?0D/// />,,, , 
 !28!  3-
 SM ./ c] SM ,,,, ,r   