diff --git a/server.py b/server.py
index 6fb18f4..21c6c8b 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: 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))
+}
+
+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 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]])):
+ html.append('''
+
+ '''.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('''
-
- '''.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__':