
    _j!                        S SK JrJrJrJr  SSKJrJrJrJ	r	J
r
Jr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  S S	KrS
\\\      S\\   4S jrS\\\4   S\\\4   4S jrS	S.S\\   S\\   S\\\4   4S jjrS\S\\   S\	4S jr S S\S\S\\   S\
4S jjrS	S.S\S\S\\   S\
4S jjr S S\S\S\\   S\\   S\\   4
S jjrS\S\S\ 4S jr!S\S\S\\\4   4S jr"g	)!    )OptionalListDictAny   )ScrapeOptionsWebhookConfigDocumentBatchScrapeResponseBatchScrapeJobPaginationConfig)AsyncHttpClient)prepare_scrape_options)handle_response_error)normalize_document_input)validate_batch_urlsN	data_listreturnc                     / nU =(       d    /  H?  n[        U[        5      (       d  M  [        U5      nUR                  [	        S0 UD65        MA     U$ )N )
isinstancedictr   appendr
   )r   	documentsdoc
normalizeds       S/home/rurouni/.local/lib/python3.13/site-packages/firecrawl/v2/methods/aio/batch.py_parse_batch_scrape_documentsr   
   sL     "IBc4  1#6JX3
34      bodyc                 t   U R                  S5      (       d  [        U R                  SS5      5      eU R                  S5      U R                  SS5      U R                  SS5      U R                  S5      U R                  S	5      U R                  S
5      [        U R                  S/ 5      =(       d    / 5      S.$ )NsuccesserrorUnknown error occurredstatus	completedr   totalcreditsUsed	expiresAtnextdatar%   r&   r'   credits_used
expires_atr*   r+   )get	Exceptionr   )r    s    r   #_parse_batch_scrape_status_responser1      s    88I*BCDD ((8$XXk1-'1%/hh{+ -dhhvr.B.HbI r   )optionsurlsr2   c          	         U (       d  [        S5      e[        U  Vs/ s H*  n[        U[        5      (       a  UR	                  5       OUPM,     sn5      nSU0nU(       a#  [        U5      nU(       a  UR                  U5        UR                  S5      =nb)  [        U[        5      (       a  UOUR                  SS9US'   UR                  S5      =nb  XS'   UR                  S5      =nb  XS	'   UR                  S
5      =nb  XS'   UR                  S5      =nb  XS'   UR                  S5      =nb$  [        U5      R	                  5       n	U	(       a  XS'   U$ s  snf )NzURLs list cannot be emptyr3   webhookT)exclude_noneappend_to_id
appendToIdignore_invalid_urlsignoreInvalidURLsmax_concurrencymaxConcurrencyzero_data_retentionzeroDataRetentionintegration)	
ValueErrorr   r   strstripr   updater/   
model_dump)
r3   r2   kwargsuvalidated_urlspayloadoptswvtrimmed_integrations
             r   _preparerM   !   sS   455(Z^)_Z^UVz!S7I7I!'')q*PZ^)_`N%~6G%g.NN4 ZZ	""/",Q"4"4Q!,,TX,:Y	ZZ''4 !ZZ-..;'(#$ZZ)**7$% !ZZ-..;'(#$ZZ&&3!!flln%8M"N) *`s   1Eclientc                   #    [        U40 UD6nU R                  SU5      I S h  vN nUR                  S:  a  [        US5        UR	                  5       nUR                  S5      (       d  [        UR                  SS5      5      e[        UR                  S5      UR                  S5      UR                  S	5      S
