Skip to content

Commit

Permalink
feat: basic create post management command
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesSheelam committed Jan 9, 2025
1 parent 96e7743 commit 3903bae
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
64 changes: 64 additions & 0 deletions django/curator/autopost/autopost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import requests
from datetime import datetime, timezone
from django.conf import settings
from library.models import CodebaseRelease


BLUESKY_HANDLE = "test266.bsky.social"
BLUESKY_APP_PASSWORD = "password"

class Autopost:
def __init__(self):
#initialize with creds
#user: test266.bsky.social
#password: password
self.identifier = BLUESKY_HANDLE
self.password = BLUESKY_APP_PASSWORD
self.access_token = None
self.did = None

def authenticate(self):
#handle authentication here
response = requests.post(
"https://bsky.social/xrpc/com.atproto.server.createSession",
json={
"identifier": self.identifier,
"password": self.password,
},
)
response.raise_for_status()
session = response.json()
self.access_token = session["accessJwt"]
self.did = session["did"]

def create_post(self, text):
#create a post here and post to bluesky
if not self.access_token or not self.did:
raise ValueError("Must authenticate before creating a post.")


post_content = {
"$type": "app.bsky.feed.post",
"text": text,
"createdAt": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
}

response = requests.post(
"https://bsky.social/xrpc/com.atproto.repo.createRecord",
headers={"Authorization": f"Bearer {self.access_token}"},
json={
"repo": self.did,
"collection": "app.bsky.feed.post",
"record": post_content,
},
)
response.raise_for_status()
return response.json()


def autopost_to_bluesky(text):
#function to autopost to Bluesky.
autopost = Autopost()
autopost.authenticate()
return autopost.create_post(text)

6 changes: 6 additions & 0 deletions django/curator/autopost/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from .views import autopost_view

urlpatterns = [
path('autopost/', autopost_view, name='autopost'),
]
10 changes: 10 additions & 0 deletions django/curator/autopost/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.http import JsonResponse
from .autopost import autopost_to_bluesky

def autopost_view(request):
try:
text = "This is an autopost"
response = autopost_to_bluesky(text)
return JsonResponse({"success": True, "data": response})
except Exception as e:
return JsonResponse({"error": str(e)}, status=500)

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
16 changes: 16 additions & 0 deletions django/curator/management/commands/create_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.core.management.base import BaseCommand
from curator.autopost.autopost import autopost_to_bluesky

class Command(BaseCommand):
help = 'Post a message to Bluesky'

def add_arguments(self, parser):
parser.add_argument('message', type=str, help='The message to post on Bluesky')

def handle(self, *args, **options):
message = options['message']
try:
response = autopost_to_bluesky(message)
self.stdout.write(self.style.SUCCESS(f"Post created successfully: {response}"))
except Exception as e:
self.stderr.write(self.style.ERROR(f"Error creating post: {e}"))

0 comments on commit 3903bae

Please sign in to comment.