-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: autopost to bluesky (WIP) #789
Open
CharlesSheelam
wants to merge
2
commits into
comses:main
Choose a base branch
from
CharlesSheelam:autopost
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,61 @@ | ||
import requests | ||
from datetime import datetime, timezone | ||
from curator.autopost import settings | ||
from library.models import CodebaseRelease | ||
|
||
|
||
BLUESKY_HANDLE = "test266.bsky.social" | ||
BLUESKY_APP_PASSWORD = "password" | ||
|
||
class Autopost: | ||
def __init__(self): | ||
self.identifier = settings.BLUESKY_HANDLE | ||
self.password = settings.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,7 @@ | ||
import environ | ||
|
||
env = environ.Env() | ||
environ.Env.read_env() | ||
|
||
BLUESKY_HANDLE = env("BLUESKY_HANDLE", default="default_handle") | ||
BLUESKY_APP_PASSWORD = env("BLUESKY_APP_PASSWORD", default="default_password") |
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) | ||
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}")) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium