a
    bg                     @   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   z/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/onenote.pyr      s   
r   c                   @   s@  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eedddZee dddZeedddZ e!eeef ddd Z"e!ee dd!d"Z#ddd#d$Z$e!ed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f i |d |d< |S )zInitialize the class.r    )
isinstancedictr   )clsr-   r   r   r   init9   s    zOneNoteLoader.init)r.   c                 c   sD  |    zddlm} W n ty2   tdY n0 | jdur| jD ]R}| |}||d}d}|j}|rv|jdd}|jd	dd
}t|d|idV  qDn| j	}|dkr@t
j|| jdd}	|	  |	 }
|
d D ]P}|d }| |}||d}d}|j}|r|jd	dd
}t|d|idV  qd|
v r:|
d }qd}qd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   sL    











zOneNoteLoader.lazy_load)rM   r.   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rN| jd}| | _ W d   n1 s@0    Y  nPzddlm} W n. ty } ztd|W Y d}~n
d}~0 0 || 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yb } z&td| jj dd |W Y d}~n
d}~0 0 | jd}|| j  W d   n1 s0    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@      sT    
,

zOneNoteLoader._authc                 C   s  g }g }g }| d | jdurJ| dd| jdd d  | d | jdur~| dd| jdd d  | d	 | jdur| d
d| jdd d  t|dkr| dd|  t|dkr| dd|  d|}|dkrd| }| 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   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   !   s.   
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   