Skip to content

Instantly share code, notes, and snippets.

@vwillcox
Last active November 16, 2024 09:51
Show Gist options
  • Save vwillcox/956bc5056f46d152b2650ba78053ef5e to your computer and use it in GitHub Desktop.
Save vwillcox/956bc5056f46d152b2650ba78053ef5e to your computer and use it in GitHub Desktop.
A Simple script in my journey to allow me to Cross post between BlueSky and Mastodon. (Repost)
#!/usr/bin/env python3
from atproto import Client, client_utils
import requests, argparse
import apis
accountname = apis.masto_account_name
accessToken = apis.masto_accessToken
bskyaccount = apis.bsky_account_name
bskypassword = apis.bsky_account_password
api_url = 'https://fosstodon.org/'
headers = {'Authorization': f'Bearer {accessToken}'}
url_account = f'{api_url}api/v1/accounts/verify_credentials'
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--post", help="What are you posting", required=True)
parser.add_argument("-u", "--urls", default = '', help="Any URL you want to post", required=False)
parser.add_argument("-t", "--hash", default = '', help="Hastags to add to the bottom of the post", required=False)
args = parser.parse_args()
config = vars(args)
response_account = requests.get(url_account, headers=headers)
account_info = response_account.json()
account_id = account_info['id']
post_text = args.post
if args.urls:
post_url = args.urls
urls = post_url.split(' ')
else:
post_url = None
urls = None
if args.hash:
hashtags = args.hash
words = hashtags.split(' ')
all_withhash = ['#' + word for word in words]
all_hash = ' '.join(all_withhash)
#all_hash = all_hash[1:]
else:
hashtags = None
def bsky_main(posting, bskyaccount, bskypassword, hashtags=None, post_urls=None):
client = Client()
client.login(bskyaccount,bskypassword)
text_builder = client_utils.TextBuilder().text(posting)
if hashtags:
for hashtag in hashtags:
text_builder = text_builder.tag(hashtag, hashtag).text(' ')
if post_urls:
for post_url in post_urls:
text_builder = text_builder.tag(post_url, post_url).text(' ')
post = client.send_post(text_builder)
def masto_main(posting, post_urls=None, hashtag=None):
if hashtag and post_urls:
#status_message = posting + f'\n' + f'\n' + '\n' + post_urls + '#'+ hashtag
status_message = posting + f'\n' + f'\n' + '\n' + post_urls + f'\n' + hashtag
elif hashtag and not post_urls:
status_message = posting + f'\n' + f'\n' + hashtag
elif not hashtag and post_urls:
status_message = posting + f'\n' + f'\n' + post_urls
elif not hashtag and not post_urls:
status_message = posting
media_url = f'{api_url}/api/v2/media'
headers = {'Authorization': f'Bearer {accessToken}'}
status_url = f'{api_url}/api/v1/statuses'
data = {
'status': status_message
}
response = requests.post(status_url, headers=headers, data=data)
print(f'Status post response status code: {response.status_code}')
if response.status_code == 200:
print('Status posted successfully!')
else:
print(f'Error posting status: {response.status_code}')
def main():
bsky_main(post_text, bskyaccount, bskypassword, words, urls)
masto_main(post_text, post_url, all_hash)
if __name__ == '__main__':
main()
@vwillcox
Copy link
Author

Still to do:

  • Image/Video posting
  • Split longer BlueSky messages into smaller posts
  • Code cleanup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment