File size: 897 Bytes
abc372c
e56b60e
abc372c
5458d61
 
e56b60e
5458d61
 
e56b60e
 
 
 
5458d61
e56b60e
5458d61
 
 
abc372c
5458d61
 
 
 
 
 
e56b60e
 
 
 
 
5458d61
 
e56b60e
 
abc372c
5458d61
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
import praw
from config import REDDIT_CLIENT_ID, REDDIT_CLIENT_SECRET, REDDIT_USER_AGENT

def init_reddit():
    try:
        if not REDDIT_CLIENT_ID:
            return None

        return praw.Reddit(
            client_id=REDDIT_CLIENT_ID,
            client_secret=REDDIT_CLIENT_SECRET,
            user_agent=REDDIT_USER_AGENT
        )
    except:
        return None

def get_reddit_comments(keyword, limit=30):
    comments = []
    reddit = init_reddit()

    if reddit is None:
        return comments

    try:
        for submission in reddit.subreddit("all").search(keyword, limit=2):
            submission.comments.replace_more(limit=0)
            for c in submission.comments.list():
                if len(c.body) > 10:
                    comments.append(c.body)
                if len(comments) >= limit:
                    break
    except:
        pass

    return comments