Skip to content
Shell
2.8 KB
Raw Blame
#!/usr/bin/env python3
"""
Store Reddit Research to Notion
Creates a Notion page with extracted insights from Reddit threads.

Usage:
    python store_to_notion.py <json_file> --page-id <notion_page_id>

The json file should be output from reddit_extractor.py --json
"""

import json
import subprocess
import sys
from datetime import datetime


def create_notion_page(data: dict, parent_page_id: str) -> str:
    """Create a Notion page with the research data using claude CLI."""

    post = data['post']
    metrics = data['metrics']
    top_comments = data.get('top_comments', [])[:10]

    # Build Notion-flavored markdown content
    content_lines = [
        f"**Source:** [{post['subreddit']}]({post['url']})",
        f"**Extracted:** {data.get('extracted_at', datetime.now().isoformat())}",
        "",
        "## Metrics",
        f"- Post Score: {post['score']}",
        f"- Upvote Ratio: {post['upvote_ratio']:.0%}" if post.get('upvote_ratio') else "",
        f"- Total Comments: {metrics['total_comments']}",
        f"- Unique Authors: {metrics['unique_authors']}",
        f"- Avg Comment Score: {metrics['avg_comment_score']}",
        "",
        "## Original Post",
        "",
        post.get('selftext', '*No content*')[:2000],
        "",
        "## Top Comments",
        "",
    ]

    for c in top_comments:
        content_lines.append(f"### [{c['score']} pts] u/{c['author']}")
        content_lines.append(c['body'][:500])
        content_lines.append("")

    content = '\n'.join(content_lines)

    # Build the MCP command
    mcp_command = {
        "parent": {"page_id": parent_page_id},
        "pages": [{
            "properties": {"title": f"πŸ“Š {post['title'][:80]}"},
            "content": content
        }]
    }

    print(f"Creating Notion page: {post['title'][:50]}...")
    print(json.dumps(mcp_command, indent=2))

    return mcp_command


def main():
    if len(sys.argv) < 2:
        print("Usage: python store_to_notion.py <json_file> [--page-id <notion_page_id>]")
        sys.exit(1)

    json_file = sys.argv[1]
    page_id = None

    if '--page-id' in sys.argv:
        idx = sys.argv.index('--page-id')
        if idx + 1 < len(sys.argv):
            page_id = sys.argv[idx + 1]

    with open(json_file, 'r') as f:
        data = json.load(f)

    if page_id:
        command = create_notion_page(data, page_id)
        # Output the command - user can then use it with MCP
        print("\nβœ… Ready to create Notion page. Use the above JSON with notion-create-pages MCP tool.")
    else:
        print("Note: No --page-id provided. Outputting preview only.")
        print(f"\nThread: {data['post']['title']}")
        print(f"Comments: {data['metrics']['total_comments']}")
        print(f"Top comment authors: {', '.join(c['author'] for c in data.get('top_comments', [])[:5])}")


if __name__ == '__main__':
    main()