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 at /rss.xml #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
200 changes: 117 additions & 83 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 @@ -19,6 +23,108 @@
parser.add_argument('-k', '--key', type=str, dest='KEY', default=None, help='Secret key to authenticate clients')
args = parser.parse_args()

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):
# 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;
}
.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[d['href']].append(d)

for href, group in sorted(grouped.items(), key=lambda g: -max([d['time'] for d in g[1]])):
html.append('''
<article>
<h4><a href="{href}">{title}</a></h4>'''.format(href=href, title=group[0]['title']))
for d in group:
if 'file' in d:
# fname = d['file']['name']
html.append('''
<div class="highlight">
<img src="{src}">
<p>{text}</p>
<div class="tags"><em>{tags}</em></div>
</div>
'''.format(
# src=os.path.join(args.UPLOAD_DIR, fname),
src=d['file']['src'],
text=d['text'],
tags=', '.join(d['tags'])
))
else:
html.append('''
<div class="highlight">
{html}
<div class="tags"><em>{tags}</em></div>
</div>
'''.format(
html=d['html'],
tags=', '.join(d['tags'])
))
html.append('</article>')

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


class JSONRequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
Expand Down Expand Up @@ -69,91 +175,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;
}
.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[d['href']].append(d)

for href, group in sorted(grouped.items(), key=lambda g: -max([d['time'] for d in g[1]])):
html.append('''
<article>
<h4><a href="{href}">{title}</a></h4>'''.format(href=href, title=group[0]['title']))
for d in group:
if 'file' in d:
# fname = d['file']['name']
html.append('''
<div class="highlight">
<img src="{src}">
<p>{text}</p>
<div class="tags"><em>{tags}</em></div>
</div>
'''.format(
# src=os.path.join(args.UPLOAD_DIR, fname),
src=d['file']['src'],
text=d['text'],
tags=', '.join(d['tags'])
))
else:
html.append('''
<div class="highlight">
{html}
<div class="tags"><em>{tags}</em></div>
</div>
'''.format(
html=d['html'],
tags=', '.join(d['tags'])
))
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