Skip to content

Commit

Permalink
Merge pull request #19 from dracos/master
Browse files Browse the repository at this point in the history
Add Bluesky support.
  • Loading branch information
russss authored Aug 29, 2024
2 parents d23e69f + 16609f7 commit 22021ef
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM python:3.9-alpine
WORKDIR /polybot
COPY . .
RUN apk add --no-cache --virtual .build-deps gcc musl-dev libffi-dev openssl-dev && \
pip install requests mastodon.py tweepy && \
pip install requests mastodon.py tweepy atproto && \
pip install . && \
apk del .build-deps && \
rm -Rf /polybot
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ It currently only supports post-only bots.

## Features

* Automatically post to both Twitter and Mastodon.
* Automatically post to Twitter, Mastodon, Bluesky.
* A friendly setup interface to handle the OAuth hassle for you.
* Automatic state persistence - just put your state in the `self.state`
dict and it'll get saved/restored across runs.
Expand Down
61 changes: 59 additions & 2 deletions polybot/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import textwrap
import mimetypes
from typing import List, Type, Union # noqa
from atproto import Client, models
from mastodon import Mastodon as MastodonClient
import tweepy
import requests
Expand Down Expand Up @@ -97,7 +98,12 @@ def do_wrapped(
line, lat=lat, lon=lon, in_reply_to_id=in_reply_to_id
)

if hasattr(out, "id"):
if isinstance(out, models.com.atproto.repo.strong_ref.Main):
if first:
in_reply_to_id = {"root": out, "parent": out}
else:
in_reply_to_id["parent"] = out
elif hasattr(out, "id"):
in_reply_to_id = out.id
else:
in_reply_to_id = out.data["id"]
Expand Down Expand Up @@ -320,4 +326,55 @@ def do_post(
raise PostError(e)


ALL_SERVICES = [Twitter, Mastodon] # type: List[Type[Service]]
class Bluesky(Service):
name = "bluesky"
max_length = 300
max_length_image = 300

def auth(self):
self.bluesky = Client()
self.bluesky.login(
self.config.get("bluesky", "email"), self.config.get("bluesky", "password")
)
self.log.info("Connected to Bluesky")

def setup(self):
print("We need your Bluesky email and password")
email = input("Email: ")
password = input("Password: ")
self.config.add_section("bluesky")
self.config.set("bluesky", "email", email)
self.config.set("bluesky", "password", password)
return True

def do_post(
self,
status,
imagefile=None,
mime_type=None,
lat=None,
lon=None,
in_reply_to_id=None,
):
if in_reply_to_id:
in_reply_to_id = models.AppBskyFeedPost.ReplyRef(
parent=in_reply_to_id["parent"], root=in_reply_to_id["root"]
)
try:
if imagefile:
if not isinstance(imagefile, list):
imagefile = [imagefile]
resp = self.bluesky.send_images(
status, imagefile, None, self.bluesky.me.did, in_reply_to_id
)
else:
resp = self.bluesky.send_post(
status, self.bluesky.me.did, in_reply_to_id
)
return models.create_strong_ref(resp)

except Exception as e:
raise PostError(e)


ALL_SERVICES = [Twitter, Mastodon, Bluesky] # type: List[Type[Service]]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"Programming Language :: Python :: 3",
],
packages=["polybot"],
install_requires=["tweepy==4.12.1", "Mastodon.py==1.8.0"],
install_requires=["tweepy==4.12.1", "Mastodon.py==1.8.0", "atproto==0.0.49"],
)

0 comments on commit 22021ef

Please sign in to comment.