from flask import Blueprint, Response from pathlib import Path from jinja2 import Template from datetime import datetime from database import articles_collection newsletter_bp = Blueprint('newsletter', __name__) @newsletter_bp.route('/api/newsletter/preview', methods=['GET']) def preview_newsletter(): """Preview the newsletter HTML (for testing)""" try: # Get latest articles with AI summaries cursor = articles_collection.find( {'summary': {'$exists': True, '$ne': None}} ).sort('created_at', -1).limit(10) articles = [] for doc in cursor: articles.append({ 'title': doc.get('title', ''), 'author': doc.get('author'), 'link': doc.get('link', ''), 'summary': doc.get('summary', ''), 'source': doc.get('source', ''), 'published_at': doc.get('published_at', '') }) if not articles: return Response( "

No articles with summaries found

Run the crawler with Ollama enabled first.

", mimetype='text/html' ) # Load template template_path = Path(__file__).parent.parent / 'templates' / 'newsletter_template.html' with open(template_path, 'r', encoding='utf-8') as f: template_content = f.read() template = Template(template_content) # Prepare data now = datetime.now() template_data = { 'date': now.strftime('%A, %B %d, %Y'), 'year': now.year, 'article_count': len(articles), 'articles': articles, 'unsubscribe_link': 'http://localhost:3000/unsubscribe', 'website_link': 'http://localhost:3000' } # Render and return HTML html_content = template.render(**template_data) return Response(html_content, mimetype='text/html') except Exception as e: return Response( f"

Error

{str(e)}

", mimetype='text/html' ), 500