Adding mastodon API examples
This commit is contained in:
parent
69be97de09
commit
4044203195
2 changed files with 95 additions and 0 deletions
|
|
@ -0,0 +1,57 @@
|
|||
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import ssl
|
||||
import wifi
|
||||
import socketpool
|
||||
import microcontroller
|
||||
import adafruit_requests
|
||||
|
||||
# enter the hashtag that you want to follow
|
||||
hashtag = "CircuitPython"
|
||||
|
||||
# connect to SSID
|
||||
wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD'))
|
||||
|
||||
# add your mastodon token as 'mastodon_token' to your .env file
|
||||
headers = {'Authorization': 'Bearer ' + os.getenv('mastodon_token') + 'read:statuses'}
|
||||
|
||||
pool = socketpool.SocketPool(wifi.radio)
|
||||
requests = adafruit_requests.Session(pool, ssl.create_default_context())
|
||||
|
||||
# initial request, gets most recent matching hashtag post
|
||||
# add your mastodon instance (mastodon.social, tech.lgbt, etc) to your .env file as mastodon_host
|
||||
r = requests.get("https://%s/api/v1/timelines/tag/%s?limit=1" % (os.getenv('mastodon_host'), hashtag), headers=headers) # pylint: disable=line-too-long
|
||||
json_data = r.json()
|
||||
post_id = str(json_data[0]['id'])
|
||||
print("A new #%s post from @%s:" % (hashtag, str(json_data[0]['account']['acct'])))
|
||||
print(re.sub('<[^>]+>', '', json_data[0]['content']))
|
||||
print()
|
||||
|
||||
while True:
|
||||
try:
|
||||
time.sleep(360)
|
||||
# compares post_id to see if a new post to the hashtag has been found
|
||||
r = requests.get("https://%s/api/v1/timelines/tag/%s?since_id=%s" % (os.getenv('mastodon_host'), hashtag, post_id), headers=headers) # pylint: disable=line-too-long
|
||||
json_data = r.json()
|
||||
json_length = len(json_data)
|
||||
# if the id's match, then the json array is empty (length of 0)
|
||||
# otherwise there is a new post
|
||||
if json_length > 0:
|
||||
post_id = str(json_data[0]['id'])
|
||||
print("A new #%s post from @%s:" % (hashtag, str(json_data[0]['account']['acct'])))
|
||||
print(re.sub('<[^>]+>', '', json_data[0]['content']))
|
||||
print()
|
||||
else:
|
||||
print("no new #%s posts" % hashtag)
|
||||
print(json_length)
|
||||
print()
|
||||
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
print("Error:\n", str(e))
|
||||
print("Resetting microcontroller in 10 seconds")
|
||||
time.sleep(10)
|
||||
microcontroller.reset()
|
||||
38
CircuitPython_Mastodon_API_Examples/Send_To_Mastodon/code.py
Normal file
38
CircuitPython_Mastodon_API_Examples/Send_To_Mastodon/code.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
import os
|
||||
import ssl
|
||||
import wifi
|
||||
import socketpool
|
||||
import adafruit_requests
|
||||
|
||||
# add your mastodon token as 'mastodon_token' to your .env file
|
||||
headers = {'Authorization': 'Bearer ' + os.getenv('mastodon_token')}
|
||||
|
||||
# add your mastodon instance to your .env file as mastodon_host
|
||||
url = 'https://' + os.getenv('mastodon_host') + '/api/v1/statuses'
|
||||
|
||||
# connect to SSID
|
||||
wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD'))
|
||||
|
||||
pool = socketpool.SocketPool(wifi.radio)
|
||||
requests = adafruit_requests.Session(pool, ssl.create_default_context())
|
||||
|
||||
# you'll be prompted in the REPL to enter your post text
|
||||
post = input("Please enter your Mastodon post text: ")
|
||||
# pack the post for sending with the API
|
||||
post_text = {"status": post}
|
||||
|
||||
# confirm in the REPL that you want to post by entering y for yes or n for no
|
||||
send_check = input("Send post to Mastodon (y/n)?")
|
||||
|
||||
# if you type y
|
||||
if send_check == "y":
|
||||
# send to mastodon with a POST request
|
||||
r = requests.post(url, data=post_text, headers=headers)
|
||||
print()
|
||||
print("You posted '%s' to Mastodon. Goodbye." % post)
|
||||
# if you type n
|
||||
else:
|
||||
print("You did not post to Mastodon. Goodbye.")
|
||||
Loading…
Reference in a new issue