From e239da0c1370e78e1b459d376c3dbced4b8bde39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Tue, 1 Sep 2020 23:35:24 +0200 Subject: [PATCH] RSS test feeds --- requirements.txt | 4 ++++ smallprint.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 2c6a854..28c9621 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,8 +2,11 @@ appdirs==1.4.4 argcomplete==1.12.0 certifi==2020.6.20 chardet==3.0.4 +feedparser==5.2.1 idna==2.10 +importlib-metadata==1.7.0 Pillow==7.2.0 +pkg-resources==0.0.0 pyserial==3.4 python-escpos==2.2.0 pyusb==1.0.2 @@ -12,3 +15,4 @@ qrcode==6.1 requests==2.24.0 six==1.15.0 urllib3==1.25.10 +zipp==3.1.0 diff --git a/smallprint.py b/smallprint.py index a2bbf84..a2784fb 100644 --- a/smallprint.py +++ b/smallprint.py @@ -6,8 +6,10 @@ import datetime import os.path # 3rd party imports -import requests from escpos.printer import Usb, Dummy, File +import feedparser +import re +import requests from config import config @@ -85,6 +87,7 @@ def print_weather(printer): else: icon_path = f"icons/weather/{icon_code}.png" + # TODO: The image impl should be an option printer.image(icon_path, impl="bitImageColumn") printer.text("\n") @@ -101,7 +104,61 @@ def print_weather(printer): logger.error("No weather info available") +def print_rss(printer, link, news=1): + """ + Given an RSS link and a printer, prints the news from it + """ + reset_defaults(printer) + feed = feedparser.parse(link) + title = feed["channel"]["title"] + + printer.set(align="center", + text_type="BU", + width=2, height=2, + smooth=True) + + printer.text(f"{title}\n\n") + reset_defaults(printer) + items = feed["items"][:news] + for item in items: + _print_rss_item(item) + + +def _print_rss_item(item): + title = item["title"] + date = item["published"] + text = clear_html(item["summary"]) + link = item["link"] + + printer.set(align="left", + text_type="BU", + smooth=True) + printer.text(f"{title}\n") + reset_defaults(printer) + printer.set(align="right", + font="b") + printer.text(f"{date}\n\n") + printer.text(text) + printer.text("\n") + printer.qr(link, size=6) + printer.text("\n") + + +def clear_html(text): + cleanr = re.compile('<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});') + cleantext = re.sub(cleanr, '', text) + return cleantext + + if __name__ == "__main__": printer = initialize() print_weather(printer) printer.text("\n\n") + print_rss(printer, "https://victorhckinthefreeworld.com/feed/") + printer.text("\n\n") + print_rss(printer, "https://pedalibre.org/feed/") + printer.text("\n\n") + print_rss(printer, "https://skyandtelescope.org/observing/sky-at-a-glance/feed/") + printer.text("\n\n") + print_rss(printer, "https://www.space.com/feeds/all", news=2) + printer.text("\n\n")