/ Dashblock_API / code.py
code.py
 1  # SPDX-FileCopyrightText: 2019 Isaac Wellish for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  Dashblock API Adafruit Learn Guide Count demo
 7  Use Dashblock to create a custom API for learn.adafruit.com,
 8  then display the number of learn guides on the site
 9  """
10  
11  import time
12  import board
13  from adafruit_pyportal import PyPortal
14  
15  # Get wifi details and more from a secrets.py file
16  try:
17      from secrets import secrets
18  except ImportError:
19      print("WiFi settings are kept in settings.py, please add them there!")
20      raise
21  
22  # Set up where we'll be fetching data from
23  DATA_SOURCE = "https://api.dashblock.io/model/v1?api_key=" + secrets['dashblock_key']
24  GUIDE_COUNT = ['entities', 0, 'guide count']
25  CAPTION = 'total tutorials:'
26  
27  # determine the current working directory
28  # needed so we know where to find files
29  cwd = ("/"+__file__).rsplit('/', 1)[0]
30  
31  # Initialize the pyportal object and let us know what data to fetch and where
32  # to display it
33  pyportal = PyPortal(url=DATA_SOURCE,
34                      json_path = (GUIDE_COUNT),
35                      status_neopixel=board.NEOPIXEL,
36                      default_bg=cwd+"/adabot_cover.bmp",
37                      text_font=cwd+"/fonts/Collegiate-50.bdf",
38                      text_position=((40, 100)),
39                      text_color=(0x8080FF),
40                      text_maxlen=(4), # max text length, only want first 4 chars for number of guides
41                      caption_text=CAPTION,
42                      caption_font=cwd+"/fonts/Collegiate-24.bdf",
43                      caption_position=(40, 60),
44                      caption_color=0xFFFFFF)
45  
46  # track the last value so we can play a sound when it updates
47  last_value = 0
48  
49  while True:
50      try:
51          value = pyportal.fetch()
52          print("Response is", value)
53          int_value = int(value[:4]) # save only first 4 chars and cast to int
54          if last_value < int_value:  # ooh it went up!
55              print("New guide!")
56              pyportal.play_file(cwd+"/coin.wav")  # make a noise!
57          last_value = int_value
58      except RuntimeError as e:
59          print("Some error occured, retrying! -", e)
60      except ValueError as e:
61          print("Value error occured, retrying! -", e)
62          continue
63  
64      time.sleep(600) #update every 10 mins