Skip to main content

Serializes classic literature to Mastodon

Project description

Chapter by Chapter

A Python bot that serializes classic literature to Mastodon, posting one chapter at a time in a daily schedule.

Overview

Chapter by Chapter is a Mastodon bot built as a plug for the robotooter framework. It takes classic literature from Project Gutenberg and posts it chapter by chapter to social media. The bot processes text files, extracts chapters, and creates engaging daily posts with customizable templates.

Features

  • Automated Chapter Processing: Converts full books into individual chapters with metadata
  • Customizable Templates: Flexible templating system for post formatting
  • Daily Scheduling: Posts chapters on a configurable schedule
  • Project Gutenberg Integration: Direct support for Project Gutenberg text files
  • Rich Content Creation: Generates engaging introductions and endings.
  • Mastodon Integration: Built on robotooter for reliable social media posting

Examples

  • Chapter By Chapter] The reason I created this. Posts a chapter a day from Public Domain litereature. Victorian-era Netflix.

Installation

Prerequisites

  • Python 3.12+
  • RoboTooter 0.2.2
  • Poetry for dependency management

Installation

Via pip

# Create a virtual environment
mkdir my_bot
cd my_bot
python3 -m venv .venv
# Activate the environment
. .venv/bin/activate
# And install...
pip install chapter_by_chapter

From source

  1. Clone the repository:
git clone <repository-url>
cd chapter-by-chapter
  1. Install dependencies:
just init
# or manually: poetry install

Usage

Setup

You will need to register the plugin, and then create a new bot.

 robotooter plugins register --plugin chapter_by_chapter
chapter_by_chapter plugin registered

# You will now see the new bot installed robotooter info
RobotoTooter info
Version: 0.2.0
Root directory: /Users/bryan.fordham/.robotooter
Filters:
  BlankLineFilter
  GutenbergFilter
  ParagraphCombiningFilter
Tooters:
  MarkovBot
  ChapterByChapterBot

And now create the bot, selecting ChapterByChapter as the bot you want to use.

 robotooter create
Let's get your bot's information.
Enter the bot name: test
Enter the username, such as 'mybot@mastodon.social': test@not.real
1. ChapterByChapterBot
2. MarkovBot
Select an option: 1
...

Setting up a Book

To configure a new book for serialization:

robotooter -b <botname> setup --book KEY --file PATH
# or
robotooter -b <botname> setup --book KEY --url URL
  • KEY will be how you refer to the book, and will also be the directory name for its files.
    • So, for example, for A Study in Scarlet I used a_study_in_scarlet but you can use anything.
  • PATH A local file path to a Project Gutenberg text file
  • URL A URL to download a text file

Example:

robotooter -b my_literature_bot setup -f /path/to/book.txt -bk "scarlet"
robotooter -b my_literature_bot setup -u "https://gutenberg.org/files/244/244.txt" -bk "study"

Setting the Book

This bot is mean to do most things automatically. So you have to tell it which book to post.

Examples:

# Defaults to chapter 1
robotooter --book a_study_in_scarlet

# You can set a specific chapter to start from, if you want
robotooter --book a_study_in_scarlet --chapter 2

Posting Content

After you've told it which book to use, it will post the next chapter automatically.

robotooter -b <botname> toot 

If you run the command again, it will post the next chapter.

Development

Code Quality

Run linting and type checking:

just lint    # Run ruff linter with auto-fix
just type    # Run mypy type checker
just build   # Run both lint and type checks, then build

Testing

Run the test suite:

poetry run pytest

Project Structure

src/chapter_by_chapter/
    bot.py              # Main bot implementation
    content_creator.py  # Content generation and templating
    greeter.py         # User interaction handling
    models.py          # Data models and configuration
    processor.py       # Text processing and chapter extraction
    util.py           # Utility functions
        resources/
        templates/     # Post templates
        word_choices.json

Configuration

The bot uses a configuration system based on robotooter's BotConfig. Key configuration options include:

  • Book Processing: Chapter extraction settings
  • Content Templates: Customizable post formatting
  • Scheduling: Daily posting configuration
  • Social Media: Mastodon API credentials

Templates

The bot includes several customizable templates. When you create a new both they are copied to the bot's resources directory. So, if you create "chapter_bot" you will find the templates in <robotooter_root>/chapter_bot/resources/templates/:

  • chapter_text.txt - Main chapter content format
  • book_intro/ - Book introduction variations
  • daily_intro/ - Daily post introductions
  • book_ending/ - Book conclusion formats
  • chapter_spoiler.txt - Spoiler warning template

The templates use jinja2 as the template engine.

When a specific template is called for, it will first to see if there is a template directory that matches. If so, it will randomly pick one of the files in the directoy as the template to use. This allows for some variety.

If there is not a directory that matches, it will look for "{template_name}.txt".

So, as an example, if you try to render the template book_intro, out of the box it will look in the templates/book_intro directory and pick a template. If you want to always use the same template, remove the directory and create templates/book_intro.txt.

Contributing

If you would like to add features, please feel free. If you use this yourself, please let me know!

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). See the LICENSE file for details.

The AGPL-3.0 is a copyleft license that requires anyone who distributes the code or runs it on a server to make the source code available under the same license terms.

Author

Bryan L. Fordham bfordham@naughtybaptist.com

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

chapter_by_chapter-0.1.1.tar.gz (31.8 kB view details)

Uploaded Source

Built Distribution

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

chapter_by_chapter-0.1.1-py3-none-any.whl (40.3 kB view details)

Uploaded Python 3

File details

Details for the file chapter_by_chapter-0.1.1.tar.gz.

File metadata

  • Download URL: chapter_by_chapter-0.1.1.tar.gz
  • Upload date:
  • Size: 31.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/24.6.0

File hashes

Hashes for chapter_by_chapter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 535d4de433599e71a7922d74ced12fa2cb298f04c832cd7e4324fd8aeeaa2d73
MD5 335572868626b0e3d11b4eb382ae0301
BLAKE2b-256 880cd82330a6dd136f96433c18d9ee1ae894cd797464294536e29e2247415f54

See more details on using hashes here.

File details

Details for the file chapter_by_chapter-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: chapter_by_chapter-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 40.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/24.6.0

File hashes

Hashes for chapter_by_chapter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ee8a05da35ba1a83fbe8d7ec2c1c20f0b6666894e3283f62e660c62a354d616
MD5 14381842e12133004eea0787dab28b63
BLAKE2b-256 d10ccec2bf3950039a05f1f256ed15980b247b39913ba8481cb231f4c035348a

See more details on using hashes here.

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