diff --git a/apiserver/feed.py b/apiserver/feed.py index fa8dc38..5f42476 100644 --- a/apiserver/feed.py +++ b/apiserver/feed.py @@ -5,13 +5,14 @@ logging.basicConfig( import requests -from feeds import hackernews +from feeds import hackernews, reddit READ_API = 'http://127.0.0.1:33843' def list(): feed = [] feed += [(x, 'hackernews') for x in hackernews.feed()] + feed += [(x, 'reddit') for x in reddit.feed()] return feed def get_article(url): @@ -29,6 +30,8 @@ def update_story(story): if story['source'] == 'hackernews': res = hackernews.story(story['ref']) + elif story['source'] == 'reddit': + res = reddit.story(story['ref']) else: return diff --git a/apiserver/feeds/hackernews.py b/apiserver/feeds/hackernews.py index e1898b0..712f940 100644 --- a/apiserver/feeds/hackernews.py +++ b/apiserver/feeds/hackernews.py @@ -30,7 +30,6 @@ def comment(i): c['score'] = i.get('points', 0) c['date'] = i.get('created_at_i', 0) c['text'] = i.get('text', '') - c['link'] = SITE_LINK(i['id']) c['comments'] = [comment(j) for j in i['children']] return c @@ -64,4 +63,5 @@ def story(ref): return s if __name__ == '__main__': + print(feed()) print(story(20763961)) diff --git a/apiserver/feeds/reddit.py b/apiserver/feeds/reddit.py new file mode 100644 index 0000000..1a5ba5e --- /dev/null +++ b/apiserver/feeds/reddit.py @@ -0,0 +1,61 @@ +import logging +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) + +import praw +from praw.models import MoreComments + +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(): + return [x.id for x in reddit.subreddit(SUBREDDITS).hot(limit=30)] + +def good_comment(c): + if isinstance(c, MoreComments): + return False + if c.body == '[removed]': + return False + if c.author and c.author.name == 'AutoModerator': + return False + return True + +def comment(i): + c = {} + c['author'] = i.author.name if i.author else '[Deleted]' + c['score'] = i.score + c['date'] = i.created_utc + c['text'] = i.body.replace('\n', '
') + c['comments'] = [comment(j) for j in i.replies if good_comment(j)] + return c + +def story(ref): + 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 if good_comment(i)] + s['num_comments'] = r.num_comments + + if r.selftext: + s['text'] = r.selftext + + return s + +if __name__ == '__main__': + print(feed()) + print(reddit.submission(feed()[0]).permalink) + print() + print(story('cuozg4')) diff --git a/apiserver/praw.ini.example b/apiserver/praw.ini.example new file mode 100644 index 0000000..f80a46b --- /dev/null +++ b/apiserver/praw.ini.example @@ -0,0 +1,4 @@ +[bot] +client_id= +client_secret= +user_agent=