Back to .md Directory

Topic Aggregation for Social Media Content

Takes multiple blog URLs on a topic and generates platform-optimized social media posts exported to CSV or Excel.

May 2, 2026
0 downloads
0 views
ai agent rag openai gemini
View source

What this file does

Takes multiple blog URLs on a topic and generates platform-optimized social media posts exported to CSV or Excel.

When to use it

  • Building a content calendar from industry news
  • Aggregating competitor product launch coverage
  • Creating multi-platform posts from research roundups
  • Automating social media output for marketing teams

Assumes this stack

PythonFastAPIGemini FlashopenpyxlJavaScript

Topic Aggregation for Social Media Content

Generate social media-ready content from multiple blog sources on a specific topic, exported to spreadsheet format!

Features

✨ Batch Summarization - Process multiple URLs concurrently for a specific topic πŸ“± Multi-Platform Posts - Generate optimized posts for Twitter, LinkedIn, Facebook, and Instagram πŸ“Š Spreadsheet Export - Export to CSV or Excel with all content organized 🎯 Character-Optimized - Each platform's post respects character limits #️⃣ Smart Hashtags - Auto-generated hashtags from topic and content ⚑ Concurrent Processing - Fast batch processing with configurable workers

Use Case

Perfect for:

  • Social media managers creating content calendars
  • Marketing teams tracking industry trends
  • Content creators aggregating research
  • Brand managers monitoring competitor content
  • Digital agencies managing multiple clients

Quick Start

1. API Usage

Aggregate and Get JSON

curl -X POST "http://localhost:8000/aggregate-topic" \
  -H "Content-Type: application/json" \
  -d '{
    "topic": "AI-Trends-2025",
    "urls": [
      "https://blog.google/technology/ai/",
      "https://openai.com/blog/",
      "https://www.anthropic.com/news"
    ],
    "platforms": ["twitter", "linkedin", "facebook", "instagram"],
    "max_workers": 3
  }'

Export to Spreadsheet

curl -X POST "http://localhost:8000/aggregate-topic/export" \
  -H "Content-Type: application/json" \
  -d '{
    "topic": "AI-Trends-2025",
    "urls": [
      "https://blog.google/technology/ai/",
      "https://openai.com/blog/"
    ],
    "platforms": ["twitter", "linkedin", "facebook"],
    "export_format": "excel"
  }' \
  --output AI-Trends-2025_social_media.xlsx

2. Python SDK

from web_summarizer import WebSummarizerAgent
from web_summarizer.topic_aggregator import TopicAggregator
from web_summarizer.spreadsheet_generator import SpreadsheetGenerator

# Initialize
agent = WebSummarizerAgent(gemini_api_key="your_key")
aggregator = TopicAggregator(agent)
spreadsheet_gen = SpreadsheetGenerator()

# Define topic and sources
topic = "AI-Trends-2025"
urls = [
    "https://blog.google/technology/ai/",
    "https://openai.com/blog/",
    "https://www.anthropic.com/news"
]

# Aggregate summaries
result = aggregator.aggregate_topic(
    topic=topic,
    urls=urls,
    platforms=["twitter", "linkedin", "facebook", "instagram"],
    max_workers=3
)

# Export to Excel
spreadsheet_gen.generate_excel(
    result,
    output_path=f"{topic}_social_media.xlsx"
)

3. JavaScript/Browser

const response = await fetch('http://localhost:8000/aggregate-topic/export', {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({
    topic: 'AI-Trends-2025',
    urls: [
      'https://blog.google/technology/ai/',
      'https://openai.com/blog/'
    ],
    platforms: ['twitter', 'linkedin', 'facebook'],
    export_format: 'csv'
  })
});

// Download the file
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'AI-Trends-2025_social_media.csv';
a.click();

API Endpoints

POST /aggregate-topic

Aggregate summaries and generate social media posts.

Request:

{
  "topic": "string",
  "urls": ["string"],
  "platforms": ["twitter", "linkedin", "facebook", "instagram"],
  "max_workers": 5
}

Response:

