Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate rss feed #11

Merged
merged 2 commits into from
Aug 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 121 additions & 88 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = ['''
<html>
<head>
<meta charset="utf8">
<style>
html {
overflow-x: hidden;
}
article {
margin: 4em auto;
max-width: 720px;
line-height: 1.4;
padding-bottom: 4em;
border-bottom: 2px solid black;
font-family: sans-serif;
}
.highlight {
margin: 2em 0;
}
.note {
margin-top: 0.5em;
text-align: right;
font-size: 0.9em;
}
.tags {
color: #888;
margin-top: 1em;
font-size: 0.8em;
}
a {
color: blue;
}
img {
max-width: 100%;
}
</style>
</head>
<body>''']

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('''
<article>
<h4><a href="{href}">{title}</a></h4>'''.format(href=href, title=group[0].get('title')))
for d in group:
if 'file' in d:
html.append('''
<div class="highlight">
<img src="{src}">
<p>{text}</p>
<div class="tags"><em>{tags}</em></div>
</div>
'''.format(
src=idx(d, 'file', overrides)['src'],
text=idx(d, 'text', overrides),
tags=', '.join(idx(d, 'tags', overrides))
))
else:
html.append('''
<div class="highlight">
{html}
<div class="note">{note}</div>
<div class="tags"><em>{tags}</em></div>
</div>
'''.format(
html=idx(d, 'html', overrides),
note=idx(d, 'note', overrides),
tags=', '.join(idx(d, 'tags', overrides))
))
html.append('</article>')

html.append('</body></html>')
return '\n'.join(html).encode('utf8')


class JSONRequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
Expand Down Expand Up @@ -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 = ['''
<html>
<head>
<meta charset="utf8">
<style>
html {
overflow-x: hidden;
}
article {
margin: 4em auto;
max-width: 720px;
line-height: 1.4;
padding-bottom: 4em;
border-bottom: 2px solid black;
font-family: sans-serif;
}
.highlight {
margin: 2em 0;
}
.note {
margin-top: 0.5em;
text-align: right;
font-size: 0.9em;
}
.tags {
color: #888;
margin-top: 1em;
font-size: 0.8em;
}
a {
color: blue;
}
img {
max-width: 100%;
}
</style>
</head>
<body>''']

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('''
<article>
<h4><a href="{href}">{title}</a></h4>'''.format(href=href, title=group[0].get('title')))
for d in group:
if 'file' in d:
html.append('''
<div class="highlight">
<img src="{src}">
<p>{text}</p>
<div class="tags"><em>{tags}</em></div>
</div>
'''.format(
src=idx(d, 'file', overrides)['src'],
text=idx(d, 'text', overrides),
tags=', '.join(idx(d, 'tags', overrides))
))
else:
html.append('''
<div class="highlight">
{html}
<div class="note">{note}</div>
<div class="tags"><em>{tags}</em></div>
</div>
'''.format(
html=idx(d, 'html', overrides),
note=idx(d, 'note', overrides),
tags=', '.join(idx(d, 'tags', overrides))
))
html.append('</article>')

html.append('</body></html>')
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__':
Expand Down