diff --git a/feedtools.py b/feedtools.py index 0e3dfc7..536648e 100644 --- a/feedtools.py +++ b/feedtools.py @@ -3,80 +3,99 @@ from simpledatabase import SimpleDatabase import json from datetime import date, timedelta -def load(): - """ Load all feeds """ +def update(): + """ Update all feeds """ feeds = open('feeds.txt').readlines() db = SimpleDatabase('feeds.json', 'feeds.log') tmp = {} + tmp['feeds'] = {} + tmp['all_posts_sorted'] = {} + for x, feed in enumerate(feeds): parsed = feedparser.parse(feed) x = str(x) - tmp[x] = {} - tmp[x]['title'] = parsed.feed.title - tmp[x]['link'] = parsed.feed.link - tmp[x]['description'] = parsed.feed.description - tmp[x]['entries'] = parsed.entries + + print(parsed) + + tmp['feeds'][x] = {} + tmp['feeds'][x]['title'] = parsed.feed.title + tmp['feeds'][x]['link'] = parsed.feed.link + tmp['feeds'][x]['description'] = parsed.feed.description + + for post in parsed.entries: + year = post['published_parsed'][0] + month = post['published_parsed'][1] + day = post['published_parsed'][2] + post_date = date(year, month, day) + + if not str(post_date) in tmp['all_posts_sorted']: + tmp['all_posts_sorted'][str(post_date)] = [] + + post['feed_details'] = {} + post['feed_details']['title'] = parsed.feed.title + post['feed_details']['link'] = parsed.feed.link + post['feed_details']['description'] = parsed.feed.description + tmp['all_posts_sorted'][str(post_date)].append(post) + db.update(tmp) return db +def load(): + db = SimpleDatabase('feeds.json', 'feeds.log') + return db + def latest(num): - """ Placeholder request """ - request = [ - { - "feedtitle" : "Varia EN", - "post": "hello world", - "date" : "Monday 15th of February 2021", - "url" : "https://vvvvvvaria.org/en/rr-wireless-imagination-1.html" - } - ] + """ Collect the latest published posts """ + db = load() + + dates = [key for key in db['all_posts_sorted'].keys()] + dates.sort(reverse=True) + request = [] + + for date in dates: + posts = db['all_posts_sorted'][date] + for post in posts: + if len(request) < int(num): + request.append(post) + else: + break + return request def today(): """ Collect posts from today """ db = load() + today = date.today() + request = [] - today = str(date.today()).split('-') - today_year = "{:02d}".format(int(today[0])) - today_month = "{:02d}".format(int(today[1])) - today_day = "{:02d}".format(int(today[2])) - print('TODAY =', today_year, today_month, today_day) + for date_str, posts in db['all_posts_sorted'].items(): + year = int(date_str.split('-')[0]) + month = int(date_str.split('-')[1]) + day = int(date_str.split('-')[2]) + d = date(year, month, day) + + # Check if any posts are published today + if d == today: + for post in posts: + request.append(post) - request = [] - for x, feed in db.items(): - for post in feed['entries']: - if post['published_parsed']: - year = "{:02d}".format(post['published_parsed'][0]) - month = "{:02d}".format(post['published_parsed'][1]) - day = "{:02d}".format(post['published_parsed'][2] + 1) - print('POST DATE =', year, month, day) - - # Check if this post is published today - if year == today_year: - if month == today_month: - if day == today_day: - request.append(post) return request def past(days): """ Collect posts from this week """ db = load() - point_in_the_past = date.today() - timedelta(int(days)) - print(f"{ days } days in the past =", point_in_the_past) - request = [] - for x, feed in db.items(): - for post in feed['entries']: - if post['published_parsed']: - year = post['published_parsed'][0] - month = post['published_parsed'][1] - day = post['published_parsed'][2] - - post_date = date(year, month, day) - print("post date =",post_date) - - if post_date > point_in_the_past: - request.append(post) + + for date_str, posts in db['all_posts_sorted'].items(): + year = int(date_str.split('-')[0]) + month = int(date_str.split('-')[1]) + day = int(date_str.split('-')[2]) + d = date(year, month, day) + + if d > point_in_the_past: + for post in posts: + request.append(post) - return request \ No newline at end of file + return request diff --git a/start.py b/start.py index b40a973..0bb4e7f 100644 --- a/start.py +++ b/start.py @@ -1,5 +1,6 @@ import flask import feedtools +import json APP = flask.Flask(__name__, static_url_path="", @@ -11,26 +12,42 @@ def index(): db = feedtools.load() template = flask.render_template( "index.html", - feeds=db, + db=db, ) return template -# @APP.route("/API/latest/") -# def latest(num): -# request = feedtools.latest(num) -# return request +@APP.route("/API/latest/") +def latest(num): + request = feedtools.latest(num) + response = APP.response_class( + response=json.dumps(request), + status=200, + mimetype='application/json' + ) + return response @APP.route("/API/today/") def today(): request = feedtools.today() - return str(request) + response = APP.response_class( + response=json.dumps(request), + status=200, + mimetype='application/json' + ) + return response @APP.route("/API/past/") def past(days): request = feedtools.past(days) - return str(request) + response = APP.response_class( + response=json.dumps(request), + status=200, + mimetype='application/json' + ) + return response if __name__ == "__main__": + feedtools.update() APP.debug = True APP.run(port=5678) diff --git a/static/css/stylesheet.css b/static/css/stylesheet.css index c04ad84..aeea20b 100644 --- a/static/css/stylesheet.css +++ b/static/css/stylesheet.css @@ -1,14 +1,21 @@ body{ background-color: pink; - color: red; + /*color: red;*/ margin: 1em; + font-size: 16px; + line-height: 1.6; } -h1, -h2{ - margin: 1em; + +h1#title{ + width: 100%; + text-align: center; + margin: 1em auto; +} +h1#title img{ + width: 500px; } h2{ - color: fuchsia; + margin: 0 0 1em; } table{ @@ -16,22 +23,31 @@ table{ width: 100%; border-collapse: collapse; } +table tr{ + border-bottom: 20px solid white; +} table td{ padding: 1em 2em; + vertical-align: top; } table td:first-of-type{ width: 100px; } -table tr{ - border-bottom: 20px solid white; -} section#api{ - margin: 6em 0; + margin: 6em 0em; color: fuchsia; } section#api div.accesspoint{ margin: 0; - padding: 1em; - border-bottom: 20px solid yellow; + padding: 1em 2em; + border-bottom: 20px solid black; +} + +a, +a:active, +a:visited, +a:hover{ + text-decoration-line: underline; + color: inherit; } \ No newline at end of file diff --git a/static/img/multifeeder.svg b/static/img/multifeeder.svg new file mode 100644 index 0000000..7436ea7 --- /dev/null +++ b/static/img/multifeeder.svg @@ -0,0 +1,81 @@ + + + + + + + + + + + + image/svg+xml + + + + + + + multifeeder + + diff --git a/templates/index.html b/templates/index.html index a5a08da..5131ac0 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,33 +6,52 @@ -

multifeeder

+

+ +

+
(Add a feed)
+
Currently feeding:
+
+
- {% for x, feed in feeds.items() %} + {% for x, feed in db['feeds'].items() %} - - + + + {% endfor %}
{{ feed.title }}{{ feed.link }}{{ feed.title }}{{ feed.description }}
+
+
+ +
-

API

- + Format: JSON +
- /API/today/ +

/API/today/

+ For example: https://multi.vvvvvvaria.org/API/today/ +
+ (for the posts published today)

- For example: localhost:5678/API/today/ + Format: JSON
- /API/past/[days] +

/API/past/[days]

+ For example: https://multi.vvvvvvaria.org/API/past/30 +
+ (for all the posts published in the last 30 days)

- For example: localhost:5678/API/past/30 + Format: JSON