
    Li                         d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ  G d	 d
          ZdS )z^
Archive Check - High-level Operation

This module checks if a URL is archived on archive.is.
    N)DictAny)ManagedPage)ClickElement)TypeText)	PasteText)
PressEnterc                   H    e Zd ZdZddedefdZdeded	eee	f         fd
Z
dS )ArchiveChecka  
    Check if a URL is archived on archive.is.

    State Management:
        This operation does NOT handle page state management.
        The caller (typically BrowserNavigator) is responsible for:
        - Setting page to BUSY before calling
        - Setting page to IDLE/ERROR after completion

    Usage:
        checker = ArchiveCheck()
        result = await checker.check(managed_page, url)
          ??direct_nav_probabilitypaste_probabilityc                     t                      | _        t                      | _        t	                      | _        t                      | _        || _        t          d| d           dS )a3  
        Initialize the archive checker with primitive operations.
        
        Args:
            direct_nav_probability (float): Probability (0.0-1.0) of directly navigating to archive.is/<url>
                                           instead of using search form (default: 0.5 = 50% chance)
        u/   🔍 ArchiveCheck initialized (direct_nav_prob=)N)
r   clickerr   typerr   pasterr	   presserr   print)selfr   r   s      @/home/byschii/byschiidev/penelope/lib/highlevel/archive_check.py__init__zArchiveCheck.__init__"   sY     $~~ZZ
kk!||&<#Y@VYYYZZZZZ    managed_pageurlreturnc           	      4  K   t          d| d           |j        }	 t          j                    | j        k     }|rPt          d|            d| }|                    |d           d{V  t          j        d           d{V  nt          d	           t          d
           |                    dd           d{V  t          j        d           d{V  t          d           | j                            |d           d{V }|	                    d          s%t          d|	                    d                     t          d|            t          j                    | j        k     r"| j                            ||           d{V }n!| j                            ||           d{V }|	                    d          s%t          d|	                    d                     t          d           | j                            |           d{V }|	                    d          s%t          d|	                    d                     t          j        d           d{V  |                                 d{V }	d|	v}
d|	v}d|	v }d||j        t)          |
||g          t+          |
 | | g          |rdndd}|S # t          $ r7}d||j        t-          |          d}t          d|            |cY d}~S d}~ww xY w) a  
        Check if a URL is archived on archive.is.

        Args:
            managed_page (ManagedPage): Managed page to use (caller handles state)
            url (str): URL to check for archives

        Returns:
            Dict containing check result and archive information
            
        Note:
            Caller must handle page state management (set_busy/set_idle/set_error).
        u   🔍 Checking if 'z' is archivedu%   🎯 Direct navigation to archive.is/zhttps://archive.is/domcontentloaded)
wait_untilN   u   🔍 Using search form approachu    🌐 Navigating to archive.is...zhttps://archive.is   u   👆 Clicking search input...z#qsuccesszFailed to click input: erroru   ⌨️ Typing URL: zFailed to type URL: u   ⏎ Pressing Enter to search...zFailed to press Enter: z
No resultszYou may want toz)List of URLs, ordered from newer to olderT
direct_navsearch_form)r#   r   page_idis_archivedwtfmethodF)r#   r   r'   r$   u   ❌ Archive check failed: )r   pagerandomr   gotoasynciosleepr   click_selectorget	Exceptionr   r   paster   typer   presscontentr'   allanystr)r   r   r   r+   use_direct_navarchive_urlclick_resulttype_resultpress_resultr6   is_archived1is_archived2is_arcihved3resulteerror_results                   r   checkzArchiveCheck.check1   s      	53555666 D	 #]__t/JJN %'CcCCDDD9C99ii8JiKKKKKKKKKmA&&&&&&&&&& 7888 8999ii 4ASiTTTTTTTTTmA&&&&&&&&& 5666%)\%@%@t%T%TTTTTTT#''	22 [#$Yl>N>Nw>W>W$Y$YZZZ 1C11222=??T%;;;(,(9(9$(D(D"D"D"D"D"D"DKK(,
c(B(B"B"B"B"B"B"BK"y11 W#$U;??7;S;S$U$UVVV 7888%)\%7%7%=%=======#''	22 [#$Yl>N>Nw>W>W$Y$YZZZ mA&&&&&&&&& !LLNN******G'w6L,G;LF'QL  '/"L,#MNN,,.>L@PQRR*8K,,m F M 	  	  	  '/Q	 L 2q22333	 s   J7K 
L ,LLLN)r   r   )__name__
__module____qualname____doc__floatr   r   r9   r   r   rE    r   r   r   r      s         [ [u [u [ [ [ [U  U # U $sCx. U  U  U  U  U  U r   r   )rI   r.   r,   typingr   r   lib.page_managementr   lib.primitive.click_elementr   lib.primitive.type_textr   lib.primitive.paste_textr   lib.primitive.press_enterr	   r   rK   r   r   <module>rR      s               + + + + + + 4 4 4 4 4 4 , , , , , , . . . . . . 0 0 0 0 0 0s  s  s  s  s  s  s  s  s  s r   