a
    bg                     @  sn   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rLd dlZdddd	ZG d
d deZdS )    )annotations)TYPE_CHECKINGIterableListOptionalSequence)Document)
BaseLoaderNprawreturnc                  C  s,   zdd l } W n ty&   tdY n0 | S )Nr   zApraw package not found, please install it with `pip install praw`)r
   ImportError)r
    r   y/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/reddit.py_dependable_praw_import   s    
r   c                	   @  sh   e Zd ZdZdgdfdddddddddd	Zd
dddZdddddddZdddd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
   strzSequence[str]zOptional[int]	client_idclient_secret
user_agentsearch_queriesmode
categoriesnumber_postsc                 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
        Nr   )selfr   r   r   r   r   r   r   r   r   r   __init__    s    zRedditPostsLoader.__init__zList[Document]r   c                 C  s   t  }|j| j| j| jd}g }| jdkr^| jD ]*}| jD ]}| j|||d}|	| q:q0nF| jdkr| jD ]*}| jD ]}| j
|||d}|	| qxqnn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   ZRedditr   r   r   r   r   r   _subreddit_posts_loaderextend_user_posts_loader
ValueError)r   r
   r"   resultsr    r!   docsr   r   r   load@   s2    





zRedditPostsLoader.loadzpraw.reddit.RedditzIterable[Document])r    r!   r"   r   c           	   	   c  s^   | |}t||}|| jd}|D ]4}|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postr-   r   r   r   r$   c   s     

	z)RedditPostsLoader._subreddit_posts_loaderc           	   	   c  s`   | |}t|j|}|| jd}|D ]4}|j||j|j|j|j|j	d}t
|j|dV  q&d S r+   )Zredditorr.   Zsubmissionsr   r/   r0   r1   r2   r3   r4   r   r5   )	r   r    r!   r"   userr6   r7   r8   r-   r   r   r   r&   z   s     
	z$RedditPostsLoader._user_posts_loaderN)__name__
__module____qualname____doc__r   r*   r$   r&   r   r   r   r   r      s    #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   