{
  "success": true,
  "data": {
    "topic": "AI-Trends-2025",
    "total_sources": 3,
    "successful_summaries": 3,
    "failed_summaries": 0,
    "platforms": ["twitter", "linkedin", "facebook", "instagram"],
    "generated_at": "2025-11-08T10:00:00Z",
    "data": [
      {
        "source_url": "https://example.com",
        "title": "Article Title",
        "category": "Technology",
        "summary": "Summary text...",
        "key_points": "Point 1; Point 2; Point 3",
        "twitter_post": "Tweet content...",
        "twitter_hashtags": "#AI #Tech",
        "twitter_chars": 280,
        "linkedin_post": "LinkedIn post...",
        "linkedin_hashtags": "#AI #Technology",
        "linkedin_chars": 500,
        ...
      }
    ]
  }
}

POST /aggregate-topic/export

Aggregate and export to downloadable spreadsheet.

Request:

{
  "topic": "string",
  "urls": ["string"],
  "platforms": ["twitter", "linkedin", "facebook"],
  "export_format": "csv" // or "excel"
}

Response: Downloadable CSV or Excel file

Spreadsheet Output

The generated spreadsheet includes:

Columns

  • source_url - Original article URL
  • title - Article title
  • category - Content category
  • summary - AI-generated summary
  • key_points - Semicolon-separated key insights
  • {platform}_post - Social media post content
  • {platform}_hashtags - Platform-specific hashtags
  • {platform}_chars - Character count
  • word_count - Summary word count
  • tokens_used - AI tokens consumed
  • processing_time_ms - Processing duration
  • timestamp - When processed

Excel Features

  • Color-coded headers
  • Auto-adjusted column widths
  • Text wrapping for posts
  • Separate metadata sheet
  • Frozen header row
  • Professional formatting

Platform-Specific Posts

Twitter/X (280 chars)

{First key point or concise summary}

https://example.com/article

#AI #Tech #Innovation

LinkedIn (3000 chars)

πŸ“Š AI Trends 2025: Article Title

{Full summary paragraph}

Key Insights:
1. First key point
2. Second key point
3. Third key point

Read more: https://example.com

#AI #Technology #Innovation #Business #DigitalTransformation

Facebook (~500 chars)

πŸ’‘ Article Title

{Summary paragraph}

✨ Highlights:
β€’ First key point
β€’ Second key point
β€’ Third key point

πŸ”— https://example.com

#AI #Tech #Innovation

Instagram (2200 chars)

{Summary paragraph}

✨ Key takeaways:
πŸ“Œ First key point
πŸ’‘ Second key point
🎯 Third key point

#AI #Tech #Innovation #Technology #DigitalTransformation #MachineLearning
... (up to 30 hashtags)

Configuration

Platforms

Available platforms:

  • twitter - Twitter/X (280 char limit)
  • linkedin - LinkedIn (3000 char limit, professional tone)
  • facebook - Facebook (optimized for ~500 chars)
  • instagram - Instagram (2200 char limit, up to 30 hashtags)

Concurrent Workers

  • max_workers: 1-10 (default: 5)
  • Higher values = faster processing
  • Limited by API rate limits

Export Formats

  • csv - Comma-separated values (universal compatibility)
  • excel - Microsoft Excel with formatting (.xlsx)

Examples

Example 1: Tech News Roundup

topic = "Weekly-Tech-News"
urls = [
    "https://techcrunch.com/latest/",
    "https://www.theverge.com/tech",
    "https://arstechnica.com/"
]

result = aggregator.aggregate_topic(topic, urls, platforms=["twitter", "linkedin"])
spreadsheet_gen.generate_excel(result, "tech_news_week.xlsx")

Example 2: Competitor Analysis

topic = "Competitor-Product-Launches"
urls = [
    "https://competitor1.com/blog/new-product",
    "https://competitor2.com/news/launch",
    "https://competitor3.com/updates"
]

result = aggregator.aggregate_topic(topic, urls, platforms=["linkedin", "facebook"])
spreadsheet_gen.generate_csv(result, "competitor_analysis.csv")

Example 3: Industry Research

topic = "Climate-Tech-Innovations"
urls = [
    "https://example.com/climate-article-1",
    "https://example.com/climate-article-2",
    "https://example.com/climate-article-3"
]

