Improve cache handling;

Order items descendingly.
This commit is contained in:
Schimon Jehudah, Adv. 2024-09-04 15:11:45 +03:00
parent ad34af72ff
commit b5762e90ef
3 changed files with 66 additions and 30 deletions

View file

@ -138,7 +138,7 @@ h3.title > a {
} }
#articles div.entry h1 { #articles div.entry h1 {
font-size: 2vw; font-size: 115%; /* 2vw */
} }
#articles div.entry h2 { #articles div.entry h2 {
@ -179,7 +179,7 @@ h3.title > a {
} }
#selection-page #return { #selection-page #return {
font-style: italic; /* font-style: italic; */
margin: auto; margin: auto;
} }
@ -188,6 +188,10 @@ h3.title > a {
text-decoration: underline; text-decoration: underline;
} }
.content[type='text'] {
white-space: pre-wrap;
}
#articles div.entry span.tags { #articles div.entry span.tags {
display: inline-flex; display: inline-flex;
/* display: ruby; */ /* display: ruby; */

View file

@ -6,6 +6,8 @@ from fastapi import FastAPI, Request, Response
from fastapi.responses import FileResponse from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
import json import json
from os import mkdir
from os.path import exists
from slixmpp import ClientXMPP from slixmpp import ClientXMPP
from slixmpp.exceptions import IqError, IqTimeout from slixmpp.exceptions import IqError, IqTimeout
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
@ -59,7 +61,7 @@ async def view_pubsub_nodes(request: Request):
xml_opml = generate_opml(iq) xml_opml = generate_opml(iq)
result = append_stylesheet(xml_opml, 'opml') result = append_stylesheet(xml_opml, 'opml')
else: else:
text = 'Please ensure that the PubSub Jabber ID is valid and accessible.' text = 'Please ensure that PubSub "{}" (Jabber ID) is valid and accessible.'.format(pubsub)
xml_atom = error_message(text) xml_atom = error_message(text)
result = append_stylesheet(xml_atom, 'atom') result = append_stylesheet(xml_atom, 'atom')
else: else:
@ -126,7 +128,7 @@ async def view_node_items(request: Request):
with open(filename, 'w', encoding='utf-8') as f: with open(filename, 'w', encoding='utf-8') as f:
json.dump(json_data, f, ensure_ascii=False, indent=4) json.dump(json_data, f, ensure_ascii=False, indent=4)
else: else:
text = 'Please ensure that the PubSub node and item are valid and accessible.' text = 'Please ensure that PubSub node "{}" and item "{}" are valid and accessible.'.format(node, item_id)
xml_atom = error_message(text) xml_atom = error_message(text)
result = append_stylesheet(xml_atom, 'atom') result = append_stylesheet(xml_atom, 'atom')
@ -146,7 +148,7 @@ async def view_node_items(request: Request):
link = form_a_node_link(pubsub, node) link = form_a_node_link(pubsub, node)
xml_atom = generate_atom(iq, link) xml_atom = generate_atom(iq, link)
else: else:
text = 'Please ensure that the PubSub node is valid and accessible.' text = 'Please ensure that PubSub node "{}" is valid and accessible.'.format(node)
xml_atom = error_message(text) xml_atom = error_message(text)
result = append_stylesheet(xml_atom, 'atom') result = append_stylesheet(xml_atom, 'atom')
elif pubsub: elif pubsub:
@ -157,29 +159,38 @@ async def view_node_items(request: Request):
text = 'PubSub parameter is missing.' text = 'PubSub parameter is missing.'
xml_atom = error_message(text) xml_atom = error_message(text)
result = append_stylesheet(xml_atom, 'atom') result = append_stylesheet(xml_atom, 'atom')
# else: # else:
# result = ('Mandatory parameter PubSub and ' # text = ('Mandatory parameter PubSub and '
# 'optional parameter Node are missing.') # 'optional parameter Node are missing.')
# xml_atom = error_message(text)
# result = append_stylesheet(xml_atom, 'atom')
else: else:
text = 'The given domain {} is not allowed.'.format(pubsub) text = 'The given domain {} is not allowed.'.format(pubsub)
xml_atom = error_message(text) xml_atom = error_message(text)
result = append_stylesheet(xml_atom, 'atom') result = append_stylesheet(xml_atom, 'atom')
default = get_configuration('default')
if not result: if not result:
default = get_configuration('default')
if default['pubsub'] and default['nodeid']: if default['pubsub'] and default['nodeid']:
if not pubsub and not node: if not pubsub and not node:
pubsub = default['pubsub'] pubsub = default['pubsub']
node = default['nodeid'] node = default['nodeid']
iq = await get_node_items(pubsub, node) iq = await get_node_items(pubsub, node)
if iq:
link = form_a_node_link(pubsub, node) link = form_a_node_link(pubsub, node)
xml_atom = generate_atom(iq, link) xml_atom = generate_atom(iq, link)
result = append_stylesheet(xml_atom, 'atom') else:
text = 'Please ensure that PubSub node "{}" is valid and accessible.'.format(node)
xml_atom = error_message(text)
elif not settings['service']: elif not settings['service']:
pubsub = default['pubsub'] pubsub = default['pubsub']
node = default['nodeid'] node = default['nodeid']
iq = await get_node_items(pubsub, node) iq = await get_node_items(pubsub, node)
if iq:
link = form_a_node_link(pubsub, node) link = form_a_node_link(pubsub, node)
xml_atom = generate_atom(iq, link) xml_atom = generate_atom(iq, link)
else:
text = 'Please ensure that PubSub node "{}" is valid and accessible.'.format(node)
xml_atom = error_message(text)
result = append_stylesheet(xml_atom, 'atom') result = append_stylesheet(xml_atom, 'atom')
else: else:
text = 'Please contact the administrator and ask him to set default PubSub and Node ID.' text = 'Please contact the administrator and ask him to set default PubSub and Node ID.'
@ -240,7 +251,7 @@ def error_message(text):
ET.SubElement(feed, 'author', {'name':'Rivista','email':'rivista@schimon.i2p'}) ET.SubElement(feed, 'author', {'name':'Rivista','email':'rivista@schimon.i2p'})
ET.SubElement(feed, 'generator', { ET.SubElement(feed, 'generator', {
'uri': 'https://git.xmpp-it.net/sch/Rivista', 'uri': 'https://git.xmpp-it.net/sch/Rivista',
'version': '0.1'}).text = 'Rivista' 'version': '0.1'}).text = 'Rivista XJP'
ET.SubElement(feed, 'updated').text = datetime.datetime.now(datetime.UTC).isoformat() ET.SubElement(feed, 'updated').text = datetime.datetime.now(datetime.UTC).isoformat()
entry = ET.SubElement(feed, 'entry') entry = ET.SubElement(feed, 'entry')
ET.SubElement(entry, 'title').text = 'Error' ET.SubElement(entry, 'title').text = 'Error'
@ -274,9 +285,9 @@ def generate_atom(iq, link):
ET.SubElement(e_feed, 'link', {'rel': 'self', 'href': link}) ET.SubElement(e_feed, 'link', {'rel': 'self', 'href': link})
ET.SubElement(e_feed, 'generator', { ET.SubElement(e_feed, 'generator', {
'uri': 'https://git.xmpp-it.net/sch/Rivista', 'uri': 'https://git.xmpp-it.net/sch/Rivista',
'version': '0.1'}).text = 'Rivista' 'version': '0.1'}).text = 'Rivista XJP'
ET.SubElement(e_feed, 'updated').text = datetime.datetime.now(datetime.UTC).isoformat() ET.SubElement(e_feed, 'updated').text = datetime.datetime.now(datetime.UTC).isoformat()
for item in items: for item in list(items)[::-1]:
item_id = item['id'] item_id = item['id']
item_payload = item['payload'] item_payload = item['payload']
namespace = '{http://www.w3.org/2005/Atom}' namespace = '{http://www.w3.org/2005/Atom}'
@ -307,7 +318,7 @@ def generate_atom(iq, link):
ET.SubElement(e_entry, 'content', {'type': content_type_text}).text = content_text ET.SubElement(e_entry, 'content', {'type': content_type_text}).text = content_text
else: else:
summary = item_payload.find(namespace + 'summary') summary = item_payload.find(namespace + 'summary')
summary_text = summary.text summary_text = summary.text if summary else None
if summary_text: if summary_text:
summary_type = summary.attrib['type'] if 'type' in summary.attrib else 'html' summary_type = summary.attrib['type'] if 'type' in summary.attrib else 'html'
summary_type_text = 'html' if 'html' in summary_type else 'text' summary_type_text = 'html' if 'html' in summary_type else 'text'
@ -320,17 +331,31 @@ def generate_atom(iq, link):
updated = item_payload.find(namespace + 'updated') updated = item_payload.find(namespace + 'updated')
updated_text = None if updated == None else updated.text updated_text = None if updated == None else updated.text
ET.SubElement(e_entry, 'updated').text = updated_text ET.SubElement(e_entry, 'updated').text = updated_text
e_author = ET.SubElement(e_entry, 'author')
authors = item_payload.find(namespace + 'author') authors = item_payload.find(namespace + 'author')
if isinstance(authors, ET.Element): if isinstance(authors, ET.Element):
for author in item_payload.findall(namespace + 'author'): for author in item_payload.findall(namespace + 'author'):
if not author.text: continue e_author = ET.SubElement(e_entry, 'author')
author_text = author.text author_email = author.find(namespace + 'email')
author_email = link.attrib['href'] if 'href' in link.attrib else '' if author_email is not None:
author_uri = link.attrib['type'] if 'type' in link.attrib else '' author_email_text = author_email.text
author_summary = link.attrib['rel'] if 'rel' in link.attrib else '' if author_email_text: ET.SubElement(e_author, 'email').text = author_email.text
ET.SubElement(e_author, 'name').text = author_text author_uri = author.find(namespace + 'uri')
if author and author.attrib: print(author.attrib) if author_uri is not None:
author_uri_text = author_uri.text
if author_uri_text: ET.SubElement(e_author, 'uri').text = author_uri.text
author_name = author.find(namespace + 'name')
if author_name is not None and author_name.text:
author_name_text = author_name.text
else:
author_name_text = author_uri_text or author_email_text
ET.SubElement(e_author, 'name').text = author_name_text
for uri in item_payload.iter(namespace + 'author'):
uri_text = uri.text
if uri_text:
ET.SubElement(e_entry, 'uri').text = uri_text
# if not e_author:
# ET.SubElement(e_author, 'name').text = uri_text
# ET.SubElement(e_author, 'uri').text = uri_text
categories = item_payload.find(namespace + 'category') categories = item_payload.find(namespace + 'category')
if isinstance(categories, ET.Element): if isinstance(categories, ET.Element):
for category in item_payload.findall(namespace + 'category'): for category in item_payload.findall(namespace + 'category'):
@ -368,8 +393,12 @@ def generate_json(iq):
json_data_entry = {'title' : title_text, json_data_entry = {'title' : title_text,
'link' : link_href} 'link' : link_href}
json_data.append(json_data_entry) json_data.append(json_data_entry)
if len(json_data) > 6: break #if len(json_data) > 6: break
filename = 'data/{}.json'.format(node) directory = 'data/{}/'.format(pubsub)
if not exists(directory):
mkdir(directory)
filename = 'data/{}/{}.json'.format(pubsub, node)
with open(filename, 'w', encoding='utf-8') as f: with open(filename, 'w', encoding='utf-8') as f:
json.dump(json_data, f, ensure_ascii=False, indent=4) json.dump(json_data, f, ensure_ascii=False, indent=4)

View file

@ -246,6 +246,7 @@ xmlns:atom='http://www.w3.org/2005/Atom'>
<!-- entry author --> <!-- entry author -->
<xsl:if test='atom:author'> <xsl:if test='atom:author'>
<h4 class='author'> <h4 class='author'>
<xsl:text>By </xsl:text>
<xsl:choose> <xsl:choose>
<xsl:when test='atom:author/atom:email'> <xsl:when test='atom:author/atom:email'>
<xsl:element name='a'> <xsl:element name='a'>
@ -272,9 +273,11 @@ xmlns:atom='http://www.w3.org/2005/Atom'>
</xsl:element> </xsl:element>
</xsl:when> </xsl:when>
<xsl:when test='atom:author/atom:name'> <xsl:when test='atom:author/atom:name'>
<xsl:text>By </xsl:text>
<xsl:value-of select='atom:author/atom:name'/> <xsl:value-of select='atom:author/atom:name'/>
</xsl:when> </xsl:when>
<xsl:when test='atom:uri'>
<xsl:value-of select='atom:uri'/>
</xsl:when>
</xsl:choose> </xsl:choose>
</h4> </h4>
</xsl:if> </xsl:if>