
    Ng                    v    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 erd dlZddZ G d	 d
e          ZdS )    )annotations)TYPE_CHECKINGIterableListOptionalSequence)Document)
BaseLoaderNreturnprawc                 L    	 dd l } n# t          $ r t          d          w xY w| S )Nr   zApraw package not found, please install it with `pip install praw`)r   ImportError)r   s    g/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/reddit.py_dependable_praw_importr      sI    
 
 
 
O
 
 	

 Ks    !c                  :    e Zd ZdZdgdfddZddZddZddZdS )RedditPostsLoaderzLoad `Reddit` posts.

    Read posts on a subreddit.
    First, you need to go to
    https://www.reddit.com/prefs/apps/
    and create your application
    new
   	client_idstrclient_secret
user_agentsearch_queriesSequence[str]mode
categoriesnumber_postsOptional[int]c                h    || _         || _        || _        || _        || _        || _        || _        dS )a  
        Initialize with client_id, client_secret, user_agent, search_queries, mode,
            categories, number_posts.
        Example: https://www.reddit.com/r/learnpython/

        Args:
            client_id: Reddit client id.
            client_secret: Reddit client secret.
            user_agent: Reddit user agent.
            search_queries: The search queries.
            mode: The mode.
            categories: The categories. Default: ["new"]
            number_posts: The number of posts. Default: 10
        N)r   r   r   r   r   r   r   )selfr   r   r   r   r   r   r   s           r   __init__zRedditPostsLoader.__init__    s>    0 #*$,	$(    r   List[Document]c                   t                      } |j        | j        | j        | j                  }g }| j        dk    rB| j        D ]9}| j        D ]/}|                     |||          }|	                    |           0:n\| j        dk    rB| j        D ]9}| j        D ]/}| 
                    |||          }|	                    |           0:nt          d          |S )zLoad reddits.)r   r   r   	subreddit)search_querycategoryredditusernamez@mode not correct, please enter 'username' or 'subreddit' as mode)r   Redditr   r   r   r   r   r   _subreddit_posts_loaderextend_user_posts_loader
ValueError)r    r   r(   resultsr&   r'   docss          r   loadzRedditPostsLoader.load@   s@   &((n,
 
 
 #%9## $ 3 ) ) $ ) )H77%1HV 8  D NN4((((	)) Y*$$ $ 3 ) ) $ ) )H22%1HV 3  D NN4((((	)) R   r"   r&   r'   r(   praw.reddit.RedditIterable[Document]c           	   #    K   |                     |          }t          ||          } || j                  }	 |D ]B}|j        ||j        |j        |j        |j        |j        d}t          |j
        |          V  Cd S N)limit)post_subredditpost_category
post_title
post_scorepost_idpost_urlpost_author)page_contentmetadata)r%   getattrr   subreddit_name_prefixedtitlescoreidurlauthorr	   selftext)	r    r&   r'   r(   r%   method	cat_postspostr?   s	            r   r+   z)RedditPostsLoader._subreddit_posts_loaderc   s       $$\22	H--F!2333	0 	 	D"&">!)"j"j7 H#{ H !]!      	 	r"   c           	   #    K   |                     |          }t          |j        |          } || j                  }	 |D ]B}|j        ||j        |j        |j        |j        |j	        d}t          |j        |          V  Cd S r5   )redditorr@   submissionsr   rA   rB   rC   rD   rE   rF   r	   rG   )	r    r&   r'   r(   userrH   rI   rJ   r?   s	            r   r-   z$RedditPostsLoader._user_posts_loaderz   s       |,,)844F!2333	0 	 	D"&">!)"j"j7 H#{ H !]!      	 	r"   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r#   )r&   r   r'   r   r(   r2   r   r3   )__name__
__module____qualname____doc__r!   r1   r+   r-    r"   r   r   r      s          &+G&() ) ) ) )@! ! ! !F   .     r"   r   )r   r   )
__future__r   typingr   r   r   r   r   langchain_core.documentsr	   )langchain_community.document_loaders.baser
   r   r   r   rS   r"   r   <module>rX      s    " " " " " " D D D D D D D D D D D D D D - - - - - - @ @ @ @ @ @ KKK   x x x x x
 x x x x xr"   