Merge branch 'main' into fullscreen_weather_module
This commit is contained in:
commit
ff33f19c71
12
clear_display.py
Normal file
12
clear_display.py
Normal file
@ -0,0 +1,12 @@
|
||||
"""
|
||||
Clears the display of any content.
|
||||
"""
|
||||
from inkycal import Inkycal
|
||||
|
||||
print("loading Inkycal and display driver...")
|
||||
inky = Inkycal(render=True) # Initialise Inkycal
|
||||
print("clearing display...")
|
||||
inky.calibrate(cycles=1) # Calibrate the display
|
||||
print("clear complete...")
|
||||
|
||||
print("finished!")
|
@ -148,7 +148,7 @@ to improve rendering on E-Papers. Set this to False for 9.7” E-Paper.</p></li>
|
||||
</dl>
|
||||
<dl class="py method">
|
||||
<dt class="sig sig-object py" id="inkycal.main.Inkycal.calibrate">
|
||||
<span class="sig-name descname"><span class="pre">calibrate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#inkycal.main.Inkycal.calibrate" title="Link to this definition"></a></dt>
|
||||
<span class="sig-name descname"><span class="pre">calibrate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cycles</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">3</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inkycal.main.Inkycal.calibrate" title="Link to this definition"></a></dt>
|
||||
<dd><p>Calibrate the E-Paper display</p>
|
||||
<p>Uses the Display class to calibrate the display with the default of 3
|
||||
cycles. After a refresh cycle, a new image is generated and shown.</p>
|
||||
@ -466,7 +466,7 @@ than the image height.</p>
|
||||
|
||||
<dl class="py method">
|
||||
<dt class="sig sig-object py" id="inkycal.modules.inky_image.Inkyimage.load">
|
||||
<span class="sig-name descname"><span class="pre">load</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inkycal.modules.inky_image.Inkyimage.load" title="Link to this definition"></a></dt>
|
||||
<span class="sig-name descname"><span class="pre">load</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><span class="pre">None</span></span></span><a class="headerlink" href="#inkycal.modules.inky_image.Inkyimage.load" title="Link to this definition"></a></dt>
|
||||
<dd><p>loads an image from a URL or filepath.</p>
|
||||
<dl class="simple">
|
||||
<dt>Args:</dt><dd><ul class="simple">
|
||||
|
File diff suppressed because one or more lines are too long
@ -532,14 +532,14 @@ class Inkycal:
|
||||
image = Image.fromarray(buffer)
|
||||
return image
|
||||
|
||||
def calibrate(self):
|
||||
def calibrate(self, cycles=3):
|
||||
"""Calibrate the E-Paper display
|
||||
|
||||
Uses the Display class to calibrate the display with the default of 3
|
||||
cycles. After a refresh cycle, a new image is generated and shown.
|
||||
"""
|
||||
|
||||
self.Display.calibrate()
|
||||
self.Display.calibrate(cycles=cycles)
|
||||
|
||||
def _calibration_check(self):
|
||||
"""Calibration scheduler
|
||||
|
@ -11,3 +11,4 @@ from .inkycal_textfile_to_display import TextToDisplay
|
||||
from .inkycal_webshot import Webshot
|
||||
from .inkycal_xkcd import Xkcd
|
||||
from .inkycal_fullweather import Fullweather
|
||||
from .inkycal_tindie import Tindie
|
||||
|
@ -29,7 +29,7 @@ class Inkyimage:
|
||||
# give an OK message
|
||||
logger.info(f'{__name__} loaded')
|
||||
|
||||
def load(self, path):
|
||||
def load(self, path:str) -> None:
|
||||
"""loads an image from a URL or filepath.
|
||||
|
||||
Args:
|
||||
|
107
inkycal/modules/inkycal_tindie.py
Executable file
107
inkycal/modules/inkycal_tindie.py
Executable file
@ -0,0 +1,107 @@
|
||||
"""
|
||||
Tindie module for Inkycal Project
|
||||
Shows unshipped orders from your Tindie store
|
||||
|
||||
Copyright by aceinnolab
|
||||
"""
|
||||
import json
|
||||
|
||||
import arrow
|
||||
|
||||
from inkycal.custom import *
|
||||
from inkycal.modules.template import inkycal_module
|
||||
|
||||
# Show less logging for request module
|
||||
logging.getLogger("urllib3").setLevel(logging.WARNING)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Tindie(inkycal_module):
|
||||
"""Tindie - show latest orders from your store"""
|
||||
|
||||
def __init__(self, config):
|
||||
"""Initialize inkycal_feeds module"""
|
||||
|
||||
super().__init__(config)
|
||||
|
||||
config = config['config']
|
||||
self.api_key = config['api_key']
|
||||
self.username = config['username']
|
||||
# todo implement mode
|
||||
# self.mode = config['mode'] # unshipped_orders, shipped_orders, all_orders
|
||||
|
||||
# give an OK message
|
||||
print(f'{__name__} loaded')
|
||||
|
||||
def generate_image(self):
|
||||
"""Generate image for this module"""
|
||||
# Define new image size with respect to padding
|
||||
im_width = int(self.width - (2 * self.padding_left))
|
||||
im_height = int(self.height - (2 * self.padding_top))
|
||||
im_size = im_width, im_height
|
||||
logger.info(f'image size: {im_width} x {im_height} px')
|
||||
|
||||
# Create an image for black pixels and one for coloured pixels
|
||||
im_black = Image.new('RGB', size=im_size, color='white')
|
||||
im_colour = Image.new('RGB', size=im_size, color='white')
|
||||
|
||||
# Check if internet is available
|
||||
if internet_available():
|
||||
logger.info('Connection test passed')
|
||||
else:
|
||||
raise NetworkNotReachableError
|
||||
|
||||
# Set some parameters for formatting feeds
|
||||
line_spacing = 5
|
||||
text_bbox = self.font.getbbox("hg")
|
||||
line_height = text_bbox[3] + line_spacing
|
||||
line_width = im_width
|
||||
max_lines = (im_height // (line_height + line_spacing))
|
||||
|
||||
logger.debug(f"max_lines: {max_lines}")
|
||||
|
||||
# Calculate padding from top so the lines look centralised
|
||||
spacing_top = int(im_height % line_height / 2)
|
||||
|
||||
# Calculate line_positions
|
||||
line_positions = [
|
||||
(0, spacing_top + _ * line_height) for _ in range(max_lines)]
|
||||
|
||||
logger.debug(f'line positions: {line_positions}')
|
||||
|
||||
# Make the API call
|
||||
url = f"https://www.tindie.com/api/v1/order/?format=json&username={self.username}&api_key={self.api_key}"
|
||||
header = {"accept": "text/json"}
|
||||
response = requests.get(url, headers=header, params={"shipped": "false", "limit": "50"})
|
||||
if response.status_code != 200:
|
||||
logger.error(f"Failed to get orders, status code: {response.status_code}, reason: {response.reason}")
|
||||
raise AssertionError("Failed to get orders")
|
||||
else:
|
||||
logger.info("Orders received")
|
||||
|
||||
text = []
|
||||
|
||||
orders = json.loads(response.text)["orders"]
|
||||
text.append(f"You have {len(orders)} unshipped orders")
|
||||
previous_date = None
|
||||
for index, order in enumerate(orders, start=1):
|
||||
items = order["items"]
|
||||
date = arrow.get(order["date"]).to("local").format("YY/MM/DD")
|
||||
if not previous_date or previous_date != date:
|
||||
text.append(date)
|
||||
previous_date = date
|
||||
user_name = order["shipping_name"]
|
||||
text.append(f"{index}) {user_name} from {order['shipping_country_code']} ordered {len(items)} items!")
|
||||
|
||||
for pos, line in enumerate(text):
|
||||
if pos > max_lines - 1:
|
||||
logger.error(f'Ran out of lines! Required {len(text)} lines but only {max_lines} available')
|
||||
break
|
||||
if pos == 0:
|
||||
write(im_colour, line_positions[pos], (line_width, line_height), line, font=self.font, alignment='left')
|
||||
else:
|
||||
write(im_black, line_positions[pos], (line_width, line_height), line, font=self.font, alignment='left')
|
||||
|
||||
# Return images for black and colour channels
|
||||
return im_black, im_colour
|
@ -31,6 +31,10 @@ class Config:
|
||||
|
||||
TEST_SETTINGS_PATH = f"{basedir}/settings.json"
|
||||
|
||||
# inkycal_tindie_test
|
||||
TINDIE_API_KEY = get("TINDIE_API_KEY")
|
||||
TINDIE_USERNAME = get("TINDIE_USERNAME")
|
||||
|
||||
|
||||
|
||||
|
||||
|
72
tests/test_inkycal_tindie.py
Executable file
72
tests/test_inkycal_tindie.py
Executable file
@ -0,0 +1,72 @@
|
||||
"""
|
||||
inkycal_Tindie unittest
|
||||
"""
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
from inkycal.modules import Tindie
|
||||
from inkycal.modules.inky_image import Inkyimage
|
||||
from tests import Config
|
||||
|
||||
preview = Inkyimage.preview
|
||||
merge = Inkyimage.merge
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
tindie_api_key = Config.TINDIE_API_KEY
|
||||
tindie_username = Config.TINDIE_USERNAME
|
||||
|
||||
tests = [
|
||||
{
|
||||
"name": "Tindie",
|
||||
"config": {
|
||||
"size": [300, 100],
|
||||
"padding_x": 10,
|
||||
"padding_y": 10,
|
||||
"fontsize": 12,
|
||||
"language": "en",
|
||||
"api_key": tindie_api_key,
|
||||
"username": tindie_username,
|
||||
"mode": "unshipped_orders"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "Tindie",
|
||||
"config": {
|
||||
"size": [300, 150],
|
||||
"padding_x": 10,
|
||||
"padding_y": 10,
|
||||
"fontsize": 12,
|
||||
"language": "en",
|
||||
"api_key": tindie_api_key,
|
||||
"username": tindie_username,
|
||||
"mode": "unshipped_orders"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "Tindie",
|
||||
"config": {
|
||||
"size": [300, 800],
|
||||
"padding_x": 10,
|
||||
"padding_y": 10,
|
||||
"fontsize": 18,
|
||||
"language": "en",
|
||||
"api_key": tindie_api_key,
|
||||
"username": tindie_username,
|
||||
"mode": "unshipped_orders"
|
||||
}
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
class TestTindie(unittest.TestCase):
|
||||
|
||||
def test_generate_image(self):
|
||||
for test in tests:
|
||||
logger.info(f'test {tests.index(test) + 1} generating image..')
|
||||
module = Tindie(test)
|
||||
im_black, im_colour = module.generate_image()
|
||||
logger.info('OK')
|
||||
if Config.USE_PREVIEW:
|
||||
preview(merge(im_black, im_colour))
|
Loading…
Reference in New Issue
Block a user