Skip to content

Instantly share code, notes, and snippets.

@DuqueDeTuring
Last active November 16, 2022 12:43
Show Gist options
  • Save DuqueDeTuring/75a270affb564873d9e709f227f57414 to your computer and use it in GitHub Desktop.
Save DuqueDeTuring/75a270affb564873d9e709f227f57414 to your computer and use it in GitHub Desktop.
quick&crude test of mastodon's api with micropython
import network
import json
import urequests
from machine import Pin, Timer, I2C
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
HTTP_OK = 200
AUTH_HEADER = {"Authorization":"Bearer API_KEY"}
I2C_ADDR = 0x27
I2C_NUM_ROWS = 4
I2C_NUM_COLS = 20
def get_new_notification(last_id):
r = None
if last_id:
r = urequests.get(f"https://mastodon.social/api/v1/notifications?since_id{last_notification_id}=&limit=1", headers=AUTH_HEADER)
else:
r = urequests.get(f"https://mastodon.social/api/v1/notifications?limit=1", headers=AUTH_HEADER)
if r.status_code == HTTP_OK:
text = r.text
data = json.loads(text)
return data[0]
else:
print(r.status_code)
return None
def get_last_toot(last_id):
r = None
if last_id:
r = urequests.get(f"https://mastodon.social/api/v1/timelines/home?since_id{last_id}=&limit=1", headers=AUTH_HEADER)
else:
r = urequests.get(f"https://mastodon.social/api/v1/timelines/home?limit=1", headers=AUTH_HEADER)
if r.status_code == HTTP_OK:
text = r.text
try:
data = json.loads(text)
return data[0]
except:
print(text)
return None
else:
print(r.status_code)
return None
def blink(timer):
led.toggle()
# print(r.status_code)
#t = r.text
#j = json.loads(t)
#print(j[0]["account"]["username"])
#print(j[0]["content"])
led = Pin(11, Pin.OUT)
timer = Timer()
wlan=network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_NAME, WIFI_PASSWD)
print(wlan.isconnected())
last_id = None
last_notification_id = None
is_a_new_notification = False
last_toot_id = None
i2c = I2C(1, sda=machine.Pin(14), scl=machine.Pin(15), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
while True:
notification = get_new_notification(last_notification_id)
if notification:
is_a_new_notification = last_notification_id != notification["id"]
last_notification_id = notification["id"]
last_notification_time = time.gmtime()
print(last_notification_id)
if is_a_new_notification:
print(f"es nueva:{is_a_new_notification}")
timer.init(freq=3.0, mode=Timer.PERIODIC, callback=blink)
toot = get_last_toot(last_toot_id)
if toot and toot["id"] != last_toot_id:
last_toot_id = toot["id"]
if "reblog" in toot.keys():
lcd.putstr(toot["reblog"]["content"])
else:
lcd.putstr(toot["content"])
time.sleep(30)
timer.deinit()
led.off()
@DuqueDeTuring
Copy link
Author

ideas:

  • preprocess API results with a full Python or Go program running elsewhere and pushing simplified, cleaned posts to an MQTT (or SQS?) topic and then use that "cleaned" content just for display/alerting with a Pico W , in other words, use the Pico W for as little processing as possible and move the Mastodon's API processing "out".
  • display user icon/avatar in a small separated screen (oled, tft)
  • cache user icons with a micro SD card module connected to the Pico W
  • button to clear notification alert
  • cache posts and rotate them when there is no new post in the timeline and show them for X amount of time
  • alert/show errors (out of memory, connection state, other exceptions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment