|
|
|
@ -1,13 +1,17 @@
|
|
|
|
|
#! /usr/bin/env python
|
|
|
|
|
|
|
|
|
|
# python imports
|
|
|
|
|
import argparse
|
|
|
|
|
import logging
|
|
|
|
|
import math
|
|
|
|
|
import datetime
|
|
|
|
|
import os
|
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
|
|
# 3rd party imports
|
|
|
|
|
from escpos.printer import Usb, Dummy, File
|
|
|
|
|
import feedparser
|
|
|
|
|
from PIL import Image
|
|
|
|
|
import re
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
@ -37,7 +41,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/lp0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reset_defaults(printer):
|
|
|
|
@ -144,21 +148,115 @@ def _print_rss_item(item):
|
|
|
|
|
printer.text("\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_text(printer, text):
|
|
|
|
|
"""
|
|
|
|
|
Prints a text in the smallest form possible
|
|
|
|
|
"""
|
|
|
|
|
reset_defaults(printer)
|
|
|
|
|
# Set the font to a small one and align to
|
|
|
|
|
# the left
|
|
|
|
|
printer.set(align="left",
|
|
|
|
|
text_type="NORMAL",
|
|
|
|
|
font="b",
|
|
|
|
|
smooth=True)
|
|
|
|
|
printer.text("\n")
|
|
|
|
|
printer.text(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_file(printer, file):
|
|
|
|
|
"""
|
|
|
|
|
Prints a file
|
|
|
|
|
"""
|
|
|
|
|
reset_defaults(printer)
|
|
|
|
|
# Set the font to a small one and align to
|
|
|
|
|
# the left
|
|
|
|
|
printer.set(align="left",
|
|
|
|
|
text_type="NORMAL",
|
|
|
|
|
font="b",
|
|
|
|
|
smooth=True)
|
|
|
|
|
|
|
|
|
|
printer.text("\n")
|
|
|
|
|
|
|
|
|
|
for line in file:
|
|
|
|
|
printer.text(line)
|
|
|
|
|
|
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_image(printer, image):
|
|
|
|
|
"""
|
|
|
|
|
Prints an image
|
|
|
|
|
"""
|
|
|
|
|
# Load the image to adjust it
|
|
|
|
|
im = Image.open(image)
|
|
|
|
|
|
|
|
|
|
ratio = float(im.size[0]) / float(im.size[1])
|
|
|
|
|
|
|
|
|
|
if im.size[0] > im.size[1]:
|
|
|
|
|
# The image needs to be rotated
|
|
|
|
|
width = math.floor(384 * ratio)
|
|
|
|
|
im = im.resize((width, 384))
|
|
|
|
|
im = im.transpose(Image.ROTATE_90)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
height = math.floor(384.0 * ratio)
|
|
|
|
|
im = im.resize((384, height))
|
|
|
|
|
|
|
|
|
|
im.save("temp.png")
|
|
|
|
|
printer.hw("INIT")
|
|
|
|
|
printer.image("temp.png")
|
|
|
|
|
os.remove("temp.png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_parser():
|
|
|
|
|
"""
|
|
|
|
|
Create an argpaser for command line interaction
|
|
|
|
|
"""
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
|
|
parser.add_argument("--weather", default=False)
|
|
|
|
|
parser.add_argument("--news", 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)
|
|
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
parser = create_parser()
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
if args.text:
|
|
|
|
|
print_text(printer, args.text[0])
|
|
|
|
|
printer.text("\n\n")
|
|
|
|
|
|
|
|
|
|
if args.file:
|
|
|
|
|
print_file(printer, args.file)
|
|
|
|
|
printer.text("\n\n")
|
|
|
|
|
|
|
|
|
|
if args.weather:
|
|
|
|
|
print_weather(printer)
|
|
|
|
|
printer.text("\n\n")
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
if args.image:
|
|
|
|
|
print_image(printer, args.image[0])
|
|
|
|
|
printer.text("\n\n")
|
|
|
|
|