
    Ng                         d Z ddlmZ ddlmZmZmZmZmZ ddl	Z	ddl
mZ ddlmZmZmZmZmZ ddlmZmZ ddlmZ  G d	 d
e          Z G d dee          ZdS )z!Loads data from OneNote Notebooks    )Path)AnyDictIteratorListOptionalN)Document)	BaseModelFieldFilePath	SecretStrmodel_validator)BaseSettingsSettingsConfigDict)
BaseLoaderc                   p    e Zd ZU  ed          Zeed<    ed          Zeed<    e	ddddd	          Z
d
S )_OneNoteGraphSettings.	client_idclient_secretFTz.env	MS_GRAPH_ignore)case_sensitivepopulate_by_nameenv_file
env_prefixextraN)__name__
__module____qualname__r   r   str__annotations__r   r   r   model_config     h/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/onenote.pyr   r      sf         U3ZZIs$uSzzM9)))%%  LLLr$   r   c                      e Zd ZU dZ ee          Zeed<   	 dZe	ed<   	 dZ
eed<   	 dZeed	<   	 d
Zeed<   	  ej                    dz  dz  Zeed<   	 dZee         ed<   	 dZee         ed<   	 dZee         ed<   	 dZeee                  ed<   	  ed          ededefd                        Zdee         fdZdedefdZ e!deeef         fd            Z"e!dee         fd            Z#d dZ$e!defd            Z%dS )!OneNoteLoaderz"Load pages from OneNote notebooks.)default_factorysettingsFauth_with_token access_tokenz+https://graph.microsoft.com/v1.0/me/onenoteonenote_api_base_urlz,https://login.microsoftonline.com/consumers/authority_urlz.credentialszonenote_graph_token.txt
token_pathNnotebook_namesection_name
page_title
object_idsbefore)modevaluesreturnc                 n    d|v r0t          |d         t                    rt          di |d         |d<   |S )zInitialize the class.r)   r#   )
isinstancedictr   )clsr6   s     r%   initzOneNoteLoader.init9   sE     Jvj/A4$H$H!6!L!L
9K!L!LF:r$   c              #   $  K   |                                   	 ddlm} n# t          $ r t          d          w xY w| j        z| j        D ]p}|                     |          } ||d          }d}|j        }|r|                    d          }|                    d	d
          }t          |d|i          V  qdS | j	        }|dk    rt          j        || j        d          }	|	                                 |	                                }
|
d         D ]b}|d         }|                     |          } ||d          }d}|j        }|r|                    d	d
          }t          |d|i          V  cd|
v r	|
d         }nd}|dk    dS dS )z
        Get pages from OneNote notebooks.

        Returns:
            A list of Documents with attributes:
                - page_content
                - metadata
                    - title
        r   )BeautifulSoupzJbeautifulsoup4 package not found, please install it with `pip install bs4`Nzhtml.parserr+   T)strip
)	separatorr?   title)page_contentmetadata
   headerstimeoutvalueidz@odata.nextLink)_authbs4r>   ImportErrorr3   _get_page_contentrB   get_textr	   _urlrequestsget_headersraise_for_statusjson)selfr>   	object_idpage_content_htmlsoupr2   	title_tagrC   request_urlresponsepagespagepage_ids                r%   	lazy_loadzOneNoteLoader.lazy_loadA   s<      	

	))))))) 	 	 	$  	 ?&!_ 
 
	$($:$:9$E$E!$}%6FF
 J	 @!*!3!3$!3!?!?J#}}t4}HH!-*8M      
 
 )K###<T]TVWWW))+++ !'N 
 
D"4jG(,(>(>w(G(G%(=):MJJD!#J $
I  Q'+}}t4}'P'P"%1Wj<Q       %--"'(9":KK"$K) ######s    9r_   c                     | j         d| dz   }t          j        || j        d          }|                                 |j        S )z!Get page content from OneNote APIz/pages/z/contentrE   rF   )r-   rQ   rR   rS   rT   text)rV   r_   r[   r\   s       r%   rN   zOneNoteLoader._get_page_contentz   sM    /2MG2M2M2MM<T]BOOO!!###}r$   c                     dd| j          iS )z*Return headers for requests to OneNote APIAuthorizationzBearer )r,   rV   s    r%   rS   zOneNoteLoader._headers   s     :t'8::
 	
