File size: 1,905 Bytes
915892a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71eae46
 
 
 
915892a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import praw
import logging
import pandas as pd
from typing import Generator
from dotenv import load_dotenv

load_dotenv()
logger = logging.getLogger(__name__)

class RedditBot:
    def __init__(
        self, 
        client_id: str | None = None, 
        client_secret: str | None = None, 
        username: str | None = None, 
        password: str | None = None
    ) -> None:

        self.reddit = praw.Reddit(
            client_id = client_id if client_id else os.getenv('REDDIT_CLIENT_ID'), 
            client_secret = client_secret if client_secret else os.getenv('REDDIT_CLIENT_SECRET'), 
            username = username if username else os.getenv('REDDIT_USERNAME'), 
            password = password if password else os.getenv('REDDIT_PASSWORD'), 
            user_agent='bot'
    )

    def get_subreddits_posts(self, name: str, type: str, amount=100) -> Generator:
        """Gets the posts from a given subreddit"""
        subreddit = self.reddit.subreddit(name)
        if type == 'new':
            posts = subreddit.new(limit=amount)
        elif type == 'hot':
            posts = subreddit.hot(limit=amount)
        elif type == 'top':
            posts = subreddit.top(limit=amount)
        elif type == 'rising':
            posts = subreddit.rising(limit=amount)

        return posts 

    @staticmethod
    def convert_posts_to_df(posts: Generator) -> pd.DataFrame:
        """Extracts the title and text from a post"""
        df = pd.DataFrame(columns=['Title', 'Content'])
        for n, p in enumerate(posts):
            df.loc[n, 'Title'] = p.title
            df.loc[n, 'Content'] = p.selftext
        
        return df

    def subreddit_exists(self, name: str) -> bool:
        try:
            self.reddit.subreddits.search_by_name(name, exact=True)
            return True
        except Exception as e:
            logger.error(e)
            return False