Init Flask app

This commit is contained in:
Tim Van Baak 2023-05-29 18:19:33 -07:00
parent 893c48175e
commit bb1bdee4c9
5 changed files with 300 additions and 9 deletions

5
.gitignore vendored
View File

@ -163,4 +163,7 @@ cython_debug/
nixos.qcow2 nixos.qcow2
# nix-build # nix-build
result result
# test sources
tests/**/*.item

66
intake/app.py Normal file
View File

@ -0,0 +1,66 @@
from datetime import datetime
from pathlib import Path
import os
from flask import Flask, render_template, request, jsonify, abort, redirect, url_for
from intake.source import LocalSource
# Globals
app = Flask(__name__)
def intake_data_dir() -> Path:
if intake_data := os.environ.get("INTAKE_DATA"):
return Path(intake_data)
if xdg_data_home := os.environ.get("XDG_DATA_HOME"):
return Path(xdg_data_home) / "intake"
if home := os.environ.get("HOME"):
return Path(home) / ".local" / "share" / "intake"
raise Exception("No intake data directory defined")
def item_sort_key(item):
item_date = item.get("time", item.get("created", 0))
return (item_date, item["id"])
@app.template_filter("datetimeformat")
def datetimeformat(value):
if not value:
return ""
dt = datetime.fromtimestamp(value)
return dt.strftime("%Y-%m-%d %H:%M:%S")
@app.route("/")
def root():
return "hello, world"
@app.route("/source/<string:source_name>")
def source_feed(source_name):
"""
Feed view for a single source.
"""
source = LocalSource(intake_data_dir(), source_name)
# Get all items
# TODO: support paging parameters
all_items = list(source.get_all_items())
all_items.sort(key=item_sort_key)
return render_template(
"feed.jinja2",
items=all_items,
mdeac=[
{"source": item["source"], "itemid": item["id"]}
for item in all_items
if "id" in item
],
)
def wsgi():
# init_default_logging()
return app

View File

@ -6,8 +6,8 @@ import os.path
import subprocess import subprocess
import sys import sys
from .source import fetch_items, LocalSource, update_items from intake.source import fetch_items, LocalSource, update_items
from .types import InvalidConfigException, SourceUpdateException from intake.types import InvalidConfigException, SourceUpdateException
def intake_data_dir() -> Path: def intake_data_dir() -> Path:
@ -76,10 +76,9 @@ def cmd_update(cmd_args):
parser.add_argument( parser.add_argument(
"--dry-run", "--dry-run",
action="store_true", action="store_true",
help="Instead of updating the source, print the fetched items" help="Instead of updating the source, print the fetched items",
) )
args = parser.parse_args(cmd_args) args = parser.parse_args(cmd_args)
ret = 0
source = LocalSource(Path(args.base), args.source) source = LocalSource(Path(args.base), args.source)
try: try:
@ -92,13 +91,38 @@ def cmd_update(cmd_args):
except InvalidConfigException as ex: except InvalidConfigException as ex:
print("Could not fetch", args.source) print("Could not fetch", args.source)
print(ex) print(ex)
ret = 1 return 1
except SourceUpdateException as ex: except SourceUpdateException as ex:
print("Error updating source", args.source) print("Error updating source", args.source)
print(ex) print(ex)
ret = 1 return 1
return ret return 0
def cmd_run(cmd_args):
"""Run the default Flask server."""
parser = argparse.ArgumentParser(
prog="intake run",
description=cmd_run.__doc__,
)
parser.add_argument(
"--base",
default=intake_data_dir(),
help="Path to the intake data directory containing source directories",
)
parser.add_argument("--debug", action="store_true")
parser.add_argument("--port", type=int, default=5000)
args = parser.parse_args(cmd_args)
try:
from intake.app import app
app.run(port=args.port, debug=args.debug)
return 0
except Exception as ex:
print(ex)
return 1
def cmd_help(_): def cmd_help(_):

View File

@ -7,7 +7,7 @@ import os
import os.path import os.path
import time import time
from .types import InvalidConfigException, SourceUpdateException from intake.types import InvalidConfigException, SourceUpdateException
class LocalSource: class LocalSource:
@ -68,6 +68,11 @@ class LocalSource:
def delete_item(self, item_id) -> None: def delete_item(self, item_id) -> None:
os.remove(self.get_item_path(item_id)) os.remove(self.get_item_path(item_id))
def get_all_items(self) -> List[dict]:
for filepath in self.source_path.iterdir():
if filepath.name.endswith(".item"):
yield json.loads(filepath.read_text(encoding="utf8"))
def read_stdout(process: Popen, outs: list): def read_stdout(process: Popen, outs: list):
""" """

View File

