Spaces:
Running
Running
| from flask import Flask, render_template, request, jsonify, send_file, redirect, url_for | |
| import validators | |
| import os | |
| import tempfile | |
| import uuid | |
| from modules.technical_seo import TechnicalSEOModule | |
| from modules.content_audit import ContentAuditModule | |
| from report_generator import ReportGenerator | |
| from simple_pdf_generator import SimplePDFGenerator | |
| app = Flask(__name__, static_folder='static') | |
| app.secret_key = 'seo_report_generator_2024' | |
| # Initialize modules | |
| technical_module = TechnicalSEOModule() | |
| content_module = ContentAuditModule() | |
| report_gen = ReportGenerator() | |
| pdf_gen = SimplePDFGenerator() | |
| # Store for generated reports (in production, use database) | |
| reports_store = {} | |
| def index(): | |
| return render_template('index.html') | |
| def generate_report(): | |
| try: | |
| data = request.json | |
| url = data.get('url', '').strip() | |
| competitors = data.get('competitors', []) | |
| if not url: | |
| return jsonify({'error': 'Website URL is required'}), 400 | |
| if not validators.url(url): | |
| return jsonify({'error': 'Please enter a valid URL'}), 400 | |
| # Generate unique report ID | |
| report_id = str(uuid.uuid4()) | |
| # Validate competitor URLs | |
| competitor_list = [] | |
| for comp in competitors: | |
| comp = comp.strip() | |
| if comp and validators.url(comp): | |
| competitor_list.append(comp) | |
| # Technical SEO Analysis | |
| technical_data = technical_module.analyze(url) | |
| # Content Audit | |
| content_data = content_module.analyze(url) | |
| # Competitor Analysis | |
| competitor_data = [] | |
| for comp_url in competitor_list: | |
| comp_technical = technical_module.analyze(comp_url) | |
| comp_content = content_module.analyze(comp_url, quick_scan=True) | |
| competitor_data.append({ | |
| 'url': comp_url, | |
| 'technical': comp_technical, | |
| 'content': comp_content | |
| }) | |
| # Generate HTML report | |
| report_html = report_gen.generate_html_report( | |
| url=url, | |
| technical_data=technical_data, | |
| content_data=content_data, | |
| competitor_data=competitor_data, | |
| include_charts=True | |
| ) | |
| # Store report | |
| reports_store[report_id] = { | |
| 'url': url, | |
| 'html': report_html, | |
| 'technical_data': technical_data, | |
| 'content_data': content_data, | |
| 'competitor_data': competitor_data | |
| } | |
| return jsonify({ | |
| 'success': True, | |
| 'report_id': report_id, | |
| 'redirect_url': f'/report/{report_id}' | |
| }) | |
| except Exception as e: | |
| return jsonify({'error': f'Error generating report: {str(e)}'}), 500 | |
| def view_report(report_id): | |
| if report_id not in reports_store: | |
| return redirect(url_for('index')) | |
| report_data = reports_store[report_id] | |
| return render_template('report.html', | |
| report_html=report_data['html'], | |
| report_id=report_id, | |
| url=report_data['url']) | |
| def download_html(report_id): | |
| if report_id not in reports_store: | |
| return jsonify({'error': 'Report not found'}), 404 | |
| report_data = reports_store[report_id] | |
| # Create temporary file | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as f: | |
| f.write(report_data['html']) | |
| temp_path = f.name | |
| filename = f"seo_report_{report_data['url'].replace('https://', '').replace('http://', '').replace('/', '_')}.html" | |
| return send_file(temp_path, as_attachment=True, download_name=filename, mimetype='text/html') | |
| def download_pdf(report_id): | |
| if report_id not in reports_store: | |
| return jsonify({'error': 'Report not found'}), 404 | |
| try: | |
| report_data = reports_store[report_id] | |
| # Generate PDF | |
| pdf_data = pdf_gen.generate_pdf(report_data['html']) | |
| # Create temporary file | |
| with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as f: | |
| f.write(pdf_data) | |
| temp_path = f.name | |
| filename = f"seo_report_{report_data['url'].replace('https://', '').replace('http://', '').replace('/', '_')}.pdf" | |
| return send_file(temp_path, as_attachment=True, download_name=filename, mimetype='application/pdf') | |
| except Exception as e: | |
| return jsonify({'error': f'PDF generation failed: {str(e)}'}), 500 | |
| if __name__ == '__main__': | |
| app.run(debug=False, host='0.0.0.0', port=7860) |