/ PyPortal_Word_of_the_Day / code.py
code.py
1 # SPDX-FileCopyrightText: 2019 Isaac Wellish for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 This example uses the Wordnik API to display Wordnik's Word of the Day. 7 Each day a new word, its part of speech, and definition 8 will appear automatically on the display. Tap the screen to start 9 as well as to switch between the word's definition and an example sentence. 10 """ 11 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.wordnik.com/v4/words.json/wordOfTheDay?api_key="+secrets['wordnik_token'] 24 PART_OF_SPEECH = ['definitions', 0, 'partOfSpeech'] 25 DEF_LOCATION = ['definitions', 0, 'text'] 26 EXAMPLE_LOCATION = ['examples', 0, 'text'] 27 CAPTION = 'wordnik.com/word-of-the-day' 28 DEFINITION_EXAMPLE_ARR = [DEF_LOCATION, EXAMPLE_LOCATION] 29 #defintion and example array variable initialized at 0 30 definition_example = 0 31 32 # determine the current working directory 33 # needed so we know where to find files 34 cwd = ("/"+__file__).rsplit('/', 1)[0] 35 36 # Initialize the pyportal object and let us know what data to fetch and where 37 # to display it 38 pyportal = PyPortal(url=DATA_SOURCE, 39 status_neopixel=board.NEOPIXEL, 40 default_bg=cwd+"/wordoftheday_background.bmp", 41 text_font=cwd+"/fonts/Arial-ItalicMT-17.bdf", 42 text_position=((50, 30), # word location 43 (50, 50), # part of speech location 44 (50, 135)), # definition location 45 text_color=(0x8080FF, 46 0xFF00FF, 47 0xFFFFFF), 48 text_wrap=(0, # characters to wrap for text 49 0, 50 28), 51 text_maxlen=(180, 30, 115), # max text size for word, part of speech and def 52 caption_text=CAPTION, 53 caption_font=cwd+"/fonts/Arial-ItalicMT-17.bdf", 54 caption_position=(50, 220), 55 caption_color=0x808080) 56 57 print("loading...") # print to repl while waiting for font to load 58 pyportal.preload_font() # speed things up by preloading font 59 pyportal.set_text("\nWord of the Day") # show title 60 61 while True: 62 if pyportal.touchscreen.touch_point: 63 try: 64 #set the JSON path here to be able to change between definition and example 65 # pylint: disable=protected-access 66 pyportal._json_path=(['word'], 67 PART_OF_SPEECH, 68 DEFINITION_EXAMPLE_ARR[definition_example]) 69 value = pyportal.fetch() 70 print("Response is", value) 71 except RuntimeError as e: 72 print("Some error occured, retrying! -", e) 73 #Change between definition and example 74 if definition_example == 0: 75 definition_example = 1 76 elif definition_example == 1: 77 definition_example = 0