@ -0,0 +1,193 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Intake{% if items %} ({{ items|length - 1 }}){% endif %}</title>
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwgAADsIBFShKgAAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMS41ZEdYUgAAAGFJREFUOE+lkFEKwDAIxXrzXXB3ckMm9EnAV/YRCxFCcUXEL3Jc77NDjpDA/VGL3RFWYEICfeGC8oQc9IPuCAnQDcoRVmBCAn3hgvKEHPSD7ggJ0A3KEVZgQgJ94YLSJ9YDUzNGDXGZ/JEAAAAASUVORK5CYII=">
<style>
div#wrapper {
max-width: 700px;
margin: 0 auto;
}
.readable-item {
border: 1px solid black; border-radius: 6px;
padding: 5px;
margin-bottom: 20px;
word-break: break-word;
}
.item-title {
font-size: 1.4em;
}
.item-button {
font-size: 1em;
float:right;
margin-left: 2px;
}
.item-link {
text-decoration: none;
float:right;
font-size: 1em;
padding: 2px 7px;
border: 1px solid;
border-radius: 2px;
}
.item-info {
color: rgba(0, 0, 0, 0.7);
}
.readable-item img {
max-width: 100%;
}
button, summary {
cursor: pointer;
}
summary {
display: block;
}
summary:focus {
outline: 1px dotted gray;
}
.strikethru span, .strikethru p {
text-decoration: line-through;
}
.fade span, .fade p {
color: rgba(0, 0, 0, 0.2);
}
pre {
white-space: pre-wrap;
}
table.feed-control td {
font-family: monospace; padding: 5px 10px;
}
</style>
<script>
var deactivate = function (source, itemid) {
fetch('/deactivate/', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify({source: source, itemid: itemid}),
})
.then(response => response.json())
.then(function (data) {
if (!data.active) {
document.getElementById(source + "-" + itemid)
.classList.add("strikethru", "fade");
}
});
};
var punt = function (source, itemid) {
fetch('/punt/', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify({source: source, itemid: itemid}),
})
.then(response => response.json())
.then(function (data) {
if (data.tts) {
document.getElementById(source + "-" + itemid)
.classList.add("fade");
}
});
};
var mdeactivate = function (items) {
fetch('/mass-deactivate/', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify({items: items}),
})
.then(function () {
location.reload();
});
};
var callback = function (source, itemid) {
document.getElementById(source + "-" + itemid + "-callback").disabled = true;
fetch('/callback/', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify({source: source, itemid: itemid}),
})
.then(function (data) {
location.reload()
});
};
</script>
</head>
<body>
<div id="wrapper">
{# #}
{% if items %}
{% for item in items %}
<div class="readable-item" id="{{item.source}}-{{item.id}}">
{% if item.id %}
<button class="item-button" onclick="javascript:deactivate('{{item.source}}', '{{item.id}}')" title="Deactivate">&#10005;</button>
{% endif %}
{% if item.id %}
<button class="item-button" onclick="javascript:punt('{{item.source}}', '{{item.id}}')" title="Punt to tomorrow">&#8631;</button>
{% endif %}
{% if item.link %}
<a class="item-link" href="{{item.link}}" target="_blank">&#8663;</a>
{% endif %}
{# The item title is a clickable <summary> if there is body content #}
{% if item.body or item.callback %}
<details>
<summary><span class="item-title">{{item.title}}</span></summary>
{% if item.body %}
<p>{{item.body|safe}}</p>
{% endif %}
{% if item.callback %}
<p><button id="{{item.source}}-{{item.id}}-callback" onclick="javascript:callback('{{item.source}}', '{{item.id}}')">Callback</button></p>
{% endif %}
</details>
{% else %}
<span class="item-title">{{item.title}}</span><br>
{% endif %}
{# author/time footer line #}
{% if item.author or item.time %}
<span class="item-info">
{% if item.author %}{{item.author}}{% endif %}
{% if item.time %}{{item.time|datetimeformat}}{% endif %}
</span><br>
{% endif %}
{# source/id/created footer line #}
{% if item.source or item.id or item.created %}
<span class="item-info" title="{{ 'Tags: {}'.format(', '.join(item.tags)) }}">
{% if item.source %}{{item.source}}{% endif %}
{% if item.id %}{{item.id}}{% endif %}
{% if item.created %}{{item.created|datetimeformat}}{% endif %}
{% if item.ttl %}L{% endif %}{% if item.ttd %}D{% endif %}{% if item.tts %}S{% endif %}
</span>
{% endif %}
</div>
{% endfor %}
{% if items %}
<div class="readable-item">
<details>
<summary><span class="item-title">Feed Management</span></summary>
<div style="text-align:center;">
<button style="font-size: 1.4em;" onclick="javascript:mdeactivate({{ mdeac|safe }})">Deactivate All</button>
</div>
</details>
</div>
{% endif %}
{# if items #}
{% else %}
<div class="readable-item">
<span class="item-title">Feed is empty</span>
</div>
{% endif %}
</div>
</body>
</html>