Architecture Design
Overview
Social Publisher follows a microservice-like architecture, decoupling the "publishing" logic from the main application. It uses a producer-consumer model for handling tasks.
mermaid
graph TD
Client[Web/Main App] -->|HTTP POST| API[FastAPI Server]
API -->|Insert| DB[(MySQL Database)]
API -->|Response| Client
Worker[Background Worker] -->|Poll| DB
Worker -->|Execute| Publisher[Publisher Service]
Publisher -->|Control| Playwright[Playwright Browser]
Playwright -->|Interact| Douyin[Douyin Web Creator]Components
1. API Server (app.py)
- Framework: FastAPI.
- Role: Receives requests, validates input, creates task records in the database, and queries task status.
- Endpoints:
/publish,/accounts, etc.
2. Database (database.py)
- ORM: SQLModel (SQLAlchemy wrapper).
- Storage: MySQL.
- Model:
PublishTaskstores task parameters, status, and results.
3. Background Worker (worker.py)
- Role: Continuously polls the database for tasks with
status="pending". - Concurrency: Processes one task at a time (per worker instance) to ensure browser stability and avoid resource contention.
- Logic:
- Fetch pending task.
- Update status to
running. - Invoke
PublisherService. - Update status to
successorfailedbased on outcome.
4. Publisher Service (service/publisher_service.py)
- Role: Orchestrates the publishing flow.
- Responsibilities:
- Resource resolution: Downloads files from URLs to local temp storage.
- Browser management: Initializes
BrowserEngineviaAccountManager. - Platform delegation: Calls specific platform publishers (e.g.,
DouyinPublisher).
5. Platform Implementation (platforms/douyin/publisher.py)
- Role: Implements the actual automation steps for Douyin.
- Steps:
- Login Check: Verifies session validty. If invalid, waits for QR scan.
- Navigation: Goes to the upload page.
- Upload: Interacts with the file input to upload the video.
- Form Filling: Fills title, description, tags.
- Settings: Configures cover, schedule time, declarations.
- Submission: Clicks the publish button and verifies success.
6. Account Manager (core/account.py)
- Role: Manages browser contexts and user data.
- Data: Stores browser profiles (cookies, local storage) in
data/{account_name}. - Persistence: Ensures that once a user logs in, the session is preserved across service restarts.
Key Design Decisions
- Playwright over Selenium: Chosen for better performance, stability, and native support for modern web features (like shadow DOM and handling multiple contexts).
- Playwright Stealth: Essential for bypassing bot detection mechanisms on Douyin.
- Synchronous Polling Worker: Chosen for simplicity and reliability over complex message queues (like Redis/RabbitMQ) for this scale. MySQL serves as the queue.