
    ih                    n    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y)
    )annotations)TYPE_CHECKINGIterableListOptionalSequence)Document)
BaseLoaderNc                 @    	 dd l } | S # t        $ r t        d      w xY w)Nr   zApraw package not found, please install it with `pip install praw`)prawImportError)r   s    i/var/www/html/dev/engine/venv/lib/python3.12/site-packages/langchain_community/document_loaders/reddit.py_dependable_praw_importr      s2    

 K	  
O
 	

s    c                  r    e Zd ZdZdgdf	 	 	 	 	 	 	 	 	 	 	 	 	 d	dZd
dZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZy)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
   c                f    || _         || _        || _        || _        || _        || _        || _        y)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)	client_idclient_secret
user_agentsearch_queriesmode
categoriesnumber_posts)selfr   r   r   r   r   r   r   s           r   __init__zRedditPostsLoader.__init__    s8    0 #*$,	$(    c                   t               } |j                  | j                  | j                  | j                        }g }| j
                  dk(  rI| j                  D ]8  }| j                  D ]'  }| j                  |||      }|j                  |       ) : |S | j
                  dk(  rI| j                  D ]8  }| j                  D ]'  }| j                  |||      }|j                  |       ) : |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   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   &(nn,,
 #%99# $ 3 3 ) $ )H77%1HV 8 D NN4(	))(  YY*$ $ 3 3 ) $ )H22%1HV 3 D NN4(	)) 	 R r   c           	   #  :  K   |j                  |      }t        ||      } || j                        }	 |D ]a  }|j                  ||j                  |j
                  |j                  |j                  |j                  d}t        |j                  |       c y w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postr8   s	            r   r&   z)RedditPostsLoader._subreddit_posts_loaderc   s      $$\2	H-!2!23	0 	D"&">">!)"jj"jj77 HH#{{H !]]! 	s   BBc           	   #  N  K   |j                  |      }t        |j                  |      } || j                        }	 |D ]a  }|j                  ||j
                  |j                  |j                  |j                  |j                  d}t        |j                  |       c y wr.   )redditorr9   submissionsr   r:   r;   r<   r=   r>   r?   r	   r@   )	r   r!   r"   r#   userrA   rB   rC   r8   s	            r   r(   z$RedditPostsLoader._user_posts_loaderz   s      |,))84!2!23	0 	D"&">">!)"jj"jj77 HH#{{H !]]! 	s   B#B%N)r   strr   rH   r   rH   r   Sequence[str]r   rH   r   rI   r   zOptional[int])returnzList[Document])r!   rH   r"   rH   r#   zpraw.reddit.RedditrJ   zIterable[Document])__name__
__module____qualname____doc__r   r,   r&   r(    r   r   r   r      s     &+G&()) ) 	)
 &) ) ") $)@!F+.8J	.+.8J	r   r   )rJ   r   )
__future__r   typingr   r   r   r   r   langchain_core.documentsr	   )langchain_community.document_loaders.baser
   r   r   r   rO   r   r   <module>rT      s-    " D D - @x
 xr   