diff --git a/README.md b/README.md index 51668d0..d84d40b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # python-feedreader Building an RSS feed reader in Python. + +This is supplementary code for this tutorial series: https://www.youtube.com/playlist?list=PLmxT2pVYo5LBcv5nYKTIn-fblphtD_OJO diff --git a/routes/__init__.py b/routes/__init__.py index d2e9b15..8a8063c 100644 --- a/routes/__init__.py +++ b/routes/__init__.py @@ -9,7 +9,15 @@ def index_get(): query = Article.query query = query.filter(Article.unread == True) - query = query.order_by(Article.date_added.desc()) + orderby = request.args.get('orderby', 'added') + if orderby == 'added': + query = query.order_by(Article.date_added.desc()) + elif orderby == 'published': + query = query.order_by(Article.date_published.desc()) + elif orderby == 'title': + query = query.order_by(Article.title) + elif orderby == 'source': + query = query.join(Source).order_by(Source.title) articles = query.all() return render_template('index.html', articles=articles) @@ -33,4 +41,6 @@ def sources_post(): parsed = feed.parse(feed_url) feed_source = feed.get_source(parsed) source = Source.insert_from_feed(feed_url, feed_source) + feed_articles = feed.get_articles(parsed) + Article.insert_from_feed(source.id, feed_articles) return redirect('/sources') diff --git a/run.py b/run.py index 5347072..7b85aca 100644 --- a/run.py +++ b/run.py @@ -2,8 +2,31 @@ from db import db from models import article, source import routes +import feed +from threading import Thread +import time with app.app_context(): db.create_all() +def update_loop(): + while True: + with app.app_context(): + query = source.Source.query + for src in query.all(): + try: + update_source(src) + except: + continue + time.sleep(60 * 15) + +def update_source(src): + parsed = feed.parse(src.feed) + feed_articles = feed.get_articles(parsed) + article.Article.insert_from_feed(src.id, feed_articles) + print('Updated ' + src.feed) + +thread = Thread(target=update_loop) +thread.start() + app.run() diff --git a/static/css/index.css b/static/css/index.css new file mode 100644 index 0000000..57858e5 --- /dev/null +++ b/static/css/index.css @@ -0,0 +1,53 @@ +* { + margin: 0; + padding: 0; +} + +body { + color: #333; + background: #f1f1f1; + font-family: Helvetica, Arial, sans-serif; +} + +main { + box-sizing: border-box; + padding: 0.5em; + margin: 0 auto; + width: 100%; + max-width: 640px; +} + +main > h1 { + font-size: 20px; + margin: 0.5em 0; +} + +article { + font-size: 14px; + padding: 0.5em; + background: #fff; + box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); +} + +article + article { + margin-top: 0.5em; +} + +article > h1 { + font-size: 18px; +} + +article > h1 > a { + color: inherit; + text-decoration: none; +} + +article > .added { + margin-top: 0.25em; + color: #777; +} + +article > .body { + margin-top: 0.5em; + line-height: 1.3em; +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 51791f4..f6e6c62 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,6 +1,7 @@ Latest News +