Handle Reddit PRAW exceptions

This commit is contained in:
Tanner Collin 2019-09-24 08:20:46 +00:00
parent 2ede5ed6ff
commit 0a1ebaa8b8

View File

@ -8,7 +8,9 @@ if __name__ == '__main__':
sys.path.insert(0,'.') sys.path.insert(0,'.')
import praw import praw
from praw.exceptions import PRAWException
from praw.models import MoreComments from praw.models import MoreComments
from prawcore.exceptions import PrawcoreException
from utils import render_md from utils import render_md
@ -20,7 +22,14 @@ SITE_AUTHOR_LINK = lambda x : 'https://old.reddit.com/u/{}'.format(x)
reddit = praw.Reddit('bot') reddit = praw.Reddit('bot')
def feed(): def feed():
return [x.id for x in reddit.subreddit(SUBREDDITS).hot()] try:
return [x.id for x in reddit.subreddit(SUBREDDITS).hot()]
except PRAWException as e:
logging.error('Problem hitting reddit API: {}'.format(str(e)))
return []
except PrawcoreException as e:
logging.error('Problem hitting reddit API: {}'.format(str(e)))
return []
def comment(i): def comment(i):
if isinstance(i, MoreComments): if isinstance(i, MoreComments):
@ -40,25 +49,33 @@ def comment(i):
return c return c
def story(ref): def story(ref):
r = reddit.submission(ref) try:
if not r: return False r = reddit.submission(ref)
if not r: return False
s = {} s = {}
s['author'] = r.author.name if r.author else '[Deleted]' s['author'] = r.author.name if r.author else '[Deleted]'
s['author_link'] = SITE_AUTHOR_LINK(r.author) s['author_link'] = SITE_AUTHOR_LINK(r.author)
s['score'] = r.score s['score'] = r.score
s['date'] = r.created_utc s['date'] = r.created_utc
s['title'] = r.title s['title'] = r.title
s['link'] = SITE_LINK(r.permalink) s['link'] = SITE_LINK(r.permalink)
s['url'] = r.url s['url'] = r.url
s['comments'] = [comment(i) for i in r.comments] s['comments'] = [comment(i) for i in r.comments]
s['comments'] = list(filter(bool, s['comments'])) s['comments'] = list(filter(bool, s['comments']))
s['num_comments'] = r.num_comments s['num_comments'] = r.num_comments
if r.selftext: if r.selftext:
s['text'] = render_md(r.selftext) s['text'] = render_md(r.selftext)
return s return s
except PRAWException as e:
logging.error('Problem hitting reddit API: {}'.format(str(e)))
return False
except PrawcoreException as e:
logging.error('Problem hitting reddit API: {}'.format(str(e)))
return False
# scratchpad so I can quickly develop the parser # scratchpad so I can quickly develop the parser
if __name__ == '__main__': if __name__ == '__main__':