bin/openring.py (view raw)
1#!/usr/bin/env python3
2# openring.py - generate a webring from rss feeds
3
4import feedparser
5import arrow
6import jinja2
7import random
8import sys
9from lxml import html
10from jinja2 import DebugUndefined
11
12def jinja_render(html, tmpl):
13 template_loader = jinja2.FileSystemLoader("./")
14 env = jinja2.Environment(loader=template_loader, undefined=DebugUndefined)
15 try:
16 template = env.get_template(tmpl)
17 except jinja2.exceptions.TemplateNotFound:
18 print("error: template not found")
19
20 out = template.render(feeds=html)
21 return out
22
23
24with open("feeds.txt") as f:
25 feeds = f.readlines()
26
27html_out = []
28
29for f in random.sample(feeds, 3):
30 fp = feedparser.parse(f)
31 feed_link = fp.feed.link
32 feed_title = fp.feed.title
33 print(feed_title)
34 full_summ = str(html.fromstring(fp.entries[0].summary).text_content())
35 trunc_summ = full_summ[:256] + "…"
36 pub_date = fp.entries[0].published
37 pretty_date = arrow.get(pub_date, "ddd, DD MMM YYYY HH:mm:ss Z").format("MMM DD, YYYY")
38 post_link = fp.entries[0].link
39 post_title = fp.entries[0].title
40
41 html_out.append(f"""<div class="openring-feed">
42 <h4><a href="{post_link}">{post_title}</a></h4>
43 <p>{trunc_summ}</p>
44
45 <p>via <a href="{feed_link}">{feed_title}</a> on {pretty_date}</p>
46 </div>
47 """
48 )
49
50if sys.argv[1] == "-j":
51 rendered = jinja_render(html_out, "templates/openring.html")
52 with open("templates/text.html", "w") as t:
53 t.write(rendered)
54else:
55 print(html_out)