-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
250 lines (213 loc) · 7.39 KB
/
app.py
File metadata and controls
250 lines (213 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
Re-Defined Blog Automation System - Main Application
"""
import os
import sys
from pathlib import Path
from fastapi import FastAPI, HTTPException, BackgroundTasks
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime
from loguru import logger
# Add project root to path
sys.path.append(str(Path(__file__).parent))
from src.database.init_db import init_database
from src.scheduler.scheduler import BlogScheduler
from src.content_generator.generator import BlogGenerator
from src.database.models import BlogPost
from src.database.init_db import get_session
from config.settings import settings
# Initialize FastAPI app
app = FastAPI(
title="Re-Defined Blog Automation",
description="AI-powered blog automation system for Re-Defined",
version="1.0.0"
)
# Static files
app.mount("/static", StaticFiles(directory="web/static"), name="static")
# Initialize scheduler
scheduler = BlogScheduler()
# Request/Response models
class BlogGenerationRequest(BaseModel):
topic: str
custom_instructions: Optional[str] = None
schedule_date: Optional[datetime] = None
class BlogResponse(BaseModel):
id: str
title: str
slug: str
status: str
created_at: datetime
published_date: Optional[datetime] = None
url: Optional[str] = None
class TaskResponse(BaseModel):
id: str
task_type: str
status: str
scheduled_for: datetime
result: Optional[dict] = None
error_message: Optional[str] = None
@app.on_event("startup")
async def startup_event():
"""Initialize the application on startup"""
logger.info("Starting Re-Defined Blog Automation System...")
# Initialize database
init_database()
# Start scheduler
scheduler.start()
logger.info("Application started successfully!")
@app.on_event("shutdown")
async def shutdown_event():
"""Cleanup on shutdown"""
logger.info("Shutting down application...")
scheduler.stop()
@app.get("/")
async def root():
"""Root endpoint"""
return {
"message": "Re-Defined Blog Automation System",
"status": "running",
"version": "1.0.0"
}
@app.post("/generate", response_model=BlogResponse)
async def generate_blog(request: BlogGenerationRequest, background_tasks: BackgroundTasks):
"""Generate a new blog post"""
try:
if request.schedule_date and request.schedule_date > datetime.utcnow():
# Schedule for later
task_id = scheduler.schedule_custom_blog(
topic=request.topic,
scheduled_date=request.schedule_date,
custom_instructions=request.custom_instructions
)
return BlogResponse(
id=task_id,
title=f"Scheduled: {request.topic}",
slug="pending",
status="scheduled",
created_at=datetime.utcnow(),
published_date=request.schedule_date
)
else:
# Generate immediately
generator = BlogGenerator()
blog_post = generator.create_blog(
topic=request.topic,
custom_instructions=request.custom_instructions
)
return BlogResponse(
id=blog_post.id,
title=blog_post.title,
slug=blog_post.slug,
status=blog_post.status,
created_at=blog_post.created_at,
published_date=blog_post.published_date,
url=f"https://re-defined.ca/blog/{blog_post.slug}" if blog_post.status == "published" else None
)
except Exception as e:
logger.error(f"Blog generation failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/blogs", response_model=List[BlogResponse])
async def list_blogs(status: Optional[str] = None, limit: int = 20):
"""List blog posts"""
session = get_session()
query = session.query(BlogPost)
if status:
query = query.filter_by(status=status)
blogs = query.order_by(BlogPost.created_at.desc()).limit(limit).all()
return [
BlogResponse(
id=blog.id,
title=blog.title,
slug=blog.slug,
status=blog.status,
created_at=blog.created_at,
published_date=blog.published_date,
url=f"https://re-defined.ca/blog/{blog.slug}" if blog.status == "published" else None
)
for blog in blogs
]
@app.get("/blogs/{blog_id}", response_model=BlogResponse)
async def get_blog(blog_id: str):
"""Get a specific blog post"""
session = get_session()
blog = session.query(BlogPost).filter_by(id=blog_id).first()
if not blog:
raise HTTPException(status_code=404, detail="Blog post not found")
return BlogResponse(
id=blog.id,
title=blog.title,
slug=blog.slug,
status=blog.status,
created_at=blog.created_at,
published_date=blog.published_date,
url=f"https://re-defined.ca/blog/{blog.slug}" if blog.status == "published" else None
)
@app.post("/blogs/{blog_id}/publish")
async def publish_blog(blog_id: str):
"""Publish a blog post"""
try:
from src.publisher.publisher import BlogPublisher
publisher = BlogPublisher()
result = publisher.publish_blog(blog_id)
if result['success']:
return {"message": "Blog published successfully", "url": result.get('url')}
else:
raise HTTPException(status_code=400, detail=result.get('message'))
except Exception as e:
logger.error(f"Publishing failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/scrape-trends")
async def scrape_trends(background_tasks: BackgroundTasks):
"""Trigger trend scraping"""
background_tasks.add_task(scheduler.scrape_trends_task)
return {"message": "Trend scraping started"}
@app.get("/tasks", response_model=List[TaskResponse])
async def list_tasks(status: Optional[str] = None):
"""List scheduled tasks"""
tasks = scheduler.get_scheduled_tasks(status)
return [
TaskResponse(
id=task.id,
task_type=task.task_type,
status=task.status,
scheduled_for=task.scheduled_for,
result=task.result,
error_message=task.error_message
)
for task in tasks
]
@app.post("/tasks/{task_id}/retry")
async def retry_task(task_id: str):
"""Retry a failed task"""
success = scheduler.retry_failed_task(task_id)
if success:
return {"message": "Task scheduled for retry"}
else:
raise HTTPException(status_code=400, detail="Cannot retry this task")
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"database": "connected",
"scheduler": "running" if scheduler.scheduler.running else "stopped"
}
if __name__ == "__main__":
import uvicorn
# Configure logging
logger.add(
settings.logs_path / "app.log",
rotation="500 MB",
retention="10 days",
level=settings.log_level
)
# Run the application
uvicorn.run(
"app:app",
host=settings.app_host,
port=settings.app_port,
reload=True,
log_level=settings.log_level.lower()
)