|
|
|
@ -2,13 +2,14 @@
|
|
|
|
|
|
|
|
|
|
# python imports
|
|
|
|
|
import logging
|
|
|
|
|
import sys
|
|
|
|
|
import datetime
|
|
|
|
|
import random
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
@ -43,7 +44,7 @@ def reset_defaults(printer):
|
|
|
|
|
"""
|
|
|
|
|
Reset the printer to the defaults
|
|
|
|
|
"""
|
|
|
|
|
printer.set(align='left', font='a', width=1,
|
|
|
|
|
printer.set(align='left', font='a', width=1, text_type="normal",
|
|
|
|
|
height=1, density=9, invert=False, smooth=False, flip=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -69,15 +70,27 @@ def print_weather(printer):
|
|
|
|
|
current_day = today.strftime("%a, %d %b, %Y")
|
|
|
|
|
|
|
|
|
|
printer.set(align="center",
|
|
|
|
|
font="a")
|
|
|
|
|
font="a",
|
|
|
|
|
text_type="b")
|
|
|
|
|
printer.text(f"{current_day}\n\n{city}\n")
|
|
|
|
|
|
|
|
|
|
reset_defaults(printer)
|
|
|
|
|
printer.set(align="center", font="b")
|
|
|
|
|
|
|
|
|
|
description = weather['weather'][0]['description']
|
|
|
|
|
printer.text(f"{description}\n")
|
|
|
|
|
printer.text(f"{description}\n\n")
|
|
|
|
|
icon_code = weather['weather'][0]['icon']
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(f"icons/weather/{icon_code}.png"):
|
|
|
|
|
icon_path = "icons/weather/any.png"
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
# TODO: Print a nice icon based on the codes here: https://openweathermap.org/weather-conditions
|
|
|
|
|
temperature = weather['main']['temp']
|
|
|
|
|
humidity = weather['main']['humidity']
|
|
|
|
@ -91,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")
|
|
|
|
|