
    ih                     Z    d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	 dZ
 G d de      Zy)	    )ListOptionalN)Document)
BaseLoader)WebBaseLoaderzhttps://www.ifixit.com/api/2.0c            	           e Zd ZdZdefdZdee   fdZe	ddededee   fd       Z
	 dd
ee   dee   fdZ	 dd
ee   dedee   fdZdd
ee   dee   fdZy	)IFixitLoadera  Load `iFixit` repair guides, device wikis and answers.

    iFixit is the largest, open repair community on the web. The site contains nearly
    100k repair manuals, 200k Questions & Answers on 42k devices, and all the data is
    licensed under CC-BY.

    This loader will allow you to download the text of a repair guide, text of Q&A's
    and wikis from devices on iFixit using their open APIs and web scraping.
    web_pathc                    |j                  d      st        d      |j                  dd      g d}	 t        fd|D              st        d      j	                  d      D cg c]  }|s|	 }}	 |d   d	k7  r|d   nd
| _        | j
                  d
k(  s| j
                  dk(  r|d   | _        || _        y|d   | _        || _        yc c}w )zInitialize with a web path.zhttps://www.ifixit.comz1web path must start with 'https://www.ifixit.com' )z/Devicez/Guidez/Answersz	/Teardownc              3   @   K   | ]  }j                  |        y wN)
startswith).0allowed_pathpaths     i/var/www/html/dev/engine/venv/lib/python3.12/site-packages/langchain_community/document_loaders/ifixit.py	<genexpr>z(IFixitLoader.__init__.<locals>.<genexpr>!   s     S\4??<0Ss   z?web path must start with /Device, /Guide, /Teardown or /Answers/r   TeardownGuideAnswers      N)r   
ValueErrorreplaceanysplit	page_typeidr
   )selfr
   allowed_pathsxpiecesr   s        @r   __init__zIFixitLoader.__init__   s    ""#;<PQQ 8"=FS]SSQ  "ZZ_2!22;&,Qi:&=7>>W$)(CQiDG ! QiDG  3s   'C/Creturnc                 
   | j                   dk(  r| j                         S | j                   dk(  s| j                   dk(  r| j                         S | j                   dk(  r| j                         S t	        d| j                   z         )NDevicer   r   r   zUnknown page type: )r   load_device
load_guideload_questions_and_answersr   )r!   s    r   loadzIFixitLoader.load2   sq    >>X%##%%^^w&$..J*F??$$^^y(22442T^^CDD    querydoc_typec                    t        j                  t        dz   | z   dz   |z         }|j                  dk7  r"t	        d| z   dz   |j                         z         |j                         }|d   }g }|D ]I  }	 t        |d         }|j                  dk(  r||j                  d	
      z  }n||j                         z  }K |S # t        $ r Y Yw xY w)zLoad suggestions.

        Args:
            query: A query string
            doc_type: The type of document to search for. Can be one of "all",
              "device", "guide", "teardown", "answer", "wiki".

        Returns:

        z	/suggest/z
?doctypes=   z Could not load suggestions for "z"
resultsurlr(   F)include_guides)
requestsgetIFIXIT_BASE_URLstatus_coder   jsonr	   r   r)   r,   )r.   r/   resdatar2   outputresultloaders           r   load_suggestionszIFixitLoader.load_suggestions<   s     llk)E1L@8K
 ??c!2U:UBSXXZO  xxzy/ 	F%fUm4##x/f000FFFfkkm+F	   s   4AB>>	C
	C
Nurl_overridec                    t        || j                  n|      }|j                         }g }|j                  dd      j                  }|j                  d|z          |j                  |j                  d      j                  j                                |j                  dd      }|r,|j                  d|j                  j                         z          |j                  d      D ]  }|j                  d	      rd
|d	   v r|j                  d       n*d|d   v r|j                  d       n|j                  d       ||j                  d      D cg c]  }|j                  j                          c}z  }|j                  d        dj                  |      j                         }	| j                  |d}
t        |	|
      gS c c}w )zLoad a list of questions and answers.

        Args:
            url_override: A URL to override the default URL.

        Returns: List[Document]

        h1z
