import requests
from typing import Dict, List
import re
import json
from web_scraper import get_cover_image
class BlogGenerator:
def __init__(self, openai_key: str, openrouter_key: str, serpapi_key: str = None):
self.openai_key = openai_key
self.openrouter_key = openrouter_key
# serpapi_key is now optional since we're using our own image search
def get_cover_image(self, title: str) -> str:
"""Get a cover image URL for the blog post"""
try:
# Use our custom image search function
image_url = get_cover_image(title)
if not image_url:
print("No image found, trying with modified query...")
# Try again with a more generic query if specific title fails
image_url = get_cover_image(title + " high quality cover image")
return image_url
except Exception as e:
print(f"Error in get_cover_image: {e}")
return None
def create_detailed_plan(self, cluster_data: Dict, preliminary_plan: str, research: str) -> str:
try:
response = requests.post(
'https://openrouter.ai/api/v1/chat/completions',
headers={
'Authorization': f'Bearer {self.openrouter_key}',
'HTTP-Referer': 'http://localhost:5001',
'X-Title': 'Blog Generator'
},
json={
'model': 'google/gemini-2.0-flash-thinking-exp:free',
'messages': [{
'role': 'user',
'content': f"""You are part of a team that creates world class blog posts.
For each new blog post project, you are provided with a list of keywords, a primary keyword, search intent, research findings, and a preliminary blog post plan. Here's a definition of each of the inputs:
- Keywords: These are the keywords which the blog post is meant to rank for on SEO. They should be scattered throughout the blog post intelligently to help with SEO.
- Search intent: The search intent recognises the intent of the user when searching up the keyword. Our goal is to optimise the blog post to be highly relevant and valuable to the user, as such the search intent should be satisfied within the blog post.
- Research findings: This is research found from very reputable resources in relation to the blog post. You must intelligently use this research to make your blog post more reputable.
- Preliminary plan: A very basic plan set out by your colleague to kick off the blog post.
- Primary keyword: Out of the keywords, there is one keyword known as the primary keyword. The primary keyword is the keyword which has the highest SEO importance and as such must go in the title and first few sentences of the blog post. It is important that the blog post is highly relevant to the primary keyword, so that it could be placed naturally into the title and introduction sections.
Given the above info, you must create a detailed plan for the blog post.
Your output must:
- Include a plan for the blog post.
- Be in dot point format.
- In each part of the blog post, you must mention which keywords should be placed.
- All keywords must be placed inside the blog post. For each section, mention which keywords to include. The keyword placement must feel natural and must make sense.
- You must include all research points in the blog post. When including the research points, make sure to also include their source URL so that the copywriter can use them as hyperlinks.
- Your plan must satisfy the search intent and revolve directly around the given keywords.
- Your plan must be very detailed.
- Keep in mind the copywriter that will use your plan to write the blog post is not an expert in the topic of the blog post. So you should give them all the detail required so they can just turn it into nicely formatted paragraphs. For example, instead of saying "define X", you must have "define X as ...".
- The plan must have a flow that makes sense.
- Ensure the blog post will be highly detailed and satisfies the most important concepts regarding the topic.
A new project has just come across your desk with the following details:
Keywords: {cluster_data['Keywords']}
Primary keyword: {cluster_data['Primary Keyword']}
Search intent: {cluster_data['Intent']}
Preliminary plan: {preliminary_plan}
Research findings: {research}
Create the detailed plan."""
}]
},
timeout=60
)
if response.status_code != 200:
raise Exception(f"OpenRouter API error: {response.text}")
response_data = response.json()
if 'choices' not in response_data:
raise Exception(f"Unexpected API response format: {response_data}")
return response_data['choices'][0]['message']['content']
except Exception as e:
print(f"Error in create_detailed_plan: {e}")
raise
def write_blog_post(self, detailed_plan: str, cluster_data: Dict) -> str:
try:
response = requests.post(
'https://openrouter.ai/api/v1/chat/completions',
headers={
'Authorization': f'Bearer {self.openrouter_key}',
'HTTP-Referer': 'http://localhost:5001',
'X-Title': 'Blog Generator'
},
json={
'model': 'google/gemini-2.0-flash-thinking-exp:free',
'messages': [{
'role': 'user',
'content': f"""You are part of a team that creates world-class blog posts.
You are the teams best copywriter and are responsible for writing out the actual blog post.
For each new blog post project you are provided with a detailed plan and research findings.
Your job is to create the blog post by closely following the detailed plan.
The blog post you create must:
- Follow the plan bit by bit
- Use short paragraphs
- Use bullet points and subheadings with keywords where appropriate
- Not have any fluff. The content must be value dense and direct
- Be very detailed
- Include the keywords mentioned in each section within that section
- Place the primary keyword in the blog title, H1 header and early in the introduction
- Place one keyword for each section in the heading of that section
- When possible pepper synonyms of the keywords throughout each section
- When possible use Latent Semantic Indexing (LSI) keywords and related terms
- Be at minimum 2000 to 2500 words long
- Be suitable for a year 5 reading level
Make sure to create the entire blog post draft in your first output. Don't stop or cut it short.
Here are the details for your next blog post:
Keywords: {cluster_data['Keywords']}
Primary keyword: {cluster_data['Primary Keyword']}
Search intent: {cluster_data['Intent']}
Detailed plan: {detailed_plan}
Write the blog post."""
}]
},
timeout=120
)
if response.status_code != 200:
raise Exception(f"OpenRouter API error: {response.text}")
response_data = response.json()
if 'choices' not in response_data:
raise Exception(f"Unexpected API response format: {response_data}")
return response_data['choices'][0]['message']['content']
except Exception as e:
print(f"Error in write_blog_post: {e}")
raise
def add_internal_links(self, blog_content: str, previous_posts: List[Dict]) -> str:
try:
response = requests.post(
# 'https://api.openai.com/v1/chat/completions',
# headers={
# 'Authorization': f'Bearer {self.openai_key}',
# 'Content-Type': 'application/json'
# },
# json={
# 'model': 'gpt-4',
'https://openrouter.ai/api/v1/chat/completions',
headers={
'Authorization': f'Bearer {self.openrouter_key}',
'HTTP-Referer': 'http://localhost:5001',
'X-Title': 'Blog Generator'
},
json={
'model': 'google/gemini-2.0-flash-thinking-exp:free',
'messages': [{
'role': 'user',
'content': f"""You are part of a team that creates world class blog posts.
You are in charge of internal linking between blog posts.
For each new blog post that comes across your desk, your job is to look through previously posted blogs and make at least 5 internal links.
To choose the best internal linking opportunities you must:
- Read the previous blog post summaries and look through their keywords. If there is a match where the previous blog post is highly relevant, then this is an internal linking opportunity.
- Do not link if it is not highly relevant. Only make a link if it makes sense and adds value for the reader.
Once you've found the best linking opportunities, you must update the blog post with the internal links. To do this you must:
- Add the link of the previous blog post at the relevant section of the new blog post. Drop the URL at the place which makes most sense. Later we will hyperlink the URL to the word in the blog post which it is placed next to. So your placing is very important.
Make sure to:
- Not delete any existing URLs or change anything about the blog post
- Only add new internal linking URLs
- Place URLs next to relevant anchor text
- Add at least 5 internal links if possible
- Only link when truly relevant and valuable
- Preserve all original content and formatting
Current blog post:
{blog_content}
Previous blog posts:
{json.dumps(previous_posts, indent=2)}
Your output must be the complete blog post with new internal links added. Don't summarize or modify the content - just add the URLs in appropriate places."""
}]
},
timeout=120
)
if response.status_code != 200:
raise Exception(f"OpenAI API error: {response.text}")
response_data = response.json()
if 'choices' not in response_data:
raise Exception(f"Unexpected API response format: {response_data}")
return response_data['choices'][0]['message']['content']
except Exception as e:
print(f"Error in add_internal_links: {e}")
# If there's an error, return the original content
return blog_content
def convert_to_html(self, blog_content: str, image_url: str = None) -> str:
try:
# First replace newlines with
tags
formatted_content = blog_content.replace('\n', '
')
# Add image URL to the prompt
image_instruction = ""
if image_url:
image_instruction = f"Add this image at the top of the post after the title: {image_url}"
response = requests.post(
# 'https://api.openai.com/v1/chat/completions',
# headers={
# 'Authorization': f'Bearer {self.openai_key}',
# 'Content-Type': 'application/json'
# },
# json={
# 'model': 'anthropic/claude-3.5-sonnet:beta',
'https://openrouter.ai/api/v1/chat/completions',
headers={
'Authorization': f'Bearer {self.openrouter_key}',
'HTTP-Referer': 'http://localhost:5001',
'X-Title': 'Blog Generator'
},
json={
'model': 'google/gemini-2.0-flash-thinking-exp:free',
'messages': [{
'role': 'user',
'content': f"""DO NOT OUTPUT ANYTHING OTHER THAN THE HTML CODE. Follow this layout template to generate WordPress code for a blog post:
The blog post should have:
- Title
{f"- Featured image: " if image_url else ""}
- Estimated reading time
- Key takeaways
- Table of contents
- Body
- FAQ
Rules:
- Make it engaging using italics, dot points, quotes, bold, spaces, and new lines. No emojis.
- Hyperlink any referenced URLs to their adjacent keyphrases
- Wrap content in container