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 print(fp.feed.link)
33 feed_title = fp.feed.title
34 print(feed_title)
35 full_summ = str(html.fromstring(fp.entries[0].summary).text_content())
36 trunc_summ = full_summ[:256] + "…"
37 pub_date = fp.entries[0].published
38 pretty_date = arrow.get(pub_date, "ddd, DD MMM YYYY HH:mm:ss Z").format("MMM DD, YYYY")
39 post_link = fp.entries[0].link
40 post_title = fp.entries[0].title
41
42 html_out.append(f"""<div class="openring-feed">
43 <h4><a href="{post_link}">{post_title}</a></h4>
44 <p>{trunc_summ}</p>
45
46 <p>via <a href="{feed_link}">{feed_title}</a> on {pretty_date}</p>
47 </div>
48 """
49 )
50
51if sys.argv[1] == "-j":
52 rendered = jinja_render(html_out, "templates/openring.html")
53 with open("templates/text.html", "w") as t:
54 t.write(rendered)
55else:
56 print(html_out)