o
    i7                     @   sn   d Z ddlZddlmZm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 G d	d
 d
ZdS )z
Archive Get Plugin

Retrieves an archived page from archive.is.
Can start from existing search results or perform a fresh search.
    N)DictAny)PluginMetadataPluginParameter)ClickElement)
PressEnter)TypeText)
ScrollPagec                   @   s   e Zd ZdZedddgedddddd	d
eddddddgdZdeee	f dedB fddZ
dedeee	f deee	f fddZdS )ArchiveGetPluginz
    Get an archived page from archive.is.
    
    Supports two modes:
    - Fresh search: Navigate to archive.is, search for URL, click first result
    - From check: Assume page already shows results, click first result directly
    zarchive-getzArchive GetPOSTurlzURL to RetrieveFzhttps://example.comzAURL to retrieve from archive (required unless from_check is true))nametypelabelrequiredplaceholder	help_text
from_checkcheckboxzFrom check pagez7Start from existing archive check results (skip search))r   r   r   defaultr   )r   descriptionmethods
parametersparamsreturnNc                 C   s,   | dd }| dd}|s|sdS dS )z@Validate that URL is provided when not starting from check page.r    r   Fz1URL is required when not starting from check pageN)getstrip)selfr   r   r    r   ?/home/byschii/byschiidev/penelope/plugins/archive_get/plugin.pyvalidate_params2   s
   z ArchiveGetPlugin.validate_paramspage_idc              
      s  | dd }| dd}td| d|  ||I dH }|j}zz}|d| I dH  t }t }	t }
t	 }|std	 |j
d
ddI dH  tdI dH  td ||dI dH }| ds~d|d| d dW W | I dH  S td|  |	||I dH }| dsd|d| d dW W | I dH  S td |
|I dH }| dsd|d| d dW W | I dH  S tdI dH  d}td|  |||I dH }| dsd}z	| I dH }W n
 ty   Y nw d|d| d |j|dW W | I dH  S tdI dH  td |j|dd d!I dH  td"I dH  td# |j|dd d!I dH  td"I dH  td$ |j|d%dd!I dH  td&I dH  z| I dH }| I dH }W n ty   d}d}Y nw d'||j||d(d)}td*|j  |W W | I dH  S  ty } ztd+|  |t|I dH   d}~ww | I dH  w ),aL  
        Execute archive get operation.
        
        Args:
            browser_navigator: BrowserNavigator instance
            page_id: Page to use for operation
            params: Validated parameters (url, from_check)
            
        Returns:
            Dict with success status, archive URL, and page content
        r   r   r   Fu   📥 Archive get: url=z, from_check=Nzgetting archive for u3   🌐 Navigating to archive.is to search for the URLzhttps://archive.isdomcontentloaded)
wait_until   u   👆 Clicking search input '#q'z#qsuccesszFailed to click search input: error)r&   r   r'   u   ⌨️ Typing URL: zFailed to type URL: u   ⏎ Pressing Enter to searchzFailed to press Enter:    z.TEXT-BLOCK > a:nth-child(1)u!   👆 Clicking first result link: z#Failed to click first result link: )r&   r   r'   page_urlpage_contentg      ?u   🔽 Scrolling down (1)i  i  )distanceslowmog333333?u   🔽 Scrolling down (2)u   🔼 Scrolling upig?Tz1Clicked first result and performed scroll pattern)r&   r   archive_url
page_titler*   messageu(   ✅ Archive get completed, resolved to: u   ❌ Archive get failed: )r   r   print_get_page_to_usepageset_busyr   r   r   r	   gotoasynciosleepclick_selectorset_idler   presscontent	Exceptionr   scrolltitle	set_errorstr)r   browser_navigatorr"   r   r   r   managed_pager2   clickertyperpresserscrollerclick_inputtype_resultpress_resultfirst_link_selectorclick_firstr*   r.   resulter   r   r    execute<   s   
Z
P
F2	zArchiveGetPlugin.execute)__name__
__module____qualname____doc__r   r   metadatar   r?   r   r!   rM   r   r   r   r    r
      s2    *
r
   )rQ   r5   typingr   r   lib.plugin_loaderr   r   lib.primitive.click_elementr   lib.primitive.press_enterr   lib.primitive.type_textr   lib.primitive.scroll_pager	   r
   r   r   r   r    <module>   s   