
    hh                     r    d dl 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 d dlmZ  G d de          ZdS )    N)Path)DictIteratorListOptionalUnion)Document)
BaseLoaderc                       e Zd ZdZddeeef         dee         fdZe	dede
eef         fd            Zdee         fdZd	ej        d
edee         fdZdededefdZdededefdZdedededefdZdS )SlackDirectoryLoaderz#Load from a `Slack` directory dump.Nzip_pathworkspace_urlc                 z    t          |          | _        || _        |                     | j                  | _        dS )a)  Initialize the SlackDirectoryLoader.

        Args:
            zip_path (str): The path to the Slack directory dump zip file.
            workspace_url (Optional[str]): The Slack workspace URL.
              Including the URL will turn
              sources into links. Defaults to None.
        N)r   r   r   _get_channel_id_mapchannel_id_map)selfr   r   s      q/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_community/document_loaders/slack_directory.py__init__zSlackDirectoryLoader.__init__   s7     X*"66t}EE    returnc                 :   t          j        | d          5 }	 |                    dd          5 }t          j        |          }ddd           n# 1 swxY w Y   d |D             cddd           S # t
          $ r i cY cddd           S w xY w# 1 swxY w Y   dS )z?Get a dictionary mapping channel names to their respective IDs.rzchannels.jsonNc                 ,    i | ]}|d          |d         S )nameid ).0channels     r   
<dictcomp>z<SlackDirectoryLoader._get_channel_id_map.<locals>.<dictcomp>"   s"    OOO7OOOr   )zipfileZipFileopenjsonloadKeyError)r   zip_filefchannelss       r   r   z(SlackDirectoryLoader._get_channel_id_map   sL    _Xs++ 	x]]?C88 ,A#y||H, , , , , , , , , , , , , , ,OOhOOO		 	 	 	 	 	 	 	
    			 	 	 	 	 	 	 	
	 	 	 	 	 	 	 	 	 	sQ   BA2AA2A	A2A	A22B>BBBBBc              #   h  K   t          j        | j        d          5 }|                                D ]f}t	          |          j        j        }|s|                    d          r3|                     ||          }|D ]}| 	                    ||          V  g	 ddd           dS # 1 swxY w Y   dS )z8Load and return documents from the Slack directory dump.r   z.jsonN)
r    r!   r   namelistr   parentr   endswith
_read_json_convert_message_to_document)r   r&   channel_pathchannel_namemessagesmessages         r   	lazy_loadzSlackDirectoryLoader.lazy_load&   s*     _T]C00 	WH ( 1 1 3 3 W W#L118=# ((11 W#xFFH#+ W W"??VVVVVVW	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	Ws   A<B''B+.B+r&   	file_pathc                     |                     |d          5 }t          j        |          }ddd           n# 1 swxY w Y   |S )z"Read JSON data from a zip subfile.r   N)r"   r#   r$   )r   r&   r4   r'   datas        r   r-   zSlackDirectoryLoader._read_json2   sz    ]]9c** 	 a9Q<<D	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   8<<r2   r0   c                 |    |                     dd          }|                     ||          }t          ||          S )a/  
        Convert a message to a Document object.

        Args:
            message (dict): A message in the form of a dictionary.
            channel_name (str): The name of the channel the message belongs to.

        Returns:
            Document: A Document object representing the message.
        text )page_contentmetadata)get_get_message_metadatar	   )r   r2   r0   r8   r;   s        r   r.   z1SlackDirectoryLoader._convert_message_to_document8   sI     {{62&&--g|DD
 
 
 	
r   c                     |                     dd          }|                     dd          }|                     |||          }||||dS )z;Create and return metadata for a given message and channel.tsr9   user)sourcer   	timestampr@   )r<   _get_message_source)r   r2   r0   rB   r@   rA   s         r   r=   z*SlackDirectoryLoader._get_message_metadataL   sZ    KKb))	{{62&&)),iHH#"	
 
 	
r   r@   rB   c                     | j         rA| j                            |d          }| j          d| d|                    dd           z   S | d| d| S )a@  
        Get the message source as a string.

        Args:
            channel_name (str): The name of the channel the message belongs to.
            user (str): The user ID who sent the message.
            timestamp (str): The timestamp of the message.

        Returns:
            str: The message source.
        r9   z
/archives/z/p.z - )r   r   r<   replace)r   r0   r@   rB   
channel_ids        r   rC   z(SlackDirectoryLoader._get_message_sourceX   s~      	<,00rBBJ%====3y((b11334
 #;;t;;	;;;r   )N)__name__
__module____qualname____doc__r   strr   r   r   staticmethodr   r   r   r	   r3   r    r!   r   dictr-   r.   r=   rC   r   r   r   r   r      se       --F FsDy!1 F(3- F F F F d tCH~    \
W8H- 
W 
W 
W 
W7? s tDz    

+.
	
 
 
 
(

T 

 

 

 

 

 

< <3 <3 <SV < < < < < <r   r   )r#   r    pathlibr   typingr   r   r   r   r   langchain_core.documentsr	   )langchain_community.document_loaders.baser
   r   r   r   r   <module>rS      s            8 8 8 8 8 8 8 8 8 8 8 8 8 8 - - - - - - @ @ @ @ @ @`< `< `< `< `<: `< `< `< `< `<r   