Handle Reddit PRAW exceptions
This commit is contained in:
parent
2ede5ed6ff
commit
0a1ebaa8b8
|
@ -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():
|
||||
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):
|
||||
if isinstance(i, MoreComments):
|
||||
|
@ -40,25 +49,33 @@ def comment(i):
|
|||
return c
|
||||
|
||||
def story(ref):
|
||||
r = reddit.submission(ref)
|
||||
if not r: return False
|
||||
try:
|
||||
r = reddit.submission(ref)
|
||||
if not r: return False
|
||||
|
||||
s = {}
|
||||
s['author'] = r.author.name if r.author else '[Deleted]'
|
||||
s['author_link'] = SITE_AUTHOR_LINK(r.author)
|
||||
s['score'] = r.score
|
||||
s['date'] = r.created_utc
|
||||
s['title'] = r.title
|
||||
s['link'] = SITE_LINK(r.permalink)
|
||||
s['url'] = r.url
|
||||
s['comments'] = [comment(i) for i in r.comments]
|
||||
s['comments'] = list(filter(bool, s['comments']))
|
||||
s['num_comments'] = r.num_comments
|
||||
s = {}
|
||||
s['author'] = r.author.name if r.author else '[Deleted]'
|
||||
s['author_link'] = SITE_AUTHOR_LINK(r.author)
|
||||
s['score'] = r.score
|
||||
s['date'] = r.created_utc
|
||||
s['title'] = r.title
|
||||
s['link'] = SITE_LINK(r.permalink)
|
||||
s['url'] = r.url
|
||||
s['comments'] = [comment(i) for i in r.comments]
|
||||
s['comments'] = list(filter(bool, s['comments']))
|
||||
s['num_comments'] = r.num_comments
|
||||
|
||||
if r.selftext:
|
||||
s['text'] = render_md(r.selftext)
|
||||
if 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
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue
Block a user