
    7|h                         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y)z!Loads data from OneNote Notebooks    )Path)AnyDictIteratorListOptionalN)Document)	BaseModelFieldFilePath	SecretStrmodel_validator)BaseSettingsSettingsConfigDict)
BaseLoaderc                   \    e Zd ZU  ed      Zeed<    ed      Zeed<    e	ddddd	      Z
y
)_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     k/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/document_loaders/onenote.pyr   r      s7    3ZIs$SzM9)%Lr$   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%y)!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                 V    d|v r$t        |d   t              rt        di |d   |d<   |S )zInitialize the class.r)   r#   )
isinstancedictr   )clsr6   s     r%   initzOneNoteLoader.init9   s6     Jvj/A4$H!6!L
9K!LF:r$   c              #     K   | j                          	 ddlm} | j                  r| j                  D ]b  }| j                  |      } ||d      }d}|j                  }|r|j                  d      }|j                  d	d
      }t        |d|i       d y| j                  }|dk7  rt        j                  || j                  d      }	|	j                          |	j                         }
|
d   D ]U  }|d   }| j                  |      } ||d      }d}|j                  }|r|j                  d	d
      }t        d|i       W d|
v r|
d   }nd}|dk7  ryy# t        $ r t        d      w xY ww)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!$%6F
 JJ	!*!3!3$!3!?J#}}t4}H!-*8M 
 ))K##<<T]]TVW))+ !'N 
D"4jG(,(>(>w(G%():MJD!#J $

I '+}}t4}'P"%1Wj<Q 
 %-"'(9":K"$K) #+  	$ 	s"   E4E D?E4E4E11E4r_   c                     | j                   d| dz   }t        j                  || j                  d      }|j	                          |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   sG    //GG9H2MM<<T]]B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!     wt'8'8&9:
 	
r$   c                     dgS )zReturn required scopes.z
Notes.Readr#   re   s    r%   _scopeszOneNoteLoader._scopes   s     ~r$   c                 T   | j                   dk7  ry| j                  r:| j                  j                  d      5 }|j	                         | _         ddd       y	 ddlm}  || j                  j                  | j                  j                  j                         | j                        }|j                  | j                        }t        d       t        |       t!        d	      }|j#                  d
      d   j#                  d      d   }|j%                  || j                        }|d   | _         	 | j                  j&                  j)                         s&| j                  j&                  j+                  d       | j                  j                  d      5 }|j/                  | j                          ddd       y# 1 sw Y   yxY w# t        $ r}t        d      |d}~ww xY w# t,        $ r,}t-        d| j                  j&                   ddz         |d}~ww xY w# 1 sw Y   yxY w)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
   "%%c* 6j$.OO$5!6 6> <--11"&--"="="N"N"P,,O )8(U(U)% <=+, %&K L!2!8!8!A!!D!J!J3!OPQ!R / S S' !T ! !2. AD--446OO**000> %%c* 4j  !2!234 4M6 6
  !W6  24??3I3I2J!L23 4 4sH   F=G	 3A
G& H=G		G#GG#&	H/'HHH'c                    g }g }g }|j                  d       | j                  C|j                  dd| j                  j                  dd       dz          |j                  d       | j                  C|j                  dd| j                  j                  dd       dz          |j                  d       | j                  2|j                  d	d| j                  j                  dd       dz          t        |      d
kD  r#|j                  ddj                  |      z          t        |      d
kD  r#|j                  ddj                  |      z          dj                  |      }|dk7  rd|z   }| j                   d| S )z8Create URL for getting page ids from the OneNoteApi API.z
$select=idz"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<=Q?@ /0(3d''//U;<A>? /??&Adoo&=&=c5&I%J!"LL {a$$Z#((;2G%GH{a$$Z+2B2B;2O%OPxx 122-L++,F<.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'   !   so   ,&+<Q&RH#R6!OT!IL# M#M0GM3G1$499;7:SSJS;#'M8C='!"&L(3-&  $J$ &*Jc#*4(#$ 3   $7%8H- 7%r   
$sCx. 
 
 c  -4^  Bc  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      sG    '  6 6  -  ? @
L 
|BJ	 |Br$   