|
| 1 | +# SPDX-FileCopyrightText: 2022 DJDevon3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Coded for Circuit Python 8.0 |
| 4 | +"""DJDevon3 Adafruit Feather ESP32-S2 Discord_API_Example""" |
| 5 | +# pylint: disable=line-too-long |
| 6 | +import gc |
| 7 | +import time |
| 8 | +import ssl |
| 9 | +import wifi |
| 10 | +import json |
| 11 | +import socketpool |
| 12 | +import adafruit_requests |
| 13 | + |
| 14 | +# Web scrape authorization key required |
| 15 | +# Learn how: https://github.com/lorenz234/Discord-Data-Scraping |
| 16 | + |
| 17 | +# Ensure these are uncommented and in secrets.py or .env |
| 18 | +# "Discord_Adafruit_Channel": "327254708534116352", # Adafruit Channel ID |
| 19 | +# "Discord_Authorization": "Discord Authorization from browser console" |
| 20 | + |
| 21 | +# Initialize WiFi Pool (There can be only 1 pool & top of script) |
| 22 | +pool = socketpool.SocketPool(wifi.radio) |
| 23 | + |
| 24 | +# Time between API refreshes |
| 25 | +# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour |
| 26 | +sleep_time = 900 |
| 27 | + |
| 28 | +try: |
| 29 | + from secrets import secrets |
| 30 | +except ImportError: |
| 31 | + print("Secrets File Import Error") |
| 32 | + raise |
| 33 | + |
| 34 | +if sleep_time < 60: |
| 35 | + sleep_time_conversion = "seconds" |
| 36 | + sleep_int = sleep_time |
| 37 | +elif 60 <= sleep_time < 3600: |
| 38 | + sleep_int = sleep_time / 60 |
| 39 | + sleep_time_conversion = "minutes" |
| 40 | +elif 3600 <= sleep_time < 86400: |
| 41 | + sleep_int = sleep_time / 60 / 60 |
| 42 | + sleep_time_conversion = "hours" |
| 43 | +else: |
| 44 | + sleep_int = sleep_time / 60 / 60 / 24 |
| 45 | + sleep_time_conversion = "days" |
| 46 | + |
| 47 | +discord_header = {'Authorization': '' + secrets['Discord_Authorization']} |
| 48 | +ADA_SOURCE = ( |
| 49 | + "https://discord.com/api/v10/guilds/" |
| 50 | + + secrets['Discord_Adafruit_Channel'] |
| 51 | + + "/preview" |
| 52 | +) |
| 53 | + |
| 54 | +# Connect to Wi-Fi |
| 55 | +print("\n===============================") |
| 56 | +print("Connecting to WiFi...") |
| 57 | +requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
| 58 | +while not wifi.radio.ipv4_address: |
| 59 | + try: |
| 60 | + wifi.radio.connect(secrets['ssid'], secrets['password']) |
| 61 | + except ConnectionError as e: |
| 62 | + print("Connection Error:", e) |
| 63 | + print("Retrying in 10 seconds") |
| 64 | + time.sleep(10) |
| 65 | + gc.collect() |
| 66 | +print("Connected!\n") |
| 67 | + |
| 68 | +while True: |
| 69 | + try: |
| 70 | + print("\nAttempting to GET DISCORD PREVIEW!") # -------------------------------- |
| 71 | + # Print Request to Serial |
| 72 | + debug_request = False # Set true to see full request |
| 73 | + if debug_request: |
| 74 | + print("Full API GET URL: ", ADA_SOURCE) |
| 75 | + print("===============================") |
| 76 | + try: |
| 77 | + ada_res = requests.get(url=ADA_SOURCE, headers=discord_header).json() |
| 78 | + except ConnectionError as e: |
| 79 | + print("Connection Error:", e) |
| 80 | + print("Retrying in 10 seconds") |
| 81 | + |
| 82 | + # Print Full JSON to Serial |
| 83 | + discord_debug_response = False # Change to true to see full response |
| 84 | + if discord_debug_response: |
| 85 | + ada_discord_dump_object = json.dumps(ada_res) |
| 86 | + print("JSON Dump: ", ada_discord_dump_object) |
| 87 | + |
| 88 | + # Print keys to Serial |
| 89 | + discord_debug_keys = True # Set to True to print Serial data |
| 90 | + if discord_debug_keys: |
| 91 | + |
| 92 | + ada_discord_all_members = ada_res['approximate_member_count'] |
| 93 | + print("Members: ", ada_discord_all_members) |
| 94 | + |
| 95 | + ada_discord_all_members_online = ada_res['approximate_presence_count'] |
| 96 | + print("Online: ", ada_discord_all_members_online) |
| 97 | + |
| 98 | + print("Monotonic: ", time.monotonic()) |
| 99 | + |
| 100 | + print("\nFinished!") |
| 101 | + print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion)) |
| 102 | + print("===============================") |
| 103 | + gc.collect() |
| 104 | + # pylint: disable=broad-except |
| 105 | + except (ValueError, RuntimeError) as e: |
| 106 | + print("Failed to get data, retrying\n", e) |
| 107 | + time.sleep(60) |
| 108 | + continue |
| 109 | + time.sleep(sleep_time) |
0 commit comments