ChatGPT – Basic HTML Autoblog

This project uses ChatGPT to automatically create blog posts and append them to the beginning of an HTML file.

For HTML formatting we split the string response provided by ChatGPT on “\n” into a list, and then do a for x loop to add <p></p> around each paragraph.

To append to the beginning of the file we read the previous version for the file into a variable value, and then concatenate the new post to the old value and then overwrite the old document.

import openai
openai.api_key = "APIKEY"

title ="Crazy facts About lice"

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "system", "content": "You are a journalist."},
    {"role": "assistant", "content": "write a 500 word blog post"},
    {"role": "user", "content": title}
  ]
)

print(title)
print(response["choices"][0]["message"]["content"])

body = response["choices"][0]["message"]["content"]
body = body.split("\n")
body_html = ""
for x in body:
    body_html = f"{body_html} <p>{x}</p>\n"

html = f"<h1>{title}</h1> {body_html}"

file_new = open("blog.html","a+")
file_new.close()

original = open("blog.html", "r")
html_old = original.read()
original.close()

html = f"{html}\n\n {html_old}"
file = open("blog.html", "w")
file.write(html)
file.close()
Code language: Python (python)