From db6cd3044d9987d307a5a4ce932447be0dba0431 Mon Sep 17 00:00:00 2001 From: davy Date: Thu, 23 Nov 2017 16:18:43 -0600 Subject: [PATCH 1/7] Added routes --- routes/__init__.py | 11 +++++++++++ run.py | 1 + 2 files changed, 12 insertions(+) create mode 100644 routes/__init__.py diff --git a/routes/__init__.py b/routes/__init__.py new file mode 100644 index 0000000..81667cf --- /dev/null +++ b/routes/__init__.py @@ -0,0 +1,11 @@ +from flask import redirect, request +from app import app + +@app.route('/test', methods=['GET']) +def test_get(): + return '
' + +@app.route('/test', methods=['POST']) +def test_post(): + username = request.form.get('username', '???') + return 'Hello ' + username \ No newline at end of file diff --git a/run.py b/run.py index 394ab0d..5347072 100644 --- a/run.py +++ b/run.py @@ -1,6 +1,7 @@ from app import app from db import db from models import article, source +import routes with app.app_context(): db.create_all() From 35fc7f0f400651efd02663fa3078d62459aa999d Mon Sep 17 00:00:00 2001 From: davy Date: Fri, 24 Nov 2017 10:55:09 -0600 Subject: [PATCH 2/7] Implemented handlers --- models/article.py | 2 +- routes/__init__.py | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/models/article.py b/models/article.py index e8c95d0..463ccc0 100644 --- a/models/article.py +++ b/models/article.py @@ -9,7 +9,7 @@ class Article(db.Model): guid = db.Column(db.String(255), nullable=False) unread = db.Column(db.Boolean, default=True, nullable=False) source_id = db.Column(db.Integer, db.ForeignKey('source.id'), nullable=False) - source = db.relationship('Source', db.backref('articles', lazy=True)) + source = db.relationship('Source', backref=db.backref('articles', lazy=True)) date_added = db.Column(db.DateTime, default=datetime.datetime.utcnow) date_published = db.Column(db.DateTime) __table_args__ = ( diff --git a/routes/__init__.py b/routes/__init__.py index 81667cf..eb48aa1 100644 --- a/routes/__init__.py +++ b/routes/__init__.py @@ -1,11 +1,36 @@ -from flask import redirect, request +from flask import abort, redirect, request from app import app +from db import db +from models.article import Article +from models.source import Source +import feed -@app.route('/test', methods=['GET']) -def test_get(): - return '
' +@app.route('/', methods=['GET']) +def index_get(): + query = Article.query + query = query.filter(Article.unread == True) + query = query.order_by(Article.date_added.desc()) + articles = query.all() + return str([article.title for article in articles]) -@app.route('/test', methods=['POST']) -def test_post(): - username = request.form.get('username', '???') - return 'Hello ' + username \ No newline at end of file +@app.route('/read/', methods=['GET']) +def read_article_get(article_id): + article = Article.query.get(article_id) + article.unread = False + db.session.commit() + return redirect(article.link) + +@app.route('/sources', methods=['GET']) +def sources_get(): + query = Source.query + query = query.order_by(Source.title) + sources = query.all() + return str([source.title for source in sources]) + +@app.route('/sources', methods=['POST']) +def sources_post(): + feed_url = request.form['feed'] + parsed = feed.parse(feed_url) + feed_source = feed.get_source(parsed) + source = Source.insert_from_feed(feed_url, feed_source) + return redirect('/sources') From 9abafeae5c023438eab0e6479cfde2bfd03198dc Mon Sep 17 00:00:00 2001 From: davy Date: Sat, 25 Nov 2017 12:49:36 -0600 Subject: [PATCH 3/7] Added templates --- routes/__init__.py | 6 +++--- templates/index.html | 29 +++++++++++++++++++++++++++++ templates/sources.html | 16 ++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 templates/index.html create mode 100644 templates/sources.html diff --git a/routes/__init__.py b/routes/__init__.py index eb48aa1..d2e9b15 100644 --- a/routes/__init__.py +++ b/routes/__init__.py @@ -1,4 +1,4 @@ -from flask import abort, redirect, request +from flask import abort, redirect, request, render_template from app import app from db import db from models.article import Article @@ -11,7 +11,7 @@ def index_get(): query = query.filter(Article.unread == True) query = query.order_by(Article.date_added.desc()) articles = query.all() - return str([article.title for article in articles]) + return render_template('index.html', articles=articles) @app.route('/read/', methods=['GET']) def read_article_get(article_id): @@ -25,7 +25,7 @@ def sources_get(): query = Source.query query = query.order_by(Source.title) sources = query.all() - return str([source.title for source in sources]) + return render_template('sources.html', sources=sources) @app.route('/sources', methods=['POST']) def sources_post(): diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..51791f4 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,29 @@ + + + Latest News + + +
+

Latest News

+ {% for article in articles %} + + {% endfor %} +
+ + \ No newline at end of file diff --git a/templates/sources.html b/templates/sources.html new file mode 100644 index 0000000..080a4aa --- /dev/null +++ b/templates/sources.html @@ -0,0 +1,16 @@ + + + Sources + + +
+ + +
+ {% for source in sources %} + + {% endfor %} + + \ No newline at end of file From df3ddd45adf3406ee576f0829a05f7d672db5e90 Mon Sep 17 00:00:00 2001 From: davy Date: Wed, 29 Nov 2017 08:57:26 -0600 Subject: [PATCH 4/7] Added css --- static/css/index.css | 53 ++++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 1 + 2 files changed, 54 insertions(+) create mode 100644 static/css/index.css 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 +
From 5caccf8a87cd1e09a9f2ebb769e494b4155410d2 Mon Sep 17 00:00:00 2001 From: davy Date: Fri, 1 Dec 2017 11:04:16 -0600 Subject: [PATCH 5/7] Added update loop --- run.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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() From 5bb4c7d3ce4b5af139a39ef20c5a6b775784c85a Mon Sep 17 00:00:00 2001 From: davy Date: Tue, 5 Dec 2017 12:14:01 -0600 Subject: [PATCH 6/7] Added orderby and preloaded articles --- routes/__init__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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') From b910d211ba4de079a5a7878a7cf89fa37b727bc9 Mon Sep 17 00:00:00 2001 From: davy wybiral Date: Mon, 23 Mar 2020 15:41:01 -0500 Subject: [PATCH 7/7] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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