9$  N7f)Nz/v2/batch/scrape  zstart batch scraper"   r#   r$   idurlinvalidURLs)rQ   rR   invalid_urls)rM   poststatus_coder   jsonr/   r0   r   )rN   r3   rE   rH   responser    s         r   start_batch_scraperY   <   s     t&v&G[[!3W==Hs"h(<===?D88I*BCDD$((4.dhhuoTXT\T\]jTkll >s   "C B>BC job_idpagination_configc           
        #    U R                  SU 35      I Sh  vN nUR                  S:  a  [        US5        UR                  5       n[	        U5      nUS   nU(       a  UR
                  OSnU(       a#  US   (       a  [        U US   UU5      I Sh  vN n[        US   US	   US
   US   US   U(       d  US   US9$ SUS9$  N N37f)a8  
Get the status of a batch scrape job.

Args:
    client: Async HTTP client instance
    job_id: ID of the batch scrape job
    pagination_config: Optional configuration for pagination behavior
    
Returns:
    BatchScrapeJob containing job status and data
    
Raises:
    Exception: If the status check fails
/v2/batch/scrape/NrP   zget batch scrape statusr+   Tr*   r%   r&   r'   r-   r.   r,   )r/   rV   r   rW   r1   auto_paginate_fetch_all_batch_pages_asyncr   )rN   rZ   r[   rX   r    rH   docsr^   s           r   get_batch_scrape_statusra   G   s     & ZZ"3F8 <==Hs"h(AB==?D1$7G6?D 8I%33dM1FO	
 
 x +&g^,<($1WV_  8< # >
s"   CCA:CC	2C	C)request_timeoutnext_urlrb   c          
         #    U R                  XS9I Sh  vN nUR                  S:  a  [        US5        UR                  5       n[	        U5      n[        US   US   US   US   US	   US
   US   S9$  N_7f)a  
Fetch a single page of batch scrape results using the provided next URL.

Args:
    client: Async HTTP client instance
    next_url: Opaque next URL from a prior batch scrape status response
    request_timeout: Timeout (in seconds) for the HTTP request

Returns:
    BatchScrapeJob with the page data and next URL (if any)

Raises:
    Exception: If the request fails or returns an error response
)timeoutNrP   zget batch scrape status pager%   r&   r'   r-   r.   r*   r+   r,   )r/   rV   r   rW   r1   r   )rN   rc   rb   rX   r    rH   s         r   get_batch_scrape_status_pagerf   v   s     ( ZZZBBHs"h(FG==?D1$7Gx +&g^,<(V_V_  Cs   A7A5A A7initial_documentsc                   #    UR                  5       nUnSnU(       a  UR                  OSnU(       a  UR                  OSnU(       a  UR                  OSn	[        R
                  " 5       n
U(       Ga  Ub  Xg:  a   U$ U	b  [        R
                  " 5       U
-
  U	:  a   U$ U R                  U5      I Sh  vN nUR                  S:  a6  SSKnUR                  S5      nUR                  SUR                   35         U$ UR                  5       n [        U5      nUS    H(  nUb  [        U5      U:  a    OUR                  U5        M*     Ub  [        U5      U:  a   U$ US   nUS-  nU(       a  GM  U$  N! [         a     U$ f = f7f)	a;  
Fetch all pages of batch scrape results asynchronously.

Args:
    client: Async HTTP client instance
    next_url: URL for the next page
    initial_documents: Documents from the first page
    pagination_config: Optional configuration for pagination limits
    
Returns:
    List of all documents from all pages
r   NrP   	firecrawlzFailed to fetch next page: r+   r*      )copy	max_pagesmax_resultsmax_wait_timetime	monotonicr/   rV   logging	getLoggerwarningrW   r1   r0   lenr   )rN   rc   rg   r[   r   current_url
page_countrl   rm   rn   
start_timerX   rq   logger	page_datapage_payloaddocuments                    r   r_   r_      s    $ "&&(IKJ 0A!++dI3D#//$K7H%33dM!J
!
(?H E %DNN,<z,I]+ZB =  K003&&&{3FNN89M9M8NOP. + MMO		>yIL
 %V,H'c)n.KX&	 - ##i.K*G  #6*a
K +N = 1  	" %	s>   B2F
4E75AF
E9 AF
5F
9
FF
FF
c                    #    U R                  SU 35      I S h  vN nUR                  S:  a  [        US5        UR                  5       nUR	                  S5      S:H  $  ND7f)Nr]   rP   zcancel batch scraper%   	cancelled)deleterV   r   rW   r/   rN   rZ   rX   r    s       r   cancel_batch_scraper      s\     ]]%6vh#?@@Hs"h(=>==?D88H,,	 As   A!AAA!c                   #    U R                  SU S35      I S h  vN nUR                  S:  a  [        US5        UR                  5       nUR                  S5      (       d  [	        UR                  SS5      5      eU$  Nc7f)Nr]   z/errorsrP   zget batch scrape errorsr"   r#   r$   )r/   rV   r   rW   r0   r   s       r   get_batch_scrape_errorsr      su     ZZ"3F87 CDDHs"h(AB==?D88I*BCDDK Es   BA?A$B)N)#typingr   r   r   r   typesr   r	   r
   r   r   r   utils.http_client_asyncr   utils.validationr   utils.error_handlerr   utils.normalizer   methods.batchr   ro   r   rA   r1   rM   rY   ra   floatrf   r_   boolr   r   r   r   r   <module>r      s   , , r r 6 6 8 7 0 Xd3i-@ T(^ d38n c3h  EI 49 (=*A W[\_ad\dWe 6m_ mDI mTg m 59,,,   01, 	,f (,	!!! e_	!
 !P 59	DDD H~D   01	D
 
(^DN-o -s -t -/ 3 4PSUXPX> r   