|
| 1 | +# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | +import os |
| 5 | +import time |
| 6 | +from adafruit_magtag.magtag import MagTag |
| 7 | + |
| 8 | +# Set up where we'll be fetching data from |
| 9 | +DATA_SOURCE = "https://www.adafruit.com/api/quotes.php" |
| 10 | +QUOTE_LOCATION = [0, "text"] |
| 11 | +AUTHOR_LOCATION = [0, "author"] |
| 12 | + |
| 13 | +# the current working directory (where this file is) |
| 14 | +try: |
| 15 | + cwd = os.path.dirname(os.path.realpath(__file__)) |
| 16 | +except AttributeError: |
| 17 | + cwd = ("/" + __file__).rsplit("/", 1)[0] |
| 18 | + |
| 19 | +magtag = MagTag( |
| 20 | + url=DATA_SOURCE, |
| 21 | + json_path=(QUOTE_LOCATION, AUTHOR_LOCATION), |
| 22 | + default_bg=0x000000, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def add_quote_marks(quote_text): |
| 27 | + return f'"{quote_text}"' |
| 28 | + |
| 29 | + |
| 30 | +def add_hyphen(author_name): |
| 31 | + return f"- {author_name}" |
| 32 | + |
| 33 | + |
| 34 | +magtag.add_text( |
| 35 | + text_position=( |
| 36 | + (magtag.graphics.display.width // 2) - 1, |
| 37 | + (magtag.graphics.display.height // 2) - 20, |
| 38 | + ), |
| 39 | + text_color=0xFFFFFF, |
| 40 | + text_wrap=44, |
| 41 | + text_maxlen=180, |
| 42 | + line_spacing=1.0, |
| 43 | + text_transform=add_quote_marks, |
| 44 | + text_anchor_point=(0.5, 0.5), |
| 45 | +) |
| 46 | + |
| 47 | +magtag.add_text( |
| 48 | + text_position=( |
| 49 | + (magtag.graphics.display.width // 2) - 120, |
| 50 | + (magtag.graphics.display.height // 2) + 34, |
| 51 | + ), |
| 52 | + text_color=0xFFFFFF, |
| 53 | + text_wrap=0, |
| 54 | + text_maxlen=30, |
| 55 | + text_transform=add_hyphen, |
| 56 | +) |
| 57 | + |
| 58 | +while True: |
| 59 | + try: |
| 60 | + value = magtag.fetch() |
| 61 | + print("Response is", value) |
| 62 | + except (ValueError, RuntimeError) as e: |
| 63 | + print("Some error occured, retrying! -", e) |
| 64 | + time.sleep(180) |
0 commit comments