r$   c                     dgS )zReturn required scopes.z
Notes.Readr#   re   s    r%   _scopeszOneNoteLoader._scopes   s     ~r$   c                 X   | j         dk    rdS | j        rN| j                            d          5 }|                                | _         ddd           dS # 1 swxY w Y   dS 	 ddlm} n"# t          $ r}t          d          |d}~ww xY w || j        j	        | j        j
                                        | j                  }|                    | j                  }t          d           t          |           t!          d	          }|                    d
          d                             d          d         }|                    || j                  }|d         | _         	 | j        j                                        s | j        j                            d           n3# t,          $ r&}t-          d| j        j         ddz             |d}~ww xY w| j                            d          5 }|                    | j                    ddd           dS # 1 swxY w Y   dS )z%Authenticate with Microsoft Graph APIr+   Nrr   )ConfidentialClientApplicationzAMSAL package not found, please install it with `pip install msal`)r   client_credential	authorityz(Visit the following url to give consent:z"Paste the authenticated url here:
zcode=   &)codescopesr,   T)parentszCould not create the folder  zto store the access token.w)r,   r*   r/   openreadmsalrj   rM   r)   r   r   get_secret_valuer.   get_authorization_request_urlrg   printinputsplit#acquire_token_by_authorization_codeparentexistsmkdir	Exceptionwrite)	rV   
token_filerj   eclient_instanceauthorization_request_urlauthorization_urlauthorization_codeaccess_token_jsons	            r%   rK   zOneNoteLoader._auth   s   ""F (	4%%c** 6j$.OO$5$5!6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6>>>>>>>   !W 
 <;-1"&-"="N"N"P"P,  O )8(U(U) )% <===+,,, %&K L L!2!8!8!A!A!!D!J!J3!O!OPQ!R / S S' !T ! ! !2. AD-4466 ?O*000>>>   L4?3ILLL23   %%c** 4j  !23334 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4sS   AAA$A+ +
B
5BB
+>F* *
G4!GG7HH#&H#c                 D   g }g }g }|                     d           | j        J|                     dd| j                            dd           dz              |                     d           | j        J|                     dd| j                            dd           dz              |                     d	           | j        5|                     d
d| j                            dd           dz              t          |          dk    r+|                     dd                    |          z              t          |          dk    r+|                     dd                    |          z              d                    |          }|dk    rd|z   }| j         d| S )z8Create URL for getting page ids from the OneNoteApi API.z
$select=idNz"parentNotebook/displayName%20eq%20'rr   z%20parentNotebookz!parentSection/displayName%20eq%20parentSectionztitle%20eq%20r   z$expand=,z$filter=z	%20and%20rn   r+   ?z/pages)appendr0   replacer1   r2   lenjoinr-   )rV   query_params_listfilter_listexpand_listquery_paramss        r%   rP   zOneNoteLoader._url   s      ...)4?d(00e<<???@   /000(3>d'//U;;>>>?   ///?&"Ldo&=&=c5&I&I"L"L"LL   {a$$Z#((;2G2G%GHHH{a$$Z+2B2B;2O2O%OPPPxx 1222-L+AA<AAAr$   )r7   N)&r   r   r   __doc__r   r   r)   r!   r*   boolr,   r    r-   r.   r   homer/   r   r0   r   r1   r2   r3   r   r   classmethodr   r   r<   r   r	   r`   rN   propertyrS   rg   rK   rP   r#   r$   r%   r'   r'   !   s6        ,,&+e<Q&R&R&RH#RRR6!OT!!!IL# M#MMM0GM3GGG1$49;;7:SSJSSS;#'M8C='''!"&L(3-&&&  $J$$$ &*Jc#***4_(###$ 3    [ $#7%8H- 7% 7% 7% 7%r      
$sCx. 
 
 
 X
 c    X-4 -4 -4 -4^  Bc  B  B  B X B  B  Br$   r'   )r   pathlibr   typingr   r   r   r   r   rQ   langchain_core.documentsr	   pydanticr
   r   r   r   r   pydantic_settingsr   r   )langchain_community.document_loaders.baser   r   r'   r#   r$   r%   <module>r      sH   ' '       6 6 6 6 6 6 6 6 6 6 6 6 6 6  - - - - - -              ? > > > > > > > @ @ @ @ @ @
 
 
 
 
L 
 
 
|B |B |B |B |BJ	 |B |B |B |B |Br$   