From ca78a6d7a94fc3367e507877050047716572a44e Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Mon, 2 Nov 2020 02:26:54 +0000 Subject: [PATCH] Move feed and Praw config to settings.py --- README.md | 12 +++++------ apiserver/.gitignore | 2 +- apiserver/feed.py | 13 +++++++++--- apiserver/feeds/reddit.py | 14 ++++++++---- apiserver/praw.ini.example | 4 ---- apiserver/settings.py.example | 40 +++++++++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 19 deletions(-) delete mode 100644 apiserver/praw.ini.example create mode 100644 apiserver/settings.py.example diff --git a/README.md b/README.md index 538681f..e703217 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ $ source env/bin/activate (env) $ pip install -r requirements.txt ``` -Configure Praw for your Reddit account: +Configure Praw for your Reddit account (optional): * Go to https://www.reddit.com/prefs/apps * Click "Create app" @@ -44,16 +44,14 @@ Configure Praw for your Reddit account: * Description: blank * About URL: blank * Redirect URL: your GitHub profile -* Submit, copy the client ID and client secret into `praw.ini`: +* Submit, copy the client ID and client secret into `settings.py` below ```text -(env) $ vim praw.ini -[bot] -client_id=paste here -client_secret=paste here -user_agent=script by github/your-username-here +(env) $ vim settings.py.example ``` +Edit it and save it as `settings.py`. + Now you can run the server: ```text diff --git a/apiserver/.gitignore b/apiserver/.gitignore index 8f7cd99..23abd96 100644 --- a/apiserver/.gitignore +++ b/apiserver/.gitignore @@ -105,7 +105,7 @@ ENV/ # DB db.sqlite3 -praw.ini +settings.py data.db data.db.bak data/archive/* diff --git a/apiserver/feed.py b/apiserver/feed.py index 60db037..c746f0c 100644 --- a/apiserver/feed.py +++ b/apiserver/feed.py @@ -7,6 +7,7 @@ import requests import time from bs4 import BeautifulSoup +import settings from feeds import hackernews, reddit, tildes, manual OUTLINE_API = 'https://api.outline.com/v3/parse_article' @@ -17,9 +18,15 @@ TWO_DAYS = 60*60*24*2 def list(): feed = [] - feed += [(x, 'hackernews') for x in hackernews.feed()[:15]] - feed += [(x, 'reddit') for x in reddit.feed()[:10]] - feed += [(x, 'tildes') for x in tildes.feed()[:5]] + if settings.NUM_HACKERNEWS: + feed += [(x, 'hackernews') for x in hackernews.feed()[:settings.NUM_HACKERNEWS]] + + if settings.NUM_REDDIT: + feed += [(x, 'reddit') for x in reddit.feed()[:settings.NUM_REDDIT]] + + if settings.NUM_TILDES: + feed += [(x, 'tildes') for x in tildes.feed()[:settings.NUM_TILDES]] + return feed def get_article(url): diff --git a/apiserver/feeds/reddit.py b/apiserver/feeds/reddit.py index 9b754cd..3268066 100644 --- a/apiserver/feeds/reddit.py +++ b/apiserver/feeds/reddit.py @@ -12,18 +12,24 @@ from praw.exceptions import PRAWException from praw.models import MoreComments from prawcore.exceptions import PrawcoreException +import settings from utils import render_md, clean -SUBREDDITS = 'Economics+AcademicPhilosophy+DepthHub+Foodforthought+HistoryofIdeas+LaymanJournals+PhilosophyofScience+PoliticsPDFs+Scholar+StateOfTheUnion+TheAgora+TrueFilm+TrueReddit+UniversityofReddit+culturalstudies+hardscience+indepthsports+indepthstories+ludology+neurophilosophy+resilientcommunities+worldevents' - 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') +if settings.NUM_REDDIT: + reddit = praw.Reddit( + client_id=settings.REDDIT_CLIENT_ID, + client_secret=settings.REDDIT_CLIENT_SECRET, + user_agent=settings.REDDIT_USER_AGENT, + ) + + subs = '+'.join(settings.SUBREDDITS) def feed(): try: - return [x.id for x in reddit.subreddit(SUBREDDITS).hot()] + return [x.id for x in reddit.subreddit(subs).hot()] except KeyboardInterrupt: raise except PRAWException as e: diff --git a/apiserver/praw.ini.example b/apiserver/praw.ini.example deleted file mode 100644 index f80a46b..0000000 --- a/apiserver/praw.ini.example +++ /dev/null @@ -1,4 +0,0 @@ -[bot] -client_id= -client_secret= -user_agent= diff --git a/apiserver/settings.py.example b/apiserver/settings.py.example new file mode 100644 index 0000000..26f4fd6 --- /dev/null +++ b/apiserver/settings.py.example @@ -0,0 +1,40 @@ +# QotNews settings +# edit this file and save it as settings.py + +# Feed Lengths +# Number of top items from each site to pull +# set to 0 to disable that site +NUM_HACKERNEWS = 15 +NUM_REDDIT = 10 +NUM_TILDES = 5 + +# Reddit account info +# leave blank if not using Reddit +REDDIT_CLIENT_ID = '' +REDDIT_CLIENT_SECRET = '' +REDDIT_USER_AGENT = '' + +SUBREDDITS = [ + 'Economics', + 'AcademicPhilosophy', + 'DepthHub', + 'Foodforthought', + 'HistoryofIdeas', + 'LaymanJournals', + 'PhilosophyofScience', + 'PoliticsPDFs', + 'Scholar', + 'StateOfTheUnion', + 'TheAgora', + 'TrueFilm', + 'TrueReddit', + 'UniversityofReddit', + 'culturalstudies', + 'hardscience', + 'indepthsports', + 'indepthstories', + 'ludology', + 'neurophilosophy', + 'resilientcommunities', + 'worldevents', +]