
    im                        d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZmZmZmZ dded	ee         d
eeef         fdZddeded	ee         d
efdZ	 dddddddededee         dee         ded         dee         dee         d
e	fdZdeded
e
fdZdeded
e
fdZ	 dddddddededee         dee         ded         dee         dee         d
e	fdZdeded
e
fdZdS )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                     | r|                                  st          d          d|                                  i}|7t          |          }|&t          |          }|r|                    |           |S )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        e/home/agentuser/.hermes/hermes-agent/venv/lib/python3.11/site-packages/firecrawl/v2/methods/scrape.py_prepare_scrape_requestr      s      0ciikk 0.///$)399;;#7L+G44	 ))44D *##D)))    clientc                 v   t          ||          }|                     d|          }|j        st          |d           |                                }|                    d          s#t          |                    dd                    |                    di           }t          |          }t          di |S )aJ  
    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     &c733G{{<11H; 2h111==??D88I E*BCCDDDHHVR((M)-88J!!j!!!r   nodepromptlanguagetimeoutoriginjob_idcoder2   r3   )pythonr0   bashr4   r5   c                   |r|                                 st          d          |o|                                 }|o|                                 }|s|st          d          d|i}	|r||	d<   |r||	d<   |||	d<   |||	d<   |                     d	| d
|	          }
|
j        st	          |
d           |
                                }|                    d          s#t          |                    dd                    t          |          }d|v rd|vr|d         |d<   d|v rd|vr|d         |d<   d|v rd|vr|d         |d<   t          di |S )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   Nr4   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   :  3 31222$

H*FLLNNJ GJ GEFFF 	HD  V  X!YX{{::::DAAH; Hh(FGGGmmooG;;y!! HG-EFFGGGgJZKz$A$A",Z"8
;
""j'H'H&0&?
?#:--2MU_2_2_2<=U2V
./!//J///r   c                 V   |r|                                 st          d          |                     d| d          }|j        st	          |d           |                                }t          |          }d|v rd|vr|d         |d<   d|v rd|vr|d         |d<   t          d	i |S )
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      3 31222}}<6<<<==H; <h(:;;;mmooGgJj((-B*-T-T,67J,K
()*$$)9)K)K'1/'B
#$ ..:...r   c                 "    t          | |          S z(Deprecated alias for stop_interaction().rN   r   r6   s     r   stop_interactive_browserrS          
 FF+++r   c          	      .    t          | ||||||          S )z Deprecated alias for interact().r1   )rG   )r   r6   r7   r2   r3   r4   r5   s          r   scrape_executerV      s0        r   c                 "    t          | |          S 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 6 6 6 6 6 f f f f f f f f f f f f  x/F RVWZ\_W_R`    2" ": "C "(=2I "U] " " " "D ?0
 !28! ?0 ?0 ?0?0?0 3-?0
 SM?0 ./?0 c]?0 SM?0 ?0 ?0 ?0 ?0D/// / / / />,,, , , , , 
 !28!    3-
 SM ./ c] SM    ,,,, , , , , , ,r   