-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
550 lines (433 loc) · 18.1 KB
/
Copy pathapi.py
File metadata and controls
550 lines (433 loc) · 18.1 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
"""
Content Repurposing API - ContentForge
Simulation: sim-content-repurposing-api-001
Takes a URL (blog post, article) and generates 10+ derivative content pieces.
Uses template-based generation (fast, no LLM needed) with optional LLM enhancement.
This makes the API deployable for free and fast enough for production use.
"""
import os
import re
import json
import time
import httpx
from bs4 import BeautifulSoup
from fastapi import FastAPI, HTTPException, Query, Request, Header
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from typing import Optional, List
import hashlib
app = FastAPI(
title="ContentForge API",
description="Turn any URL into 10+ ready-to-publish content pieces: tweets, LinkedIn posts, email sequences, and more.",
version="1.0.0",
contact={"name": "ContentForge", "url": "https://contentforge.dev"},
docs_url="/docs",
redoc_url="/redoc",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Optional LLM endpoint
LLM_URL = os.environ.get("LLM_URL", "")
LLM_KEY = os.environ.get("LLM_KEY", "")
LLM_MODEL = os.environ.get("LLM_MODEL", "gpt-3.5-turbo")
# API key for paid tiers (set via environment variable)
MASTER_API_KEY = os.environ.get("CONTENTFORGE_API_KEY", "")
# Simple in-memory usage tracking (for freemium enforcement)
USAGE_TRACKER = {} # api_key -> {"count": N, "reset_date": timestamp}
FREE_TIER_LIMIT = 10 # requests per month
def check_usage(api_key: str) -> tuple[bool, str]:
"""Check if the API key is within usage limits. Returns (allowed, message)."""
# If no master key is set, everything is free (demo mode)
if not MASTER_API_KEY:
return True, "Demo mode - no limits enforced"
# Paid API key (matches master key) = unlimited
if api_key == MASTER_API_KEY:
return True, "Pro tier - unlimited"
# Free tier (no key or unknown key) - track by IP/key
tracker_key = api_key or "anonymous"
now = time.time()
if tracker_key not in USAGE_TRACKER:
USAGE_TRACKER[tracker_key] = {"count": 0, "reset_date": now + 30 * 86400}
entry = USAGE_TRACKER[tracker_key]
# Reset if month has passed
if now > entry["reset_date"]:
entry["count"] = 0
entry["reset_date"] = now + 30 * 86400
if entry["count"] >= FREE_TIER_LIMIT:
return False, f"Free tier limit ({FREE_TIER_LIMIT} requests/month) exceeded. Upgrade at https://eddyscanlan.github.io/contentforge-api/"
entry["count"] += 1
remaining = FREE_TIER_LIMIT - entry["count"]
return True, f"Free tier: {remaining} requests remaining this month"
class RepurposeRequest(BaseModel):
url: str
tone: Optional[str] = "professional"
audience: Optional[str] = "general"
format: Optional[str] = "all" # or specific: tweets, linkedin, email, etc.
# ============ ARTICLE EXTRACTION ============
def extract_article(url: str) -> dict:
"""Extract article content from URL using httpx + BeautifulSoup."""
try:
headers = {
"User-Agent": "Mozilla/5.0 (compatible; ContentForgeBot/1.0; +https://contentforge.dev)"
}
with httpx.Client(timeout=20, follow_redirects=True, verify=False, headers=headers) as client:
response = client.get(url)
response.raise_for_status()
except Exception as e:
raise ValueError(f"Failed to fetch URL: {str(e)[:200]}")
soup = BeautifulSoup(response.text, 'html.parser')
# Remove unwanted elements
for tag in soup.find_all(['script', 'style', 'nav', 'footer', 'header', 'aside', 'iframe', 'noscript']):
tag.decompose()
# Extract title
title = ''
h1 = soup.find('h1')
if h1:
title = h1.get_text(strip=True)
elif soup.find('title'):
title = soup.find('title').get_text(strip=True)
elif soup.find('meta', attrs={'property': 'og:title'}):
title = soup.find('meta', attrs={'property': 'og:title'}).get('content', '')
# Meta description
meta_desc = ''
meta_tag = soup.find('meta', attrs={'name': 'description'}) or soup.find('meta', attrs={'property': 'og:description'})
if meta_tag and meta_tag.get('content'):
meta_desc = meta_tag['content']
# Extract main text content
article_tag = soup.find('article') or soup.find('main') or soup.find('body')
text = article_tag.get_text(separator=' ', strip=True) if article_tag else soup.get_text(separator=' ', strip=True)
text = re.sub(r'\s+', ' ', text).strip()
# Authors
authors = []
author_tag = soup.find('meta', attrs={'name': 'author'}) or soup.find('meta', attrs={'property': 'article:author'})
if author_tag and author_tag.get('content'):
authors.append(author_tag['content'])
# Extract key sentences for content generation
sentences = [s.strip() for s in text.split('.') if len(s.strip()) > 20]
key_sentences = sentences[:20] # Top 20 sentences
return {
"title": title,
"text": text,
"authors": authors,
"summary": meta_desc if meta_desc else text[:500],
"meta_description": meta_desc,
"key_sentences": key_sentences,
"word_count": len(text.split()),
}
# ============ TEMPLATE-BASED CONTENT GENERATION ============
def split_into_sentences(text: str, min_length: int = 20) -> list:
sentences = re.split(r'(?<=[.!?])\s+', text)
return [s.strip() for s in sentences if len(s.strip()) > min_length]
def extract_key_points(article: dict, n: int = 5) -> list:
"""Extract N key points from the article using simple heuristics."""
sentences = split_into_sentences(article["text"])
# Prioritize sentences with numbers, questions, or strong statements
scored = []
for s in sentences:
score = 0
if re.search(r'\d+', s): score += 2 # Contains numbers
if '?' in s: score += 1 # Questions
if len(s) > 50 and len(s) < 200: score += 1 # Good length
if any(w in s.lower() for w in ['important', 'key', 'must', 'should', 'need', 'best', 'secret', 'proven']):
score += 2
scored.append((s, score))
scored.sort(key=lambda x: x[1], reverse=True)
return [s for s, _ in scored[:n]]
def generate_tweet_thread(article: dict, tone: str) -> dict:
points = extract_key_points(article, 6)
tweets = []
# Hook tweet
tweets.append(f"Just read: \"{article['title']}\"\n\nHere are {len(points)} key takeaways 🧵")
# Content tweets
for i, point in enumerate(points, 1):
tweet = f"{i}/ {point}"
if len(tweet) > 270:
tweet = tweet[:267] + "..."
tweets.append(tweet)
# CTA tweet
tweets.append(f"Found this helpful? The full article is worth reading.\n\nWhat's your biggest takeaway? 👇")
content = "\n\n---\n\n".join(tweets)
return {
"type": "tweet_thread",
"title": f"Twitter Thread ({len(tweets)} tweets)",
"content": content,
"word_count": len(content.split()),
}
def generate_linkedin_post(article: dict, tone: str) -> dict:
points = extract_key_points(article, 4)
title = article["title"]
hook = f"I just read something that changed how I think about this topic.\n\n"
intro = f'"{title}"\n\n'
body = "Here's what stood out:\n\n"
for i, point in enumerate(points, 1):
body += f"• {point}\n\n"
cta = f"What would you add to this list?\n\n#contentstrategy #marketing #business"
content = hook + intro + body + cta
return {
"type": "linkedin_post",
"title": "LinkedIn Post",
"content": content,
"word_count": len(content.split()),
}
def generate_newsletter_intro(article: dict, tone: str) -> dict:
summary = article["summary"][:200]
content = f"""In this issue, we're diving into "{article['title']}".
{summary}
The key insight? The details matter more than you think. Here's why this matters for your business and what you can do about it today."""
return {
"type": "newsletter_intro",
"title": "Newsletter Introduction",
"content": content,
"word_count": len(content.split()),
}
def generate_email_sequence(article: dict, tone: str) -> dict:
points = extract_key_points(article, 3)
email1 = f"""Subject: I found something you need to see
Hi [Name],
I just read "{article['title']}" and immediately thought of you.
{points[0] if points else 'The article covers key strategies that are worth your time.'}
More tomorrow.
[Your Name]"""
email2 = f"""Subject: Following up on yesterday...
Hi [Name],
Yesterday I mentioned "{article['title']}".
Here's the second key point:
{points[1] if len(points) > 1 else 'Another important insight from the article.'}
Tomorrow: the biggest takeaway.
[Your Name]"""
email3 = f"""Subject: The #1 thing I learned
Hi [Name],
Over the past two days I've shared insights from "{article['title']}".
Here's the biggest takeaway:
{points[2] if len(points) > 2 else 'The full article is worth reading for the complete picture.'}
Want to chat about how this applies to your situation?
[Your Name]"""
content = f"=== EMAIL 1 ===\n{email1}\n\n=== EMAIL 2 ===\n{email2}\n\n=== EMAIL 3 ===\n{email3}"
return {
"type": "email_sequence",
"title": "3-Part Email Sequence",
"content": content,
"word_count": len(content.split()),
}
def generate_facebook_post(article: dict, tone: str) -> dict:
points = extract_key_points(article, 3)
content = f'Just finished reading "{article["title"]}" and had to share.\n\n'
for p in points:
content += f"→ {p}\n\n"
content += "Has anyone else read this? What did you think?"
return {
"type": "facebook_post",
"title": "Facebook Post",
"content": content,
"word_count": len(content.split()),
}
def generate_instagram_caption(article: dict, tone: str) -> dict:
points = extract_key_points(article, 3)
content = f"📚 Just read something game-changing.\n\n"
content += f'"{article["title"]}"\n\n'
content += "3 things that stood out:\n"
for i, p in enumerate(points, 1):
# Shorten for IG
short = p[:80] + "..." if len(p) > 80 else p
content += f"\n{i}. {short}"
content += "\n\nSave this for later 📌"
content += "\n\n#contentmarketing #businessgrowth #entrepreneur #marketingtips #contentstrategy"
return {
"type": "instagram_caption",
"title": "Instagram Caption",
"content": content,
"word_count": len(content.split()),
}
def generate_youtube_script(article: dict, tone: str) -> dict:
points = extract_key_points(article, 5)
content = f"""YOUTUBE SHORT SCRIPT (60-90 sec)
HOOK (0-5s):
What if I told you everything you know about this topic might be wrong?
INTRO (5-15s):
Today I'm breaking down "{article["title"]}" and sharing {len(points)} key takeaways you can use immediately.
MAIN CONTENT (15-60s):
"""
for i, point in enumerate(points, 1):
content += f"\nPoint {i}: {point[:100]}\n"
content += f"""
CTA (60-90s):
If you found this valuable, subscribe for more breakdowns like this.
Title idea: "{article['title'][:60]}"
Tags: #contentmarketing #business #growth"""
return {
"type": "youtube_script",
"title": "YouTube Script (60-90s)",
"content": content,
"word_count": len(content.split()),
}
def generate_blog_summary(article: dict, tone: str) -> dict:
summary = article["summary"][:300]
points = extract_key_points(article, 3)
content = f'"{article["title"]}"\n\n'
content += f"{summary}\n\n"
content += "Key takeaways from the article:\n\n"
for p in points:
content += f"• {p}\n"
content += f"\nRead the full article for the complete analysis."
return {
"type": "blog_summary",
"title": "Blog Summary (2 paragraphs)",
"content": content,
"word_count": len(content.split()),
}
def generate_quote_cards(article: dict, tone: str) -> dict:
sentences = split_into_sentences(article["text"])
# Pick punchy, short sentences
quotes = []
for s in sentences:
if 30 < len(s) < 150 and not s.startswith(('http', 'www', 'Click')):
quotes.append(s)
if len(quotes) >= 5:
break
content = "=== QUOTABLE EXCERPTS ===\n\n"
for i, q in enumerate(quotes[:3], 1):
content += f'Quote {i}:\n"{q}"\n\n'
content += f'— Source: "{article["title"]}"'
return {
"type": "quote_cards",
"title": "3 Quotable Excerpts",
"content": content,
"word_count": len(content.split()),
}
def generate_carousel_outline(article: dict, tone: str) -> dict:
points = extract_key_points(article, 6)
content = "=== LINKEDIN CAROUSEL OUTLINE ===\n\n"
content += f"Slide 1 (Cover): {article['title'][:60]}\n"
content += f" Subtitle: {len(points)} key insights you need to know\n\n"
for i, point in enumerate(points, 1):
content += f"Slide {i+1}: Insight #{i}\n"
# Take first sentence of the point
first_sentence = point.split('.')[0] + '.' if '.' in point else point[:80]
content += f" • {first_sentence[:100]}\n\n"
content += f"Slide {len(points)+2}: What's your take?\n"
content += f" • Share your thoughts in the comments\n"
content += f"\nSlide {len(points)+3}: Follow for more content breakdowns"
return {
"type": "carousel_outline",
"title": f"LinkedIn Carousel ({len(points)+3} slides)",
"content": content,
"word_count": len(content.split()),
}
def generate_cold_email(article: dict, tone: str) -> dict:
points = extract_key_points(article, 2)
content = f"""Subject: Quick thought on {article['title'][:50]}
Hi [Name],
I was reading "{article['title']}" and it got me thinking about what you're working on.
{points[0] if points else 'The article raises important points about strategy and execution.'}
I help businesses implement exactly this kind of thinking. Worth a 15-minute chat?
Best,
[Your Name]"""
return {
"type": "cold_email",
"title": "Cold Outreach Email",
"content": content,
"word_count": len(content.split()),
}
# ============ MAIN API ENDPOINTS ============
GENERATORS = {
"tweet_thread": generate_tweet_thread,
"linkedin_post": generate_linkedin_post,
"newsletter_intro": generate_newsletter_intro,
"email_sequence": generate_email_sequence,
"facebook_post": generate_facebook_post,
"instagram_caption": generate_instagram_caption,
"youtube_script": generate_youtube_script,
"blog_summary": generate_blog_summary,
"quote_cards": generate_quote_cards,
"carousel_outline": generate_carousel_outline,
"cold_email": generate_cold_email,
}
@app.get("/")
def root():
return {
"name": "ContentForge API",
"version": "1.0.0",
"description": "Turn any URL into 10+ ready-to-publish content pieces.",
"endpoints": {
"repurpose": "POST /api/v1/repurpose",
"health": "GET /health",
"docs": "GET /docs",
},
"content_types": list(GENERATORS.keys()),
"pricing": {
"free": "10 requests/month",
"starter": "$9/mo - 100 requests/month",
"pro": "$29/mo - 500 requests/month",
}
}
@app.get("/health")
def health():
return {"status": "healthy", "version": "1.0.0", "timestamp": int(time.time())}
@app.post("/api/v1/repurpose")
def repurpose(request: RepurposeRequest, x_api_key: Optional[str] = Header(None)):
"""Turn any URL into 10+ ready-to-publish content pieces."""
start_time = time.time()
# Check usage limits
allowed, usage_msg = check_usage(x_api_key or "")
if not allowed:
raise HTTPException(status_code=429, detail=usage_msg)
# Step 1: Extract article
try:
article = extract_article(request.url)
except ValueError as e:
raise HTTPException(status_code=422, detail=str(e))
except Exception as e:
raise HTTPException(status_code=422, detail=f"Failed to extract article: {str(e)[:200]}")
if not article["text"] or len(article["text"]) < 100:
raise HTTPException(status_code=422, detail="Article text too short. Ensure the URL contains readable article content.")
# Step 2: Generate content pieces
generators = GENERATORS if request.format == "all" else {
k: v for k, v in GENERATORS.items() if k == request.format
}
pieces = []
for gen_type, gen_func in generators.items():
try:
piece = gen_func(article, request.tone)
pieces.append(piece)
except Exception as e:
pieces.append({
"type": gen_type,
"title": gen_type.replace("_", " ").title(),
"content": f"Error generating content: {str(e)[:100]}",
"word_count": 0,
})
elapsed = round(time.time() - start_time, 2)
return {
"success": True,
"source": {
"url": request.url,
"title": article["title"],
"authors": article["authors"],
"word_count": article["word_count"],
},
"content_pieces": pieces,
"total_pieces": len(pieces),
"processing_time_seconds": elapsed,
"metadata": {
"tone": request.tone,
"audience": request.audience,
"engine": "template-v1",
}
}
@app.get("/api/v1/repurpose")
def repurpose_get(
url: str = Query(..., description="Article URL to repurpose"),
tone: str = Query("professional", description="Tone: professional, casual, witty, authoritative"),
audience: str = Query("general", description="Target audience"),
format: str = Query("all", description="Content type filter: all, tweet_thread, linkedin_post, etc."),
):
"""GET version of the repurpose endpoint for easy testing and simple integrations."""
return repurpose(RepurposeRequest(url=url, tone=tone, audience=audience, format=format))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)