From 9fa98136126e20ca76985e05f553494234a5a629 Mon Sep 17 00:00:00 2001 From: Francis Tseng Date: Tue, 29 Dec 2020 14:20:30 -0500 Subject: [PATCH 1/2] generate rss feed separated html to gen_html() --- server.py | 209 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 121 insertions(+), 88 deletions(-) diff --git a/server.py b/server.py index 6fb18f4..72eac5f 100644 --- a/server.py +++ b/server.py @@ -9,6 +9,10 @@ import base64 import hashlib import argparse +from email import utils +from datetime import datetime +from xml.etree import ElementTree as etree + from collections import defaultdict from http.server import BaseHTTPRequestHandler, HTTPServer @@ -33,6 +37,112 @@ def idx(obj, base, overrides): overrides = args.OVERRIDES.split(";") overrides = [x for x in overrides if len(x.split(":")) == 2] +rss_meta = { + 'title': 'hili', + 'description': 'highlights', +} +rss_mapping = { + 'link': lambda i: i['href'], + 'title': lambda i: i['title'], + 'description': lambda i: i['text'], + 'pubDate': lambda i: utils.format_datetime(datetime.fromtimestamp(i['time']/1000)) +} + +def gen_rss(items): + rss = etree.Element('rss', version='2.0') + channel = etree.SubElement(rss, 'channel') + for key, val in rss_meta.items(): + sub = etree.SubElement(channel, key) + sub.text = val + + for item in items: + item_el = etree.SubElement(channel, 'item') + for tag, fn in rss_mapping.items(): + el = etree.SubElement(item_el, tag) + el.text = fn(item) + for tag in item['tags']: + el = etree.SubElement(item_el, 'category') + el.text = tag + return etree.tostring(rss) + +def gen_html(items): + html = [''' + + + + + + '''] + + grouped = defaultdict(list) + for d in data: + grouped[idx(d, 'href', overrides)].append(d) + + for href, group in sorted(grouped.items(), key=lambda g: -max([idx(d, 'time', overrides) for d in g[1]])): + html.append(''' +
+

{title}

'''.format(href=href, title=group[0].get('title'))) + for d in group: + if 'file' in d: + html.append(''' +
+ +

{text}

+
{tags}
+
+ '''.format( + src=idx(d, 'file', overrides)['src'], + text=idx(d, 'text', overrides), + tags=', '.join(idx(d, 'tags', overrides)) + )) + else: + html.append(''' +
+ {html} +
{note}
+
{tags}
+
+ '''.format( + html=idx(d, 'html', overrides), + note=idx(d, 'note', overrides), + tags=', '.join(idx(d, 'tags', overrides)) + )) + html.append('
') + + html.append('') + return '\n'.join(html).encode('utf8') + class JSONRequestHandler(BaseHTTPRequestHandler): def do_POST(self): @@ -83,96 +193,19 @@ def do_POST(self): return def do_GET(self): - self.send_response(200) - self.send_header('Content-type', 'text/html') - self.send_header('Access-Control-Allow-Origin', '*') - self.end_headers() - - data = [] with open(args.FILE, 'r') as f: - for l in f.read().splitlines(): - data.append(json.loads(l)) - - # Reverse chron - html = [''' - - - - - - '''] - - grouped = defaultdict(list) - for d in data: - grouped[idx(d, 'href', overrides)].append(d) - - for href, group in sorted(grouped.items(), key=lambda g: -max([idx(d, 'time', overrides) for d in g[1]])): - html.append(''' -
-

{title}

'''.format(href=href, title=group[0].get('title'))) - for d in group: - if 'file' in d: - html.append(''' -
- -

{text}

-
{tags}
-
- '''.format( - src=idx(d, 'file', overrides)['src'], - text=idx(d, 'text', overrides), - tags=', '.join(idx(d, 'tags', overrides)) - )) - else: - html.append(''' -
- {html} -
{note}
-
{tags}
-
- '''.format( - html=idx(d, 'html', overrides), - note=idx(d, 'note', overrides), - tags=', '.join(idx(d, 'tags', overrides)) - )) - html.append('
') - - html.append('') + items = map(json.loads, f.read().splitlines()) - # Response - html = '\n'.join(html).encode('utf8') - self.wfile.write(html) + if self.path.startswith('/rss.xml'): + self.send_response(200) + self.send_header('Content-type', 'text/xml') + self.end_headers() + self.wfile.write(gen_rss(items)) + else: + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + self.wfile.write(gen_html(items)) if __name__ == '__main__': From f502ee9d0f8f89a9bb36cc26d0fef7800946d108 Mon Sep 17 00:00:00 2001 From: LK Date: Sat, 1 May 2021 23:50:09 +0000 Subject: [PATCH 2/2] :lipstick: --- server.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server.py b/server.py index 72eac5f..21c6c8b 100644 --- a/server.py +++ b/server.py @@ -42,9 +42,9 @@ def idx(obj, base, overrides): 'description': 'highlights', } rss_mapping = { - 'link': lambda i: i['href'], - 'title': lambda i: i['title'], - 'description': lambda i: i['text'], + 'link': lambda i: idx(i, 'href', overrides), + 'title': lambda i: idx(i, 'html', overrides), + 'description': lambda i: idx(i, 'text', overrides), 'pubDate': lambda i: utils.format_datetime(datetime.fromtimestamp(i['time']/1000)) } @@ -106,7 +106,7 @@ def gen_html(items): '''] grouped = defaultdict(list) - for d in data: + for d in items: grouped[idx(d, 'href', overrides)].append(d) for href, group in sorted(grouped.items(), key=lambda g: -max([idx(d, 'time', overrides) for d in g[1]])):