/ Twitter_API / code.py
code.py
 1  # SPDX-FileCopyrightText: 2019 Dave Astels for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  Twitter API for PyPortal.
 7  
 8  Adafruit invests time and resources providing this open source code.
 9  Please support Adafruit and open source hardware by purchasing
10  products from Adafruit!
11  
12  Written by Dave Astels for Adafruit Industries
13  Copyright (c) 2019 Adafruit Industries
14  Licensed under the MIT license.
15  
16  All text above must be included in any redistribution.
17  """
18  
19  #pylint:disable=invalid-name
20  import time
21  import board
22  from adafruit_pyportal import PyPortal
23  
24  try:
25      from secrets import secrets
26  except ImportError:
27      print("""WiFi settings are kept in secrets.py, please add them there!
28  the secrets dictionary must contain 'ssid' and 'password' at a minimum""")
29      raise
30  
31  # Set this to the username you'd like to display tweets from
32  username = 'codewisdom'
33  
34  # determine the current working directory
35  # needed so we know where to find files
36  cwd = ("/"+__file__).rsplit('/', 1)[0]
37  url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=1&screen_name=' + username
38  
39  
40  # Initialize the pyportal object and let us know what data to fetch and where
41  # to display it
42  pyportal = PyPortal(url=url,
43                      json_path=(0, 'text'),
44                      status_neopixel=board.NEOPIXEL,
45                      default_bg=cwd + '/twitter_background.bmp',
46                      text_font=cwd+'/fonts/Helvetica-Bold-16.bdf',
47                      text_position=(20, 60),
48                      text_color=0xFFFFFF,
49                      text_wrap=35,
50                      caption_text='@' + username,
51                      caption_font=cwd+'/fonts/Helvetica-Bold-16.bdf',
52                      caption_position=(5, 210),
53                      caption_color=0x808080)
54  
55  # Set OAuth2.0 Bearer Token
56  bearer_token = secrets['twitter_bearer_token']
57  pyportal.set_headers({'Authorization': 'Bearer ' + bearer_token})
58  
59  while True:
60      pyportal.fetch()
61      time.sleep(3600)       # check every hour