-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic create post management command
- Loading branch information
1 parent
96e7743
commit 3903bae
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Error loading related location Loading |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}")) |