code.py
 1  # SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  import board
 7  from adafruit_pyportal import PyPortal
 8  
 9  # Get wifi details and more from a secrets.py file
10  try:
11      from secrets import secrets
12  except ImportError:
13      print("WiFi secrets are kept in secrets.py, please add them there!")
14      raise
15  
16  PROJECT_NAME = "3c92f0"
17  
18  # Set up where we'll be fetching data from
19  DATA_SOURCE = "https://api.hackster.io/v2/projects/"+PROJECT_NAME+"?"
20  DATA_SOURCE += "client_id="+secrets['hackster_clientid']
21  DATA_SOURCE += "&client_secret="+secrets['hackster_secret']
22  VIEWS_LOCATION = ['stats', 'views']
23  LIKES_LOCATION = ['stats', 'respects']
24  CAPTION = "http://www.hackster.com/project/"+PROJECT_NAME
25  
26  # the current working directory (where this file is)
27  cwd = ("/"+__file__).rsplit('/', 1)[0]
28  pyportal = PyPortal(url=DATA_SOURCE, json_path=(LIKES_LOCATION, VIEWS_LOCATION),
29                      status_neopixel=board.NEOPIXEL,
30                      default_bg=cwd+"/hackster_background.bmp",
31                      text_font=cwd+"/fonts/Arial-Bold-24.bdf",
32                      text_position=((80, 75), (80, 145)),
33                      text_color=(0x0000FF, 0x000000),
34                      caption_text=CAPTION,
35                      caption_font=cwd+"/fonts/Arial-12.bdf",
36                      caption_position=(20, 200),
37                      caption_color=0x000000)
38  
39  # track the last value so we can play a sound when it updates
40  last_likes = 0
41  
42  while True:
43      try:
44          likes, views = pyportal.fetch()
45          print("Views", views, "Likes", likes)
46          if last_likes < likes:  # ooh it went up!
47              print("New respect!")
48              pyportal.play_file(cwd+"/coin.wav")
49          last_likes = likes
50      except RuntimeError as e:
51          print("Some error occured, retrying! -", e)
52  
53      time.sleep(60)