diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | requirements.txt | 7 | ||||
-rwxr-xr-x | serve.py | 74 | ||||
-rw-r--r-- | views/add_feed.tpl | 24 | ||||
-rw-r--r-- | views/index.tpl | 33 |
5 files changed, 139 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f21b54 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/venv/ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..425cad0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +bottle==0.12.25 +feedparser==6.0.10 +gevent==22.10.2 +greenlet==2.0.2 +sgmllib3k==1.0.0 +zope.event==4.6 +zope.interface==6.0 diff --git a/serve.py b/serve.py new file mode 100755 index 0000000..7e07fdc --- /dev/null +++ b/serve.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +from gevent import monkey + +monkey.patch_all() +import bottle +import feedparser +import gevent.lock + +import argparse +import datetime + +parser = argparse.ArgumentParser() +parser.add_argument("host", nargs="?", default="localhost") +parser.add_argument("-p", "--port", default=8000, type=int) +args = parser.parse_args() + +feeds_lock = gevent.lock.RLock() +feeds = {} + +feed_items_lock = gevent.lock.RLock() +feed_items = [] + +def store_feed(feed_url: str): + ... + + +@bottle.route("/") +def index(): + with feed_items_lock: + return bottle.template("index", items=feed_items) + +@bottle.get("/add_feed") +def add_feed_ui(): + return bottle.template("add_feed") + + +@bottle.post("/add_feed") +def add_feed_effect(): + feed_source = bottle.request.forms.get("feed_source") + already_present: bool = False + with feeds_lock: + if feed_source not in feeds: + feeds[feed_source] = {} + else: + already_present = True + print(feeds) + feed = feedparser.parse(feed_source) + with feed_items_lock: + for entry in reversed(feed.entries): + try: + date_published = datetime.datetime(*(entry.published_parsed[0:6])).strftime("%x %X") + except AttributeError: + date_published = None + try: + date_updated = datetime.datetime(*(entry.updated_parsed[0:6])).strftime("%x %X") + except AttributeError: + date_updated = None + if date_updated == date_published: + date_updated = None + feed_items.append({ + "title": entry["title"], + "link": entry["link"], + "date_published": date_published, + "date_updated": date_updated, + }) + return bottle.template("add_feed", after_add=True, feed_source=feed_source, already_present=already_present) + + +@bottle.get("/modify_feed") +def modify_feed_ui(): + ... + + +bottle.run(host=args.host, port=args.port, server="gevent") diff --git a/views/add_feed.tpl b/views/add_feed.tpl new file mode 100644 index 0000000..bac78b0 --- /dev/null +++ b/views/add_feed.tpl @@ -0,0 +1,24 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Add Feed</title> +</head> +<body> + <a href="/">< home</a> + % if not get("already_present", False): + % if get("after_add", False): + <p><em>Added feed {{feed_source}}</em></p> + % end + % else: + <p><em>Feed {{feed_source}} was already added; no changes made.</em></p> + % end + <h1>Add a feed</h1> + <form method="post"> + <input type="url" placeholder="Feed source" name="feed_source"> + <br> + <input type="submit" value="Add"> + </form> +</body> +</html> diff --git a/views/index.tpl b/views/index.tpl new file mode 100644 index 0000000..b08ce0f --- /dev/null +++ b/views/index.tpl @@ -0,0 +1,33 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>View Feeds</title> +</head> +<body> + <table> + <tr> + <th>Title</th> + <th>Date</th> + </tr> + % for i, item in enumerate(reversed(items)): + <tr> + <td><a href="{{item["link"]}}">{{item["title"]}}</a></td> + <% + dates = [] + if item.get("date_published", None): + dates.append(item["date_published"]) + end + if item.get("date_updated", None): + dates.append(item["date_updated"]) + end + %> + <td> + {{", updated ".join(dates)}} + </td> + </tr> + % end + </table> +</body> +</html> |