post-title# z.post-content .post-textdivzpost-answers-headerz
## z".js-answers-list .post.post-answeritempropacceptedAnswerz
### Accepted Answerzpost-helpfulclassz
### Most Helpful Answerz
### Other Answer
sourcetitlepage_contentmetadata)r   r
   scrapefindtextappend
select_onestripselecthas_attrjoinr   )r!   r@   r>   soupr<   rK   answersHeaderanswerarQ   rN   s              r   r+   z'IFixitLoader.load_questions_and_answersb   s    0Dt}},W}}		$-22dUl#doo&@AFFLLNO		%)>?MM'M$6$6$<$<$>>?kk"FG 	 Fz*/?6*CU/U566'?29:23(.6P(Q#$ F MM$	  yy &&("mme<dX>??s   !Gr4   c                    g }|t         dz   | j                  z   }n|}t        j                  |      }|j	                         }dj                  dD cg c]  }||v r||    c}      j                         }| j                  |d   d}	|j                  t        ||	             |rH	 |d   D 
cg c]  }
|
d   	 }}
|D ]-  }|j                  t        |      j                         d	          / |S c c}w c c}
w )
zLoads a device

        Args:
            url_override: A URL to override the default URL.
            include_guides: Whether to include guides linked to from the device.
              Defaults to True.

        Returns:

        z/wikis/CATEGORY/rH   )rK   descriptioncontents_rawrK   rI   rL   guidesr3   r   )r7   r    r5   r6   r9   rW   rT   r
   rR   r   r	   r,   )r!   r@   r4   	documentsr3   r:   r;   keyrQ   rN   guide
guide_urls	guide_urls                r   r)   zIFixitLoader.load_device   s    	!$66@CCll3xxzyy D$; S	
 %' 	 #mmd7mDthGHT48NC5%,CJC' D	  i!8!=!=!?!BCD ! Ds   C1/C6c                    |t         dz   | j                  z   }n|}t        j                  |      }|j                  dk7  r,t        d| j                  z   dz   |j                         z         |j                         }d|d   z   |d   g}|j                  d       t        |d	         d
k(  r|j                  d       n!|d	   D ]  }|j                  d|d   z           |j                  d       t        |d         d
k(  r|j                  d       n!|d   D ]  }|j                  d|d   z           |d   D ]S  }|j                  d|d   dk7  r|d   ndj                  |d         z          |d   D ]  }	|j                  |	d           U |j                  |d          dj                  |      }
| j                  |d   d}t        |
|      gS )zLoad a guide

        Args:
            url_override: A URL to override the default URL.

        Returns: List[Document]

        z/guides/r1   zCould not load guide: rH   rC   rK   introduction_rawz

###Tools Required:toolsr   z
 - Nonez
 - rQ   z

###Parts Required:partsstepsz

## r   zStep {}orderbylinestext_rawconclusion_rawrI   rL   )r7   r    r5   r6   r8   r   r
   r9   rR   lenformatrW   r   )r!   r@   r3   r:   r;   	doc_partstoolpartrowlinerQ   rN   s               r   r*   zIFixitLoader.load_guide   s    !J.8CCll3??c!(4==84?#((*L  xxzDM)40B+CD	12tG}"[)W 9  4<!789 	12tG}"[)W 9  4<!789 = 	3C 7|r) L"))#i.9	 G 3  j!123	3 	./0yy#"mmd7mDdX>??r-   )r   allr   )NT)__name__
__module____qualname____doc__strr%   r   r   r,   staticmethodr?   r   r+   boolr)   r*    r-   r   r	   r	      s    ! !6Ed8n E # #C #DN # #L -1*@$SM*@	h*@Z JN&$SM&BF&	h&P:@x} :@X :@r-   r	   )typingr   r   r5   langchain_core.documentsr   )langchain_community.document_loaders.baser   -langchain_community.document_loaders.web_baser   r7   r	   r}   r-   r   <module>r      s)    !  - @ G2d@: d@r-   