o
    Zh                     @  sj   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d	ZG d
d deZdS )    )annotations)TYPE_CHECKINGIterableListOptionalSequence)Document)
BaseLoaderNreturnprawc                  C  s&   zdd l } W | S  ty   tdw )Nr   zApraw package not found, please install it with `pip install praw`)r   ImportError)r    r   b/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/reddit.py_dependable_praw_import   s   
r   c                   @  s@   e Zd ZdZdgdfdddZdddZd ddZd 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                 C  s.   || _ || _|| _|| _|| _|| _|| _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   r   r   r   __init__    s   
zRedditPostsLoader.__init__r
   List[Document]c                 C  s   t  }|j| j| j| jd}g }| jdkr0| jD ]}| jD ]}| j|||d}|	| qq|S | jdkrP| jD ]}| jD ]}| j
|||d}|	| q=q8|S td)zLoad reddits.)r   r   r   	subreddit)search_querycategoryredditusernamez@mode not correct, please enter 'username' or 'subreddit' as mode)r   ZRedditr   r   r   r   r   r   _subreddit_posts_loaderextend_user_posts_loader
ValueError)r   r   r#   resultsr!   r"   docsr   r   r   load@   s8   





zRedditPostsLoader.loadr!   r"   r#   praw.reddit.RedditIterable[Document]c           	   	   c  sb    | |}t||}|| jd}	 |D ]}|j||j|j|j|j|jd}t	|j
|dV  qd S N)limit)Zpost_subredditZpost_categoryZ
post_titleZ
post_scoreZpost_idZpost_urlZpost_author)Zpage_contentmetadata)r    getattrr   subreddit_name_prefixedtitlescoreidurlauthorr   selftext)	r   r!   r"   r#   r    method	cat_postspostr0   r   r   r   r%   c   s&   

	
z)RedditPostsLoader._subreddit_posts_loaderc           	   	   c  sd    | |}t|j|}|| jd}	 |D ]}|j||j|j|j|j|j	d}t
|j|dV  qd S r.   )Zredditorr1   Zsubmissionsr   r2   r3   r4   r5   r6   r7   r   r8   )	r   r!   r"   r#   userr9   r:   r;   r0   r   r   r   r'   z   s&   
	
z$RedditPostsLoader._user_posts_loaderN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r
   r   )r!   r   r"   r   r#   r,   r
   r-   )__name__
__module____qualname____doc__r   r+   r%   r'   r   r   r   r   r      s    
 
#r   )r
   r   )
__future__r   typingr   r   r   r   r   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser	   r   r   r   r   r   r   r   <module>   s    

