From 8bb547cfb4fea8225ace03c690ce9d0ae2112b95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Wed, 11 Aug 2021 01:37:47 +0200 Subject: [PATCH] Added gemini protocol and made RSS option work --- requirements.txt | 1 + smallprint.py | 105 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 96 insertions(+), 10 deletions(-) diff --git a/requirements.txt b/requirements.txt index 4f96265..8228dfc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ certifi==2020.6.20 chardet==3.0.4 feedparser==5.2.1 idna==2.10 +ignition-gemini==0.1.7 importlib-metadata==1.7.0 Pillow==7.2.0 pycairo==1.19.1 diff --git a/smallprint.py b/smallprint.py index d30dbfe..9a9338c 100644 --- a/smallprint.py +++ b/smallprint.py @@ -11,6 +11,7 @@ import os.path # 3rd party imports from escpos.printer import Usb, Dummy, File import feedparser +import ignition from PIL import Image import re import requests @@ -41,7 +42,7 @@ def initialize(): printer_endpoint = config.get("PRINTER_ENDPOINT") or 0x01 # return Usb(prid1, prid2, printer_interface, printer_endpoint) - return File("/dev/usb/lp2") + return File("/dev/usb/lp4") def reset_defaults(printer): @@ -108,7 +109,94 @@ def print_weather(printer): logger.error("No weather info available") -def print_rss(printer, link, news=1): +def print_gemini(printer, link): + """ + Given a Gemini link, it prints the file to the printer + and tries to do some interpretation if it is gemtext + """ + response = ignition.request(link) + + if not response.success(): + logger.error(f"Received error {response.status}") + return + + reset_defaults(printer) + if response.meta == "text/gemini": + for raw_line in str(response.data()).split("\n"): + line = raw_line.rstrip() + if len(line) == 0: + printer.text("\n") + continue + + if line.startswith("# "): + printer.set(align="left", + text_type="BU", + width=2, height=2, + smooth=True) + printer.text(line[2:]) + + elif line.startswith("## "): + printer.set(align="left", + text_type="BU", + width=1, height=1, + smooth=True) + printer.text(line[3:]) + + elif line.startswith("### "): + printer.set(align="left", + font="a", + text_type="U", + width=1, height=1, + smooth=True) + printer.text(line[4:]) + + elif line.startswith("#### "): + printer.set(align="left", + font="b", + text_type="U", + width=1, height=1, + smooth=True) + printer.text(line[5:]) + + elif line.startswith("=>"): + reset_defaults(printer) + printer.set(align="left", + font="b") + printer.text(line[3:]) + reset_defaults(printer) + lnk = _process_link(link, line[3:]) + printer.text("\n") + printer.qr(lnk, size=4) + + else: + printer.set(align="left", + font="b") + printer.text(line) + + printer.text("\n") + + else: + printer.text(response.data()) + + +def _process_link(orig_link, link_text): + """ + Extracts the link from the text, and completes it if + necessary + """ + uri = link_text.split()[0] + + if "://" in link_text: + return uri + + if link_text.startswith("/"): + return orig_link + link_text + + else: + return orig_link + "/" + link_text + + +def print_rss(printer, link, news=3): """ Given an RSS link and a printer, prints the news from it """ @@ -222,6 +310,7 @@ def create_parser(): parser.add_argument("--weather", default=False) parser.add_argument("--news", default=False) + parser.add_argument("--gemini", default=False) parser.add_argument("--text", nargs=1, default=None) parser.add_argument("--file", default=None, type=open) parser.add_argument("--image", nargs=1, default=None) @@ -247,15 +336,11 @@ if __name__ == "__main__": print_weather(printer) printer.text("\n\n") + if args.gemini: + print_gemini(printer, args.gemini) + if args.news: - 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") + print_rss(printer, args.news) if args.image: print_image(printer, args.image[0])