Add Flask-based application for collecting and archiving Mastodon posts from configured accounts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2.7 KiB
2.7 KiB
Mastodon Collector
Collects posts, replies, and mentions from a list of Mastodon accounts and stores them in PostgreSQL. Includes a web UI for account management and data browsing, plus JSON/CSV APIs for your analysis pipeline.
Quick Start
# 1. Add accounts to monitor
echo "@user@mastodon.social" >> accounts.txt
# 2. Start everything
docker compose up -d
# 3. Open the dashboard
open http://localhost:8585
Architecture
| Service | Description | Port |
|---|---|---|
| db | PostgreSQL 16 | 5432 |
| web | Flask dashboard (Gunicorn) | 8585 |
| collector | Background service, polls every 4 hours | — |
Adding Accounts
Two methods:
- Text file — edit
accounts.txt, one handle per line (@user@instance.social). Picked up on next collection cycle. - Web UI — go to http://localhost:8585/accounts and use the form.
Configuration
Edit .env to customize:
POSTGRES_PASSWORD=collector_secret # Change for production
FLASK_SECRET_KEY=change-me-in-production
POLL_INTERVAL_SECONDS=14400 # Default: 4 hours (14400s)
API Endpoints
For plugging into your analysis pipeline:
| Endpoint | Description |
|---|---|
GET /api/stats |
Overview stats (counts by type) |
GET /api/statuses |
Paginated statuses as JSON |
GET /export |
Download all statuses as CSV |
/api/statuses parameters
page— page number (default: 1)per_page— results per page (default: 100, max: 500)account_id— filter by internal account IDtype— filter by status type:post,reply,mention,reblogsince— ISO datetime, only return statuses after this time
Database Schema
Main tables:
monitored_accounts— accounts being trackedstatuses— collected posts with plain text + HTML contentmentions— who was @-mentioned in each statusmedia_attachments— images/videos attached to statusestags— hashtags usedcollection_logs— audit trail of each collection run
Each status stores raw_json with the full Mastodon API response for future analysis needs.
Moving to a Server
# Copy the project
scp -r mastodon-collector/ user@server:~/
# On the server
cd mastodon-collector
# Edit .env with production secrets
docker compose up -d
Stopping
docker compose down # Stop services, keep data
docker compose down -v # Stop services AND delete database