Skip to content

Instantly share code, notes, and snippets.

@anaisbetts
Last active September 22, 2023 17:25
Show Gist options
  • Save anaisbetts/012031fa1d742ceafc820fb36278c8d7 to your computer and use it in GitHub Desktop.
Save anaisbetts/012031fa1d742ceafc820fb36278c8d7 to your computer and use it in GitHub Desktop.
Make a sane HTTP API for Vestaboard
import os
import vestaboard
import json
import threading
from dotenv import load_dotenv
from flask import Flask, request
load_dotenv()
API_KEY = os.environ["VESTABOARD_API_KEY"]
API_SECRET = os.environ["VESTABOARD_API_SECRET"]
RW_API_KEY = os.environ["VESTABOARD_READ_WRITE_KEY"]
SUB_ID = os.environ["VESTABOARD_SUB_ID"]
LOCAL_API_KEY = os.environ["VESTABOARD_LOCAL_API_KEY"]
LOCAL_IP_ADDRESS = os.environ["VESTABOARD_LOCAL_IP_ADDRESS"]
api = Flask(__name__)
board = vestaboard.Board(
apiKey=API_KEY,
apiSecret=API_SECRET,
subscriptionId=SUB_ID)
readBoard = vestaboard.Board(
apiKey=RW_API_KEY,
apiSecret=API_SECRET,
readWrite=True,
subscriptionId=SUB_ID)
# localApi={ "key": LOCAL_API_KEY, "ip": LOCAL_IP_ADDRESS })
pending_board = None
@api.route("/", methods=["GET"])
def hello():
return "Hello world\n" + json.dumps(readBoard.read())
@api.route("/text", methods=["POST"])
def text():
global pending_board
print(request.json)
if (pending_board is not None):
return "busy"
board.post(request.json["text"])
return "ok"
def repost_pending():
global pending_board
pb = pending_board
pending_board = None
if (pb is not None):
print(f"Reposting {json.dumps(pb)}")
board.raw(pb)
@api.route("/notify", methods=["POST"])
def notify():
global pending_board
delay_sec = 45
if 'delay' in request.json:
delay_sec = int(request.json["delay"])
if 'text' not in request.json:
return "no text"
pending_board = json.loads(readBoard.read()["currentMessage"]["layout"])
print(f"Pending {json.dumps(pending_board)}")
print(f"Posting {request.json['text']} for {delay_sec} seconds")
board.post(request.json["text"])
timer = threading.Timer(delay_sec, repost_pending)
timer.start()
return "ok"
@api.route("/raw", methods=["POST"])
def raw():
if (pending_board is not None):
return "busy"
board.raw(request.json.raw)
return "ok"
if __name__ == "__main__":
print("Vestaboard Server now running on port 6563")
api.run(host="0.0.0.0", port=6563)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment