Skip to main content

A tool to synchronize posts from Bluesky to Mastodon

Project description

BlueMastodon

A Python tool to automatically cross-post manual Bluesky posts to Mastodon.

Formerly known as bluemastodon

Version Status Test Status Python Versions License

Overview

BlueMastodon monitors your Bluesky account for new posts and automatically cross-posts them to your Mastodon account. It's designed to be run as a scheduled job via GitHub Actions or any other scheduler.

This tool specifically handles synchronizing manual posts from Bluesky to Mastodon. Blog post publishing to social platforms can be managed by a separate tool.

Features

  • 🔄 Automatic Synchronization: Fetches recent Bluesky posts and cross-posts to Mastodon
  • 🖼️ Media Support: Transfers images and attachments between platforms
  • 🔗 Link Handling: Preserves external links in your posts
  • 🧠 Smart Synchronization: Keeps track of synced posts to avoid duplicates
  • 📝 Format Preservation: Maintains post formatting during cross-posting
  • 🚨 Error Handling: Robust error handling and logging
  • 🔒 Secure: Handles API credentials securely
  • 🤖 GitHub Action Ready: Easily deployable as a GitHub Actions workflow

Setup

Prerequisites

  • Python 3.10 or higher (tested on 3.10, 3.11, 3.12, 3.13)
  • A Bluesky account
  • A Mastodon account with API access

Installation

Using pip

Install directly from PyPI:

pip install bluemastodon

After installation, you can run the tool from the command line:

# Basic usage
bluemastodon --config /path/to/your/.env

# Dry run (won't post to Mastodon, just shows what would be posted)
bluemastodon --config /path/to/your/.env --dry-run

# With debug logging enabled
bluemastodon --config /path/to/your/.env --debug

# Specify state file location (default is sync_state.json in current directory)
bluemastodon --config /path/to/your/.env --state /path/to/state.json

You can also use it as a library in your Python code:

from bluemastodon import Config, SyncManager

# Create configuration
config = Config(
    bluesky_username="your_username.bsky.social",
    bluesky_password="your_app_password",
    mastodon_instance_url="https://your.mastodon.instance",
    mastodon_access_token="your_access_token",
    lookback_hours=6,
)

# Initialize sync manager
sync_manager = SyncManager(config, state_file="sync_state.json")

# Run the sync
new_records = sync_manager.run_sync()
print(f"Synced {len(new_records)} posts")

Using Poetry

  1. Clone the repository:
git clone https://github.com/kelp/bluemastodon.git
cd bluemastodon
  1. Install with Poetry:
poetry install

Configuration

Create a .env file with your credentials:

# Required credentials
BLUESKY_USERNAME=your_bluesky_username
BLUESKY_PASSWORD=your_bluesky_password
MASTODON_INSTANCE_URL=https://your.mastodon.instance
MASTODON_ACCESS_TOKEN=your_mastodon_access_token

# Optional settings with defaults
LOOKBACK_HOURS=24                # How far back to look for posts
SYNC_INTERVAL_MINUTES=60         # Frequency of synchronization
MAX_POSTS_PER_RUN=5              # Maximum posts to sync in one run
INCLUDE_MEDIA=true               # Include media attachments
INCLUDE_LINKS=true               # Include post links

Obtaining API Credentials

Bluesky

For Bluesky, you'll need your username and application password.

Mastodon
  1. Log in to your Mastodon instance
  2. Go to Preferences > Development > New Application
  3. Give your app a name and select the following permissions:
    • read:accounts
    • read:statuses
    • write:media
    • write:statuses
  4. Save and copy the "Access token"

Usage

Command Line

Run the sync manually:

# If installed with pip
bluemastodon

# If using Poetry
poetry run python -m social_sync

Options

-c, --config    Path to custom config file (.env format)
-s, --state     Path to custom state file (JSON format)
-d, --debug     Enable debug logging
--dry-run       Simulate syncing without posting

Example Commands

# Run with debug logging
bluemastodon --debug

# Dry run (no actual posts)
bluemastodon --dry-run

# Use custom config file
bluemastodon --config /path/to/custom.env

# Specify state file location
bluemastodon --state /path/to/state.json

Scheduled Sync with GitHub Actions

Setting Up Securely with GitHub Secrets

BlueMastodon is designed to work with GitHub Actions in a secure way, using GitHub's encrypted secrets feature to store sensitive credentials:

  1. Fork this repository or copy the workflow files to your own repository
  2. In your GitHub repository, go to "Settings" > "Secrets and variables" > "Actions"
  3. Add the following repository secrets (these are encrypted and secure):
    • BLUESKY_USERNAME: Your Bluesky username
    • BLUESKY_PASSWORD: Your Bluesky application password
    • MASTODON_INSTANCE_URL: Your Mastodon instance URL
    • MASTODON_ACCESS_TOKEN: Your Mastodon API access token

For detailed instructions on obtaining and setting up these secrets, see the GitHub Secrets Setup Guide.

SECURITY WARNING: Never commit your actual credentials to the repository! Always use GitHub Secrets for sensitive information.

Optional Configuration Variables

You can also add these as repository variables (not secrets, as they're not sensitive):

  1. Go to "Settings" > "Secrets and variables" > "Actions" > "Variables" tab
  2. Configure any of the following:
    • LOOKBACK_HOURS: How far back to look for posts (default: 24)
    • SYNC_INTERVAL_MINUTES: How often to sync (default: 60)
    • MAX_POSTS_PER_RUN: Maximum posts to sync (default: 5)
    • INCLUDE_MEDIA: Whether to include media (default: true)
    • INCLUDE_LINKS: Whether to include links (default: true)

Running the Workflow

  1. Enable the workflow in your GitHub repository (go to "Actions" tab)
  2. The sync will run automatically according to the schedule (hourly by default)
  3. You can also trigger it manually from the Actions tab by clicking "Run workflow"
  4. For manual runs, you can enable debug mode or dry-run mode using the provided options

How Duplicate Posts Are Prevented

BlueMastodon uses a robust state tracking system to prevent duplicate posts:

  1. State File: Each run maintains a JSON file with IDs of previously synced posts
  2. GitHub Actions Cache: The state file is persisted between runs using GitHub's cache
  3. Lookback Window: The tool only examines posts from the last 6 hours (configurable)
  4. Deduplication Logic: Posts with IDs already in the state file are automatically skipped

This system ensures:

  • Each post is only synced once, even across multiple workflow runs
  • If a workflow run is missed, posts are still captured in the next run
  • Even if the cache is lost, only posts within the lookback window might be duplicated

For advanced cache management options (including how to clear the cache), see Managing GitHub Actions Cache.

Development

Version Information

BlueMastodon follows Semantic Versioning and is currently at version 0.9.4 (beta). For more information about our versioning system and release process, see Versioning Guidelines.

Setup Development Environment

# Clone the repository
git clone https://github.com/kelp/bluemastodon.git
cd bluemastodon

# Run the setup script (installs dependencies and pre-commit hooks)
./scripts/setup-dev.sh

# Or manually:
# Install dependencies
poetry install

# Install pre-commit hooks
pre-commit install
pre-commit install --hook-type pre-push

Pre-commit Hooks

This project uses pre-commit hooks to ensure code quality. The hooks run:

  • Code formatting (Black, isort)
  • Linting (Flake8, pylint)
  • Type checking (Mypy)
  • Security analysis (Bandit)
  • Tests with minimum coverage requirements (pytest)

Pre-commit hooks run automatically on commit. Pre-push hooks run more comprehensive checks before pushing to the remote repository.

Testing

Run tests with coverage:

# Run all tests
make test

# Run with coverage report
make test-cov

Linting and Formatting

# Format code
make format

# Run linting
make lint

# Run type checking
make type-check

Building and Publishing

# Build package
make build

# Create a release
make release

# Publish to PyPI (requires credentials)
make publish

Troubleshooting

Common Issues

  1. Authentication Errors:

    • Verify your credentials in the .env file
    • Ensure your Mastodon token has the correct permissions
  2. No Posts Being Synced:

    • Check the LOOKBACK_HOURS setting
    • Verify that you have recent posts on Bluesky
    • Run with --debug for detailed logging
  3. Media Not Transferring:

    • Ensure INCLUDE_MEDIA=true in your configuration
    • Some media types might not be supported by both platforms
  4. GitHub Actions Not Running:

    • Check if the workflow is enabled
    • Verify all required secrets are set
    • Check the workflow logs for errors
  5. Security Concerns:

    • Never commit .env files with real credentials to your repository
    • Always use GitHub Secrets for sensitive information
    • Regularly rotate your API tokens for better security
    • If you suspect your tokens were compromised, regenerate them immediately
  6. Cache Issues:

    • If you experience duplicate posts, the GitHub Actions cache may have been lost
    • Cache can expire after 7 days of non-use or during GitHub maintenance
    • To debug cache issues, check workflow logs for "Cache restored from key: sync-state"
    • For instructions on how to view, clear, or reset the cache, see Managing GitHub Actions Cache

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Commit your changes: git commit -am 'Add feature'
  4. Push to the branch: git push origin feature-name
  5. Submit a pull request

Acknowledgements

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bluemastodon-0.9.4.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bluemastodon-0.9.4-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file bluemastodon-0.9.4.tar.gz.

File metadata

  • Download URL: bluemastodon-0.9.4.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for bluemastodon-0.9.4.tar.gz
Algorithm Hash digest
SHA256 573fd609eb14848103c429f3e22674e79dd6105adae54f97ff4c99aa52d8df34
MD5 7b1349234245db7a13d0d95c97c807db
BLAKE2b-256 267855de5b8e6ac14e9e1939375b99fc8f4c431e3106e6d3e0c57cdcc0bc9007

See more details on using hashes here.

Provenance

The following attestation bundles were made for bluemastodon-0.9.4.tar.gz:

Publisher: python-release.yml on kelp/bluemastodon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bluemastodon-0.9.4-py3-none-any.whl.

File metadata

  • Download URL: bluemastodon-0.9.4-py3-none-any.whl
  • Upload date:
  • Size: 20.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for bluemastodon-0.9.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d85b3125cb02aa9f9ffac835ac3b6eb2aca4ad5dd13a252a3e352941200085c9
MD5 f4eea00fcc50ea489e306d53e91f0d74
BLAKE2b-256 7617f7e29d03a99494b74e66be09627c6ffe64fb9403b9109f4cd17f44609802

See more details on using hashes here.

Provenance

The following attestation bundles were made for bluemastodon-0.9.4-py3-none-any.whl:

Publisher: python-release.yml on kelp/bluemastodon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page