o
    Zh                     @   s   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
 d
eZG d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                   @   s@   e Zd ZU edZeed< edZeed< e	dddddd	Z
d
S )_OneNoteGraphSettings.	client_idclient_secretFTz.envZ	MS_GRAPH_ignore)case_sensitiveZpopulate_by_nameZenv_fileZ
env_prefixextraN)__name__
__module____qualname__r
   r   str__annotations__r   r   r   Zmodel_config r   r   c/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/onenote.pyr      s   
 
r   c                   @   sT  e Zd ZU dZeed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 d d 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dededefddZdee fddZdedefddZ e!deeef fdd Z"e!dee fd!d"Z#d'd#d$Z$e!defd%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                 C   s0   d|v rt |d trtdi |d |d< |S )zInitialize the class.r    Nr   )
isinstancedictr   )clsr-   r   r   r   init9   s   zOneNoteLoader.initc                 c   sH   |    zddlm} W n ty   tdw | jdurM| jD ])}| |}||d}d}|j}|r:|jdd}|jd	dd
}t|d|idV  q!dS | j	}|dkrt
j|| jdd}	|	  |	 }
|
d D ]'}|d }| |}||d}d}|j}|r|jd	dd
}t|d|idV  qid|
v r|
d }nd}|dksTd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
)	separatorr4   title)page_contentmetadata
   headerstimeoutvalueidz@odata.nextLink)_authZbs4r3   ImportErrorr*   _get_page_contentr7   Zget_textr   _urlrequestsget_headersraise_for_statusjson)selfr3   Z	object_idZpage_content_htmlZsoupr)   Z	title_tagr8   request_urlresponseZpagespagepage_idr   r   r   	lazy_loadA   sT   









zOneNoteLoader.lazy_loadrM   c                 C   s2   | j d| d }tj|| jdd}|  |jS )z!Get page content from OneNote APIz/pages/z/contentr:   r;   )r$   rD   rE   rF   rG   text)rI   rM   rJ   rK   r   r   r   rB   z   s   zOneNoteLoader._get_page_contentc                 C   s   dd| j  iS )z*Return headers for requests to OneNote APIAuthorizationzBearer )r#   rI   r   r   r   rF      s   zOneNoteLoader._headersc                 C   s   dgS )zReturn required scopes.z
Notes.Readr   rQ   r   r   r   _scopes   s   zOneNoteLoader._scopesc           	   
   C   s  | j dkrdS | jr(| jd}| | _ W d   dS 1 s!w   Y  dS zddlm} W n tyA } ztd|d}~ww || jj	| jj
 | jd}|| j}td t| td	}|d
d dd }|j|| jd}|d | _ z| jj s| jjjdd W n ty } ztd| jj dd |d}~ww | jd}|| j  W d   dS 1 sw   Y  dS )z%Authenticate with Microsoft Graph APIr"   Nrr   )ConfidentialClientApplicationzAMSAL package not found, please install it with `pip install msal`)r   Zclient_credential	authorityz(Visit the following url to give consent:z"Paste the authenticated url here:
zcode=   &)codeZscopesr#   T)parentszCould not create the folder  zto store the access token.w)r#   r!   r&   openreadZmsalrT   rA   r    r   r   Zget_secret_valuer%   Zget_authorization_request_urlrR   printinputsplitZ#acquire_token_by_authorization_codeparentexistsmkdir	Exceptionwrite)	rI   Z
token_filerT   eZclient_instanceZauthorization_request_urlZauthorization_urlZauthorization_codeZaccess_token_jsonr   r   r   r@      sb   
"

"zOneNoteLoader._authc                 C   s  g }g }g }| d | jdur%| dd| jdd d  | d | jdur?| dd| jdd d  | d	 | jdurT| d
d| jdd d  t|dkrd| dd|  t|dkrt| dd|  d|}|dkrd| }| j d| S )z8Create URL for getting page ids from the OneNoteApi API.z
$select=idNz"parentNotebook/displayName%20eq%20'rZ   z%20ZparentNotebookz!parentSection/displayName%20eq%20ZparentSectionztitle%20eq%20r   z$expand=,z$filter=z	%20and%20rW   r"   ?z/pages)appendr'   replacer(   r)   lenjoinr$   )rI   Zquery_params_listZfilter_listZexpand_listZquery_paramsr   r   r   rC      s<   






zOneNoteLoader._url)r.   N)&r   r   r   __doc__r
   r   r    r   r!   boolr#   r   r$   r%   r   homer&   r   r'   r   r(   r)   r*   r   r   classmethodr   r   r2   r   r   rN   rB   propertyrF   rR   r@   rC   r   r   r   r   r   !   sD   
 9
/r   )rn   pathlibr   typingr   r   r   r   r   rD   Zlangchain_core.documentsr   Zpydanticr	   r
   r   r   r   Zpydantic_settingsr   r   Z)langchain_community.document_loaders.baser   r   r   r   r   r   r   <module>   s    