
    Ng,                     z    d dl Z d dlZd dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ dZ G d d	e          ZdS )
    N)datetime)IteratorListOptional)Document)get_from_env)
BaseLoaderz(joplin://x-callback-url/openNote?id={id}c            	           e Zd ZdZ	 	 	 ddee         dededdfd	Zdee	         fd
Z
dedefdZdedee         fdZdedefdZdee	         fdZdS )JoplinLoadera  Load notes from `Joplin`.

    In order to use this loader, you need to have Joplin running with the
    Web Clipper enabled (look for "Web Clipper" in the app settings).

    To get the access token, you need to go to the Web Clipper options and
    under "Advanced Options" you will find the access token.

    You can find more information about the Web Clipper service here:
    https://joplinapp.org/clipper/
    N  	localhostaccess_tokenporthostreturnc                     |pt          dd          }d| d| }| d| d| _        | d| d| _        | d	| d| _        d
S )a  

        Args:
            access_token: The access token to use.
            port: The port where the Web Clipper service is running. Default is 41184.
            host: The host where the Web Clipper service is running.
                Default is localhost.
        r   JOPLIN_ACCESS_TOKENzhttp://:z/notes?token=zE&fields=id,parent_id,title,body,created_time,updated_time&page={page}z/folders/{id}?token=z&fields=titlez/notes/{id}/tags?token=N)r   _get_note_url_get_folder_url_get_tag_url)selfr   r   r   base_urls        g/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/joplin.py__init__zJoplinLoader.__init__   s     $ 
|1(
 (
 +T**D** W Wl W W W 	
 JJ|JJJ 	 MM,MMM 	    c              #     K   d}d}|rot           j                            | j                            |                    }t           j                            |          5 }t          j        |                                	                                          }|d         D ]}t                              |d                   |                     |d                   |                     |d                   |d         |                     |d	                   |                     |d
                   d}t          |d         |          V  |d         }|dz  }d d d            n# 1 swxY w Y   |md S d S )NT   )pageitemsidr!   	parent_idtitlecreated_timeupdated_time)sourcefoldertagsr$   r%   r&   body)page_contentmetadatahas_more)urllibrequestRequestr   formaturlopenjsonloadsreaddecodeLINK_NOTE_TEMPLATE_get_folder	_get_tags_convert_dater   )r   r-   r   req_noteresponse	json_datanoter,   s           r   
_get_noteszJoplinLoader._get_notes8   s      	~--d.@.G.GT.G.R.RSSH''11 X Jx}}'='='?'?@@	%g. 	Q 	QD"4";";tDz";"J"J"&"2"243D"E"E $tDz : :!%g(,(:(:4;O(P(P(,(:(:4;O(P(P   H #VxPPPPPPP$Z0	                	 	 	 	 	s   !C<E))E-0E-	folder_idc                 d   t           j                            | j                            |                    }t           j                            |          5 }t          j        |                                	                                          }|d         cd d d            S # 1 swxY w Y   d S )Nr"   r$   )
r.   r/   r0   r   r1   r2   r3   r4   r5   r6   )r   r@   
req_folderr<   r=   s        r   r8   zJoplinLoader._get_folderM   s    ^++D,@,G,G9,G,U,UVV
^##J// 	&8
8==??#9#9#;#;<<IW%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s   A B%%B),B)note_idc                 x   t           j                            | j                            |                    }t           j                            |          5 }t          j        |                                	                                          }d |d         D             cd d d            S # 1 swxY w Y   d S )Nr"   c                     g | ]
}|d          S )r$    ).0tags     r   
<listcomp>z*JoplinLoader._get_tags.<locals>.<listcomp>W   s    ???SCL???r   r    )
r.   r/   r0   r   r1   r2   r3   r4   r5   r6   )r   rC   req_tagr<   r=   s        r   r9   zJoplinLoader._get_tagsS   s    .(():)A)AW)A)M)MNN^##G,, 	@
8==??#9#9#;#;<<I??Ig,>???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   A
B//B36B3datec                 V    t          j        |dz                                d          S )Ni  z%Y-%m-%d %H:%M:%S)r   fromtimestampstrftime)r   rK   s     r   r:   zJoplinLoader._convert_dateY   s&    %dTk22;;<OPPPr   c              #   >   K   |                                  E d {V  d S )N)r?   )r   s    r   	lazy_loadzJoplinLoader.lazy_load\   s.      ??$$$$$$$$$$$r   )Nr   r   )__name__
__module____qualname____doc__r   strintr   r   r   r?   r8   r   r9   r:   rP   rF   r   r   r   r      s"       
 
 '+	
 
sm
 
 	

 

 
 
 
:HX.    *&S &S & & & &@ @c @ @ @ @Q# Q# Q Q Q Q%8H- % % % % % %r   r   )r3   r.   r   typingr   r   r   langchain_core.documentsr   langchain_core.utilsr   )langchain_community.document_loaders.baser	   r7   r   rF   r   r   <module>r[      s            + + + + + + + + + + - - - - - - - - - - - - @ @ @ @ @ @? O% O% O% O% O%: O% O% O% O% O%r   