Skip to content

Instantly share code, notes, and snippets.

@paultopia
Created August 20, 2023 17:02
Show Gist options
  • Save paultopia/fa8200b913860ad4617431740b9d0580 to your computer and use it in GitHub Desktop.
Save paultopia/fa8200b913860ad4617431740b9d0580 to your computer and use it in GitHub Desktop.
quick and dirty bluesky posting script
# Quick and dirty script to post via API, mostly for the purposes of
# making threads without having to manually break up at character count breaks (in version 2)
# largely adapted/swiped from the more full-featured library at https://github.com/ianklatzco/atprototools/tree/master
import requests
import datetime
# Authentication information
# n.b.: ketchain is a pythonista (ipad/ios python app) library, for a real computer use envirnoment variables instead
import keychain
username = "YOUREMAIL@HERE.COM"
password = keychain.get_password("bluesky", username)
# Session information
server = "https://bsky.social"
REPO = "" # I think this is an internal account identifier?
AUTH_TOKEN = ""
# should really just do all of this in a class like a proper python guy but fuckit
def login(username, password):
endpoint = server + "/xrpc/com.atproto.server.createSession"
data = {"identifier": username, "password": password}
response = requests.post(endpoint, json=data)
return response.json().get("did"), response.json().get('accessJwt')
REPO, AUTH_TOKEN = login(username, password)
def post(content):
endpoint = server + "/xrpc/com.atproto.repo.createRecord"
headers = {"Authorization": "Bearer " + AUTH_TOKEN}
timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat().replace('+00:00', 'Z')
data = {
"collection": "app.bsky.feed.post",
"$type": "app.bsky.feed.post",
"repo": REPO,
"record": {
"$type": "app.bsky.feed.post",
"createdAt": timestamp,
"text": content
}
}
resp = requests.post(endpoint, json=data, headers=headers)
return resp
post("Ignore this, I'm testing a script to post via API.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment