
    Ng                         d dl Z d dlZd dlZd dl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mZ  e j        e          Z G d de	          ZdS )    N)IteratorListUnion)BaseChatLoader)ChatSession)	AIMessageHumanMessagec                   r    e Zd ZdZdefdZdedefdZedede	e         fd            Z
de	e         fdZd	S )
WhatsAppChatLoaderz@Load `WhatsApp` conversations from a dump zip file or directory.pathc                     || _         g d}t          j        dd                    d |D                       z   dz   t          j                  | _        t          j        dt          j                  | _        dS )	aZ  Initialize the WhatsAppChatLoader.

        Args:
            path (str): Path to the exported WhatsApp chat
                zip directory, folder, or file.

        To generate the dump, open the chat, click the three dots in the top
        right corner, and select "More". Then select "Export chat" and
        choose "Without media".
        )zThis message was deletedz<Media omitted>zimage omittedzxMessages and calls are end-to-end encrypted. No one outside of this chat, not even WhatsApp, can read or listen to them.(|c                     g | ]}d |z   S )z\u200E* ).0lines     e/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/chat_loaders/whatsapp.py
<listcomp>z/WhatsAppChatLoader.__init__.<locals>.<listcomp>%   s    HHH4Z$.HHH    ))flagsz\\u200E*\[?(\d{1,2}/\d{1,2}/\d{2,4}, \d{1,2}:\d{2}:\d{2} (?:AM|PM))\]?[ \u200E]*([^:]+): (.+)N)r   recompilejoin
IGNORECASE_ignore_lines_message_line_regex)selfr   ignore_liness      r   __init__zWhatsAppChatLoader.__init__   s     	
 
 
  Z388HH<HHHIIIDP-
 
 
 $&:k-$
 $
 $
   r   	file_pathreturnc           
      $   t          |dd          5 }|                                }ddd           n# 1 swxY w Y   g }d}|                    d          D ]P}| j                            |          r|r|                    |           |}6|d|                                z   z  }Q|r|                    |           g }|D ]}| j                            |                                          }|rp|                                \  }	}
}| j                            |                                          s+|                    t          |
||
d|	igd	
                     t                              d|            t          |          S )zLoad a single chat session from a file.

        Args:
            file_path (str): Path to the chat file.

        Returns:
            ChatSession: The loaded chat session.
        rzutf-8)encodingN 
 message_time)senderevents)rolecontentadditional_kwargszCould not parse line: )messages)openreadsplitr   matchappendstripgroupsr   r	   loggerdebugr   )r   r"   filetxt
chat_linescurrent_messager   resultsresult	timestampr+   texts               r   _load_single_chat_sessionz,WhatsAppChatLoader._load_single_chat_session-   s    )S7333 	t))++C	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 !#
IIdOO 	6 	6D'--d33 6" 7%%o666"&3#55 	/o...8: 	> 	>D-33DJJLLAAF >*0--//'	64)//

== 
NN$!'$(*0,:I+F*G/ /  	 	 	 <d<<====G,,,,s   488c              #   R  K   t           j                            |           r| V  dS t           j                            |           rYt          j        |           D ]B\  }}}|D ]9}|                    d          r"t           j                            ||          V  :CdS t          j        |           rst          j	        |           5 }|
                                D ].}|                    d          r|                    |          V  /	 ddd           dS # 1 swxY w Y   dS dS )zIterate over the files in a directory or zip file.

        Args:
            path (str): Path to the directory or zip file.

        Yields:
            str: The path to each file.
        z.txtN)osr   isfileisdirwalkendswithr   zipfile
is_zipfileZipFilenamelistextract)r   root_filesr:   zip_files         r   _iterate_filesz!WhatsAppChatLoader._iterate_filesY   s      7>>$ 	5JJJJJW]]4   		5"$'$-- 7 7a! 7 7D}}V,, 7 gll46666677 7 %% 	5&& 5($--// 5 5D}}V,, 5&..t4444455 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5	5 	5s   ADD!Dc              #   B   K   |                      | j                  V  dS )zLazy load the messages from the chat file and yield
        them as chat sessions.

        Yields:
            Iterator[ChatSession]: The loaded chat sessions.
        N)rB   r   )r   s    r   	lazy_loadzWhatsAppChatLoader.lazy_loadp   s*       ,,TY7777777r   N)__name__
__module____qualname____doc__strr!   r   rB   staticmethodr   rR   rT   r   r   r   r   r      s        JJ
S 
 
 
 
8*-3 *-; *- *- *- *-X 5S 5Xc] 5 5 5 \5,88K0 8 8 8 8 8 8r   r   )loggingrD   r   rI   typingr   r   r   langchain_core.chat_loadersr   langchain_core.chat_sessionsr   langchain_core.messagesr   r	   	getLoggerrU   r8   r   r   r   r   <module>ra      s     				 				  ( ( ( ( ( ( ( ( ( ( 6 6 6 6 6 6 4 4 4 4 4 4 ; ; ; ; ; ; ; ;		8	$	$i8 i8 i8 i8 i8 i8 i8 i8 i8 i8r   