result = aggregator.aggregate_topic(
    topic,
    urls,
    platforms=["twitter", "linkedin", "facebook", "instagram"],
    max_workers=2
)

# Export both formats
spreadsheet_gen.generate_csv(result, "climate_tech.csv")
spreadsheet_gen.generate_excel(result, "climate_tech.xlsx")

Workflow Example

  1. Research Phase: Find relevant blog posts/articles on your topic
  2. Aggregate: Run topic aggregation with all URLs
  3. Review: Open Excel file, review summaries and posts
  4. Edit: Fine-tune posts if needed directly in spreadsheet
  5. Schedule: Use spreadsheet to schedule posts on social media platforms
  6. Track: Keep spreadsheet for content calendar tracking

Performance

  • Speed: 3-5 seconds per URL (with Gemini Flash)
  • Concurrent: Processes multiple URLs in parallel
  • Cost: ~$0.01 per 1000 URLs (with Gemini Flash)
  • Success Rate: 95%+ on standard blog posts

Error Handling

Failed URLs are tracked separately:

{
  "total_sources": 5,
  "successful_summaries": 4,
  "failed_summaries": 1
}

Spreadsheet only includes successful summaries. Check response for failure details.

Dependencies

pip install openpyxl>=3.1.0  # For Excel export

(CSV export has no additional dependencies)

Tips

  1. Topic Naming: Use descriptive, kebab-case names (e.g., AI-Trends-2025)
  2. URL Selection: Choose authoritative, well-structured sources
  3. Platform Mix: Start with 2-3 platforms, expand as needed
  4. Batch Size: Keep to 5-10 URLs per batch for best performance
  5. Review Posts: Always review generated posts before publishing
  6. Custom Hashtags: Edit hashtags in spreadsheet for brand consistency
  7. Content Calendar: Use timestamp column for scheduling
  8. A/B Testing: Generate multiple versions with different platforms

Advanced Usage

Custom Post Templates

Extend TopicAggregator to customize post formats:

class CustomAggregator(TopicAggregator):
    def _generate_twitter_post(self, summary, key_points, url, hashtags):
        # Your custom template
        return f"πŸ”₯ {key_points[0]}\n\n{url}\n\n{' '.join(hashtags[:2])}"

Filter by Category

Post-process results to filter by category:

result = aggregator.aggregate_topic(topic, urls, platforms=["twitter"])

# Filter only "Technology" category
tech_only = {
    **result,
    "data": [r for r in result["data"] if r["category"] == "Technology"]
}

spreadsheet_gen.generate_excel(tech_only, "tech_only.xlsx")

Merge Multiple Topics

Combine results from multiple topics:

topic1_result = aggregator.aggregate_topic("AI", urls1, platforms=["twitter"])
topic2_result = aggregator.aggregate_topic("ML", urls2, platforms=["twitter"])

combined = {
    "topic": "AI-and-ML",
    "data": topic1_result["data"] + topic2_result["data"],
    ...
}

spreadsheet_gen.generate_excel(combined, "combined.xlsx")

Troubleshooting

Excel file won't open

  • Ensure openpyxl is installed: pip install openpyxl
  • Check file permissions

Posts too long

  • Platform limits are automatically enforced
  • Content is truncated with "..."

Missing hashtags

  • Hashtags are auto-generated from topic keywords
  • Customize in spreadsheet after export

Slow processing

  • Reduce max_workers if hitting rate limits
  • Process in smaller batches

Swagger Docs

Interactive API documentation: http://localhost:8000/docs

Test the endpoints directly in your browser!


Ready to create amazing social media content from any topic! πŸš€

What's inside

6 feature bullets, 3 code examples, 2 API endpoints, 4 platform templates, 3 advanced usage snippets, troubleshooting section

Change this for your project

  • Replace gemini_api_key="your_key" with your actual Gemini API key
  • Replace http://localhost:8000 with your deployed API base URL
  • Replace example URLs like https://blog.google/technology/ai/ with your own source URLs

Where it goes

Keep it in your repository where the agent or team that needs it will read it.

Worth borrowing

  • Separating aggregation logic from spreadsheet generation into distinct classes
  • Using a topic-based naming convention (kebab-case) for organizing exports
  • Providing both JSON and downloadable file endpoints for the same data

Related Documents