|
| 1 | +""" |
| 2 | +Halloween countdown for PyPortal. |
| 3 | +
|
| 4 | +Adafruit invests time and resources providing this open source code. |
| 5 | +Please support Adafruit and open source hardware by purchasing |
| 6 | +products from Adafruit! |
| 7 | +
|
| 8 | +Written by Dave Astels for Adafruit Industries |
| 9 | +Copyright (c) 2019 Adafruit Industries |
| 10 | +Licensed under the MIT license. |
| 11 | +
|
| 12 | +All text above must be included in any redistribution. |
| 13 | +""" |
| 14 | + |
| 15 | +#pylint:disable=invalid-name |
| 16 | +import time |
| 17 | +import binascii |
| 18 | +import json |
| 19 | +import board |
| 20 | +from adafruit_pyportal import PyPortal |
| 21 | +import adafruit_esp32spi.adafruit_esp32spi_requests as requests |
| 22 | + |
| 23 | +username = 'codewisdom' |
| 24 | + |
| 25 | +try: |
| 26 | + from secrets import secrets |
| 27 | +except ImportError: |
| 28 | + print("""WiFi settings are kept in secrets.py, please add them there! |
| 29 | +the secrets dictionary must contain 'ssid' and 'password' at a minimum""") |
| 30 | + raise |
| 31 | + |
| 32 | +def halt_and_catch_fire(message, *args): |
| 33 | + """Log a critical error and stall the system.""" |
| 34 | + print(message % args) |
| 35 | + while True: |
| 36 | + pass |
| 37 | + |
| 38 | +def get_bearer_token(): |
| 39 | + """Get the bearer authentication token from twitter.""" |
| 40 | + raw_key = secrets['twitter_api_key'] + ':' + secrets['twitter_secret_key'] |
| 41 | + encoded_key = binascii.b2a_base64(bytes(raw_key, 'utf8')) |
| 42 | + string_key = bytes.decode(encoded_key) |
| 43 | + headers = {'Authorization': 'Basic ' + string_key, |
| 44 | + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'} |
| 45 | + response = requests.post('https://api.twitter.com/oauth2/token', |
| 46 | + headers=headers, |
| 47 | + data='grant_type=client_credentials') |
| 48 | + response_dict = json.loads(response.content) |
| 49 | + if response_dict['token_type'] != 'bearer': |
| 50 | + halt_and_catch_fire('Wrong token type from twitter: %s', response_dict['token_type']) |
| 51 | + return response_dict['access_token'] |
| 52 | + |
| 53 | +# determine the current working directory |
| 54 | +# needed so we know where to find files |
| 55 | +cwd = ("/"+__file__).rsplit('/', 1)[0] |
| 56 | +url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=1&screen_name=' + username |
| 57 | + |
| 58 | + |
| 59 | +# Initialize the pyportal object and let us know what data to fetch and where |
| 60 | +# to display it |
| 61 | +pyportal = PyPortal(url=url, |
| 62 | + json_path=(0, 'text'), |
| 63 | + status_neopixel=board.NEOPIXEL, |
| 64 | + default_bg=cwd + '/twitter_background.bmp', |
| 65 | + # external_spi=spi, |
| 66 | + # esp=esp, |
| 67 | + text_font=cwd+'/fonts/Helvetica-Bold-16.bdf', |
| 68 | + text_position=(20, 60), |
| 69 | + text_color=0xFFFFFF, |
| 70 | + text_wrap=35, |
| 71 | + caption_text='@' + username, |
| 72 | + caption_font=cwd+'/fonts/Helvetica-Bold-16.bdf', |
| 73 | + caption_position=(5, 210), |
| 74 | + caption_color=0x808080) |
| 75 | + |
| 76 | +bearer_token = get_bearer_token() |
| 77 | + |
| 78 | +pyportal.set_headers({'Authorization': 'Bearer ' + bearer_token}) |
| 79 | + |
| 80 | +while True: |
| 81 | + pyportal.fetch() |
| 82 | + time.sleep(3600) # check every hour |
0 commit comments