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,'.')
import praw
from praw.exceptions import PRAWException
from praw.models import MoreComments
from prawcore.exceptions import PrawcoreException
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')
def feed():
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):
if isinstance(i, MoreComments):
@ -40,6 +49,7 @@ def comment(i):
return c
def story(ref):
try:
r = reddit.submission(ref)
if not r: return False
@ -60,6 +70,13 @@ def story(ref):
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
if __name__ == '__main__':
print(feed())