A unified platform abstraction layer for posting content to multiple social media platforms while maintaining Open/Closed Principle and anti-detection techniques.
Abstract base class PlatformHandler providing:
-
Abstract Methods:
post(content, options)- Platform-specific postingvalidate_auth()- Authentication validation
-
Common Behavior:
- Logging with timestamps and task IDs
- Error handling and structured logging
- Retry logic with exponential backoff (max 3 attempts)
- Randomized delays for anti-detection
- Content normalization (markdown stripping, whitespace normalization)
- Randomized viewport sizes for browser automation
-
Configuration:
- Platform-specific character limits
- Field limits (title, summary, hashtags, etc.)
Features:
- Title Extraction: First 200 chars (max 300), no trailing period
- Summary Extraction: Remaining content (max 3000)
- Tag Extraction: Max 5 tags, 25 chars each, uppercase format (#TAG), no duplicates
- Article Link: Included in all posts
- Formatting: Professional tone, no trailing period on title
- Anti-Detection: Randomized delays (0.5-2.5s), randomized viewports
Features:
- 280-Character Limit: Truncation with ellipsis
- Hashtag Formatting: #Hashtag camelCase, max 3 tags, no duplicates
- Hook Preservation: First line as strong hook
- Article Link: Preserved at end of tweet
- Randomized Delays: 5-10 seconds between posts
- Batch Posting: Support for posting multiple tweets sequentially
- Anti-Detection: Randomized delays, randomized viewports
- Already inherits from
PlatformHandler - Compatible with the abstraction layer
- Uses same pattern for posting
Main entry point with:
-
Routing Function:
post(content, options) -> dict- Normalizes content
- Routes to correct platform handler
- Validates authentication
- Handles retries and errors
- Returns structured result
-
Content Normalization:
normalize_content(content, options)- Strips markdown formatting
- Normalizes whitespace
- Extracts title and summary
- Platform-specific adjustments
-
Helper Functions:
post_to_linkedin(content, options)- Convenience functionpost_to_x(content, options)- Convenience functionbatch_post(contents, options)- Batch posting to same platformget_handler(platform_name, browser_data_dir, task_id)- Get handler instanceregister_handler(platform_name, handler_class)- Register new platformsget_supported_platforms()- Get list of supported platforms
from autonomedia.core.platform import post
# Post to LinkedIn
result = await post(
content="Your content here...",
options={
"platform": "linkedin",
"browser_data_dir": "./runtime/browser_profiles",
"task_id": "task_123",
"article_link": "https://example.com/article",
}
)
# Post to X (Twitter)
result = await post(
content="Your content here...",
options={
"platform": "x",
"browser_data_dir": "./runtime/browser_profiles",
"task_id": "task_123",
}
)from autonomedia.core.platform import post_to_linkedin, post_to_x
# LinkedIn
result = await post_to_linkedin(
content="Your content here...",
options={
"browser_data_dir": "./runtime/browser_profiles",
"task_id": "task_123",
}
)
# X (Twitter)
result = await post_to_x(
content="Your content here...",
options={
"browser_data_dir": "./runtime/browser_profiles",
"task_id": "task_123",
"batch_mode": True,
}
)from autonomedia.core.platform import batch_post
# Post multiple tweets
results = await batch_post(
contents=[
"Tweet 1 content...",
"Tweet 2 content...",
"Tweet 3 content...",
],
options={
"platform": "x",
"browser_data_dir": "./runtime/browser_profiles",
"task_id": "task_123",
}
)Authentication is not yet implemented (TODO). Platforms require:
- OAuth 2.0 token
- API token
- Or session cookies
Tokens should be retrieved from:
- Environment variables
- Secure storage
- Configuration files
-
Randomized Delays:
- Human-like delays between actions
- Jitter added to exponential backoff
-
Randomized Viewports:
- Different viewport sizes simulate different devices
- LinkedIn: 1280x800, 1366x768, 1536x864
- X: Same range
-
Human-like Behavior:
- Wait times between operations
- Session health checks before posting
- Patient UI interaction
- Retry Logic: Exponential backoff with max 3 attempts
- Error Logging: Structured logging with timestamps and task IDs
- Validation: Content validation and authentication checks
- Structured Results: Clear status codes and error messages
- Create handler class inheriting from
PlatformHandler - Implement abstract methods:
post()andvalidate_auth() - Add platform-specific constants and formatting logic
- Register handler in
core/platform/__init__.py:
from autonomedia.platforms.mastodon.task_handler import MastodonHandler
register_handler("mastodon", MastodonHandler)- Use via unified API:
result = await post(
content="Your content here...",
options={
"platform": "mastodon",
"browser_data_dir": "./runtime/browser_profiles",
"task_id": "task_123",
}
)src/autonomedia/core/platform/__init__.py- Unified abstraction layersrc/autonomedia/core/platform/base.py- Abstract base class
src/autonomedia/platforms/linkedin/task_handler.py- LinkedIn handlersrc/autonomedia/platforms/x/task_handler.py- X (Twitter) handlersrc/autonomedia/platforms/mastodon/task_handler.py- Mastodon handler (existing)
src/autonomedia/core/__init__.py- Exports platform functionssrc/autonomedia/platforms/__init__.py- Platform handler exports
src/autonomedia/core/config/settings.py- Added LINKEDIN_URL and X_URL
Run logic tests to verify implementation:
python test_platform_logic.pyTests cover:
- Handler class structure
- LinkedIn handler components
- X handler components
- Unified abstraction layer
- Content normalization logic
- Tag/hashtag extraction and formatting
- Retry logic structure
- Open/Closed Principle: New platforms can be added without modifying existing handlers
- Existing Mastodon Handler: Already inherits from PlatformHandler, remains compatible
- No Hardcoded Tokens: Authentication requires configuration
- Structured Logging: All operations logged with metadata
- OAuth 2.0 implementation for all platforms
- Authentication token management in secure storage
- Rate limiting and quota management
- Content scheduling and queuing
- Platform-specific content optimization
- Analytics and posting metrics