From b5762e90ef540c88a28e07b2bb5e80ee70313f2e Mon Sep 17 00:00:00 2001 From: "Schimon Jehudah, Adv." Date: Wed, 4 Sep 2024 15:11:45 +0300 Subject: [PATCH] Improve cache handling; Order items descendingly. --- css/stylesheet.css | 8 +++-- pubsub_to_atom.py | 83 +++++++++++++++++++++++++++++-------------- xsl/atom_as_xhtml.xsl | 5 ++- 3 files changed, 66 insertions(+), 30 deletions(-) diff --git a/css/stylesheet.css b/css/stylesheet.css index 0fd3c56..4f0db3f 100644 --- a/css/stylesheet.css +++ b/css/stylesheet.css @@ -138,7 +138,7 @@ h3.title > a { } #articles div.entry h1 { - font-size: 2vw; + font-size: 115%; /* 2vw */ } #articles div.entry h2 { @@ -179,7 +179,7 @@ h3.title > a { } #selection-page #return { - font-style: italic; + /* font-style: italic; */ margin: auto; } @@ -188,6 +188,10 @@ h3.title > a { text-decoration: underline; } +.content[type='text'] { + white-space: pre-wrap; +} + #articles div.entry span.tags { display: inline-flex; /* display: ruby; */ diff --git a/pubsub_to_atom.py b/pubsub_to_atom.py index a8645e1..61d19f2 100644 --- a/pubsub_to_atom.py +++ b/pubsub_to_atom.py @@ -6,6 +6,8 @@ from fastapi import FastAPI, Request, Response from fastapi.responses import FileResponse from fastapi.staticfiles import StaticFiles import json +from os import mkdir +from os.path import exists from slixmpp import ClientXMPP from slixmpp.exceptions import IqError, IqTimeout import xml.etree.ElementTree as ET @@ -59,7 +61,7 @@ async def view_pubsub_nodes(request: Request): xml_opml = generate_opml(iq) result = append_stylesheet(xml_opml, 'opml') 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) result = append_stylesheet(xml_atom, 'atom') else: @@ -126,7 +128,7 @@ async def view_node_items(request: Request): with open(filename, 'w', encoding='utf-8') as f: json.dump(json_data, f, ensure_ascii=False, indent=4) 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) result = append_stylesheet(xml_atom, 'atom') @@ -146,7 +148,7 @@ async def view_node_items(request: Request): link = form_a_node_link(pubsub, node) xml_atom = generate_atom(iq, link) 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) result = append_stylesheet(xml_atom, 'atom') elif pubsub: @@ -157,30 +159,39 @@ async def view_node_items(request: Request): text = 'PubSub parameter is missing.' xml_atom = error_message(text) result = append_stylesheet(xml_atom, 'atom') - # else: - # result = ('Mandatory parameter PubSub and ' - # 'optional parameter Node are missing.') +# else: +# text = ('Mandatory parameter PubSub and ' +# 'optional parameter Node are missing.') +# xml_atom = error_message(text) +# result = append_stylesheet(xml_atom, 'atom') else: text = 'The given domain {} is not allowed.'.format(pubsub) xml_atom = error_message(text) result = append_stylesheet(xml_atom, 'atom') - default = get_configuration('default') if not result: + default = get_configuration('default') if default['pubsub'] and default['nodeid']: if not pubsub and not node: pubsub = default['pubsub'] node = default['nodeid'] iq = await get_node_items(pubsub, node) - link = form_a_node_link(pubsub, node) - xml_atom = generate_atom(iq, link) - result = append_stylesheet(xml_atom, 'atom') + if iq: + link = form_a_node_link(pubsub, node) + xml_atom = generate_atom(iq, link) + else: + text = 'Please ensure that PubSub node "{}" is valid and accessible.'.format(node) + xml_atom = error_message(text) elif not settings['service']: pubsub = default['pubsub'] node = default['nodeid'] iq = await get_node_items(pubsub, node) - link = form_a_node_link(pubsub, node) - xml_atom = generate_atom(iq, link) - result = append_stylesheet(xml_atom, 'atom') + if iq: + link = form_a_node_link(pubsub, node) + 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') else: text = 'Please contact the administrator and ask him to set default PubSub and Node ID.' xml_atom = error_message(text) @@ -240,7 +251,7 @@ def error_message(text): ET.SubElement(feed, 'author', {'name':'Rivista','email':'rivista@schimon.i2p'}) ET.SubElement(feed, 'generator', { '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() entry = ET.SubElement(feed, 'entry') 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, 'generator', { '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() - for item in items: + for item in list(items)[::-1]: item_id = item['id'] item_payload = item['payload'] 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 else: summary = item_payload.find(namespace + 'summary') - summary_text = summary.text + summary_text = summary.text if summary else None if summary_text: summary_type = summary.attrib['type'] if 'type' in summary.attrib else 'html' 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_text = None if updated == None else updated.text ET.SubElement(e_entry, 'updated').text = updated_text - e_author = ET.SubElement(e_entry, 'author') authors = item_payload.find(namespace + 'author') if isinstance(authors, ET.Element): for author in item_payload.findall(namespace + 'author'): - if not author.text: continue - author_text = author.text - author_email = link.attrib['href'] if 'href' in link.attrib else '' - author_uri = link.attrib['type'] if 'type' in link.attrib else '' - author_summary = link.attrib['rel'] if 'rel' in link.attrib else '' - ET.SubElement(e_author, 'name').text = author_text - if author and author.attrib: print(author.attrib) + e_author = ET.SubElement(e_entry, 'author') + author_email = author.find(namespace + 'email') + if author_email is not None: + author_email_text = author_email.text + if author_email_text: ET.SubElement(e_author, 'email').text = author_email.text + author_uri = author.find(namespace + 'uri') + 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') if isinstance(categories, ET.Element): for category in item_payload.findall(namespace + 'category'): @@ -368,8 +393,12 @@ def generate_json(iq): json_data_entry = {'title' : title_text, 'link' : link_href} json_data.append(json_data_entry) - if len(json_data) > 6: break - filename = 'data/{}.json'.format(node) + #if len(json_data) > 6: break + 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: json.dump(json_data, f, ensure_ascii=False, indent=4) diff --git a/xsl/atom_as_xhtml.xsl b/xsl/atom_as_xhtml.xsl index e5fa93f..89ed3bd 100644 --- a/xsl/atom_as_xhtml.xsl +++ b/xsl/atom_as_xhtml.xsl @@ -246,6 +246,7 @@ xmlns:atom='http://www.w3.org/2005/Atom'>

+ By @@ -272,9 +273,11 @@ xmlns:atom='http://www.w3.org/2005/Atom'> - By + + +