From 240bcf92f8a41885152e1825d8e9870db14f7da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Mon, 31 Aug 2020 23:36:47 +0200 Subject: [PATCH] First commit: Weather basic --- .gitignore | 4 ++ README.md | 7 ++++ __init__.py | 1 + config.py.example | 9 +++++ smallprint.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 __init__.py create mode 100644 config.py.example create mode 100644 smallprint.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58bbfe9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.pyc +.pyo +config.py +__pycache__ diff --git a/README.md b/README.md new file mode 100644 index 0000000..6bdca7c --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +SmallPrint +========== + +This project aims to create a ticket generator +for small thermal paper printers (ESC/POS +compatible) so you can take your most interesting +information with you in a small form. diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..b794fd4 --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ +__version__ = '0.1.0' diff --git a/config.py.example b/config.py.example new file mode 100644 index 0000000..bfdc214 --- /dev/null +++ b/config.py.example @@ -0,0 +1,9 @@ +# Simple configuration example + +config = { + "OWM": "Your OpenWeatherMap token geos here", + "CITY": "City name", + "PRINTER_USB_ID": "", + "PRINTER_INTERFACE": 0, + "PRINTER_ENDPOINT": 0x01, +} diff --git a/smallprint.py b/smallprint.py new file mode 100644 index 0000000..fd0dd2f --- /dev/null +++ b/smallprint.py @@ -0,0 +1,97 @@ +#! /usr/bin/env python + +# python imports +import logging +import sys +import datetime +import random + +# 3rd party imports +import requests +from escpos.printer import Usb, Dummy, File + +from config import config + + +# Enable logging +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) + +logger = logging.getLogger(__name__) + + +def initialize(): + """ + Initializes the printer and returns a printer object to + generate the print + """ + printer_id = config.get("PRINTER_USB_ID") + + if not printer_id: + logger.error("Please configure your printer") + + prid1, prid2 = printer_id.split(":") + + printer_interface = config.get("PRINTER_INTERFACE") or 0 + printer_endpoint = config.get("PRINTER_ENDPOINT") or 0x01 + + # return Usb(prid1, prid2, printer_interface, printer_endpoint) + return File("/dev/usb/lp2") + + +def reset_defaults(printer): + """ + Reset the printer to the defaults + """ + printer.set(align='left', font='a', width=1, + height=1, density=9, invert=False, smooth=False, flip=False) + + +def print_weather(printer): + reset_defaults(printer) + appkey = config.get("OWM") + if not appkey: + logger.error("Open Weather key not set!") + return + + city = config.get("CITY") + + if not city: + logger.error("No city set") + + params = {"q": city, "APPID": appkey, "units": "metric"} + + weatherdata = requests.get('http://api.openweathermap.org/data/2.5/weather', params=params) + + if "weather" in weatherdata.json(): + weather = weatherdata.json() + today = datetime.datetime.now() + current_day = today.strftime("%a, %d %b, %Y") + + printer.set(align="center", + font="a") + 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") + icon_code = weather['weather'][0]['icon'] + # TODO: Print a nice icon based on the codes here: https://openweathermap.org/weather-conditions + temperature = weather['main']['temp'] + humidity = weather['main']['humidity'] + wind = weather['wind']['speed'] + + printer.text(f"Temperature: {temperature}C\n") + printer.text(f"Humidity: {humidity}%\n") + printer.text(f"Wind: {wind}km\\h\n") + + else: + logger.error("No weather info available") + + +if __name__ == "__main__": + printer = initialize() + print_weather(printer) + printer.text("\n\n")