|
1 | | -# SPDX-FileCopyrightText: 2022 DJDevon3 |
| 1 | +# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries |
2 | 2 | # SPDX-License-Identifier: MIT |
3 | 3 | # Coded for Circuit Python 8.0 |
4 | 4 | """DJDevon3 Adafruit Feather ESP32-S2 Twitter_API_Example""" |
5 | | -# pylint: disable=line-too-long |
6 | 5 | import gc |
7 | 6 | import time |
8 | 7 | import ssl |
|
12 | 11 | import adafruit_requests |
13 | 12 |
|
14 | 13 | # Twitter developer account bearer token required. |
15 | | -# Ensure these are uncommented and in secrets.py or .env |
| 14 | +# Ensure these are uncommented and in secrets.py or .env |
16 | 15 | # "TW_userid": "Your Twitter user id", # numerical id not username |
17 | 16 | # "TW_bearer_token": "Your long API Bearer token", |
18 | 17 |
|
|
43 | 42 | sleep_time_conversion = "days" |
44 | 43 |
|
45 | 44 | # Used with any Twitter 0auth request. |
46 | | -twitter_header = {'Authorization': 'Bearer ' + secrets["TW_bearer_token"]} |
| 45 | +twitter_header = {"Authorization": "Bearer " + secrets["TW_bearer_token"]} |
47 | 46 | TW_SOURCE = ( |
48 | 47 | "https://api.twitter.com/2/users/" |
49 | 48 | + secrets["TW_userid"] |
|
58 | 57 | requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
59 | 58 | while not wifi.radio.ipv4_address: |
60 | 59 | try: |
61 | | - wifi.radio.connect(secrets['ssid'], secrets['password']) |
| 60 | + wifi.radio.connect(secrets["ssid"], secrets["password"]) |
62 | 61 | except ConnectionError as e: |
63 | 62 | print("Connection Error:", e) |
64 | 63 | print("Retrying in 10 seconds") |
|
79 | 78 | except ConnectionError as e: |
80 | 79 | print("Connection Error:", e) |
81 | 80 | print("Retrying in 10 seconds") |
82 | | - |
| 81 | + |
83 | 82 | # Print Full JSON to Serial |
84 | 83 | debug_response = False # Set true to see full response |
85 | 84 | if debug_response: |
86 | 85 | dump_object = json.dumps(tw_json) |
87 | 86 | print("JSON Dump: ", dump_object) |
88 | | - |
| 87 | + |
89 | 88 | # Print to Serial |
90 | 89 | tw_debug_keys = True # Set true to print Serial data |
91 | 90 | if tw_debug_keys: |
92 | | - |
93 | | - tw_userid = tw_json['data']['id'] |
| 91 | + |
| 92 | + tw_userid = tw_json["data"]["id"] |
94 | 93 | print("User ID: ", tw_userid) |
95 | | - |
96 | | - tw_username = tw_json['data']['name'] |
| 94 | + |
| 95 | + tw_username = tw_json["data"]["name"] |
97 | 96 | print("Name: ", tw_username) |
98 | | - |
99 | | - tw_join_date = tw_json['data']['created_at'] |
| 97 | + |
| 98 | + tw_join_date = tw_json["data"]["created_at"] |
100 | 99 | print("Member Since: ", tw_join_date) |
101 | | - |
102 | | - tw_tweets = tw_json['data']['public_metrics']['tweet_count'] |
| 100 | + |
| 101 | + tw_tweets = tw_json["data"]["public_metrics"]["tweet_count"] |
103 | 102 | print("Tweets: ", tw_tweets) |
104 | | - |
105 | | - tw_followers = tw_json['data']['public_metrics']['followers_count'] |
| 103 | + |
| 104 | + tw_followers = tw_json["data"]["public_metrics"]["followers_count"] |
106 | 105 | print("Followers: ", tw_followers) |
107 | | - |
| 106 | + |
108 | 107 | print("Monotonic: ", time.monotonic()) |
109 | 108 |
|
110 | 109 | print("\nFinished!") |
111 | 110 | print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) |
112 | 111 | print("===============================") |
113 | 112 | gc.collect() |
114 | | - # pylint: disable=broad-except |
| 113 | + |
115 | 114 | except (ValueError, RuntimeError) as e: |
116 | 115 | print("Failed to get data, retrying\n", e) |
117 | 116 | time.sleep(60) |
|
0 commit comments