noranisa's picture
Update services/reddit.py
e56b60e verified
raw
history blame contribute delete
897 Bytes
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