qotnews/apiserver/feeds/reddit.py

86 lines
2.5 KiB
Python
Raw Normal View History

2019-08-24 21:37:43 +00:00
import logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG)
if __name__ == '__main__':
import sys
sys.path.insert(0,'.')
2019-08-24 21:37:43 +00:00
import praw
2019-09-24 08:20:46 +00:00
from praw.exceptions import PRAWException
2019-08-24 21:37:43 +00:00
from praw.models import MoreComments
2019-09-24 08:20:46 +00:00
from prawcore.exceptions import PrawcoreException
2019-08-24 21:37:43 +00:00
from utils import render_md
2019-08-24 21:37:43 +00:00
SUBREDDITS = 'Economics+Foodforthought+Futurology+TrueReddit+business+science+technology'
SITE_LINK = lambda x : 'https://old.reddit.com/{}'.format(x)
SITE_AUTHOR_LINK = lambda x : 'https://old.reddit.com/u/{}'.format(x)
reddit = praw.Reddit('bot')
def feed():
2019-09-24 08:20:46 +00:00
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 []
2019-08-24 21:37:43 +00:00
2019-08-30 06:23:14 +00:00
def comment(i):
if isinstance(i, MoreComments):
2019-08-24 21:37:43 +00:00
return False
2019-08-30 06:23:14 +00:00
if '[removed]' in i.body or '[deleted]' in i.body:
2019-08-24 21:37:43 +00:00
return False
2019-08-30 06:23:14 +00:00
if i.author and i.author.name == 'AutoModerator':
2019-08-24 21:37:43 +00:00
return False
c = {}
c['author'] = i.author.name if i.author else '[Deleted]'
c['score'] = i.score
c['date'] = i.created_utc
c['text'] = render_md(i.body)
2019-08-30 06:23:14 +00:00
c['comments'] = [comment(j) for j in i.replies]
c['comments'] = list(filter(bool, c['comments']))
2019-08-24 21:37:43 +00:00
return c
def story(ref):
2019-09-24 08:20:46 +00:00
try:
r = reddit.submission(ref)
if not r: return False
2019-08-24 21:37:43 +00:00
2019-09-24 08:20:46 +00:00
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
2019-08-24 21:37:43 +00:00
2019-09-24 08:20:46 +00:00
if r.selftext:
s['text'] = render_md(r.selftext)
2019-08-24 21:37:43 +00:00
2019-09-24 08:20:46 +00:00
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
2019-08-24 21:37:43 +00:00
# scratchpad so I can quickly develop the parser
2019-08-24 21:37:43 +00:00
if __name__ == '__main__':
print(feed())
print(reddit.submission(feed()[0]).permalink)
print()
print(story('cuozg4'))