[Step-by-Step Guide] Find keywords, create and publish 150 articles to wordpress site in less than 30 minutes using GPTChat and Python/Google Colab

what exactly is the purpose of using colab? why not just run the python code locally. thats what i did and it works just fine.
 
what exactly is the purpose of using colab? why not just run the python code locally. thats what i did and it works just fine.
i'd say both ways work just fine, but IMO colab makes it really easy for beginners.

You can do a lot wrong when trying to install and run python locally so colab just gets rid of setup problems.
 
Thanks
  1. It takes less that 30 mins to start posting 150 posts to your wordpress site, and it should not take more than 1 hour to have them all online.
  2. 100% free method except that we need an GPTChat api key. But you can get a free 5 usd trial so this can be free as well
  3. We create 150 keywords using ahrefs free tool - https://ahrefs.com/keyword-generator
  4. We paste some code in Google colab.
  5. Make sure to not make your Google colab public for safety. It should be private by default, but it is better to check that all the time.
  6. This code is responsible for creating posts via GPTChat API and posting to your wordpress site.
  7. We create credential for wordpress site to be able to post there
  8. We decide how much posts we want and then click a button
  9. It posts automatically after we click a button.

Please note this is my first tutorial so If I broken any rules let me know and I will fix that. I have google colab file with code, I am not sure how to upload it.


Lets start...
1) Go to https://ahrefs.com/keyword-generator and add any keyword you want. Select both the keywords and the questions exactly like this, and paste them to a txt file.

View attachment 274230


keyword file is going to look like this, it is messy, but we will format it automatically using python.

View attachment 274231


02) Now we need to create out own Google Colab (which is also 100% free to use)

go to https://colab.google/ and click on New Notebook

View attachment 274232

This is how it looks like, we just need to add a few lines of code, create file and we are ready to go

View attachment 274233


Add this code to first line, this install openai:

Code:
 !pip install openai --quiet

This code need to be added to the second line, it formats keywords and contain all the functions:

Code:
import openai
import os
openai.api_key = "your_open_ai_api_key"

def make_post(the_title,the_text,your_user,your_password,your_site,wordpress_category):
        import requests
        import base64
        your_credentials = your_user + ":" + your_password
        your_token = base64.b64encode(your_credentials.encode())
        your_header = {'Authorization': 'Basic ' + your_token.decode('utf-8')}
 
        api_url = your_site+'/wp-json/wp/v2/posts'
        if not wordpress_category == "":
            data = {
                'title' : the_title,
                'status': 'publish',
                'content': the_text,
                'categories': 3
                ##'slug' : 'example-post',
                }
        else:
            data = {
                'title' : the_title,
                'status': 'publish',
                'content': the_text,
                }
            
        response = requests.post(api_url,headers=your_header, json=data)
        return response.json()

def gpt_chat(all_params):
    the_keyword,the_prefix,the_temperature,the_max_tokens = all_params
    the_text = the_prefix + the_keyword + ":"
        #the_text =  the_prefix + the_text
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": the_text}],
        temperature=the_temperature,
        max_tokens=the_max_tokens
        )
 
    the_result = response["choices"][0]["message"]["content"]
 
    return the_result

def fix_ahrefs_keywords(file_with_keywords):
    #remove empty lines and non keyword lines from output of https://ahrefs.com/keyword-generator
    with open(file_with_keywords, 'r') as fyl:
        lines = fyl.readlines()
 
    good_lines = []
    for aline in lines:
        aline = aline.strip()
        if (not any(str.isdigit(x) for x in aline) or len(aline.split())>3) and not aline.strip()=="" and not "Sign up" in aline and not "N/A" in aline:
            good_lines.append(aline)
  
  
     
    with open(file_with_keywords, 'w') as f:
        for line in good_lines:
            f.write(f"{line}\n")

#load file with your keywords
file_with_keywords = "sample_data/keywords.txt"
#this step fixes the file with keywords from https://ahrefs.com/keyword-generator
fix_ahrefs_keywords(file_with_keywords)


Add this code to the third line:

Code:
# SETTINGS
begin_index = 11
end_index = 12
your_site = "yur site url"
your_user = "your site username"
your_password = "your site application password"
wordpress_category = "" # or you can add here a category id
title_is_keyword = "yes" #cam be "yes" or "no". If yes then the title is the keyword, if no then the title is created by GPTCHAT
remove_ai_detection = "no"


#prefix = "Write an article about"
prefix = "Write a very extremelly long and detailed article about "
#load the fixed keywords
with open(file_with_keywords, 'r') as fyl:
    keywords = fyl.readlines()
keywords = [x.strip() for x in keywords]
 
#now we create posts using GPT-chat and post them to our wordpress site
for e,akeyword in enumerate(keywords):
 
    if e<begin_index or e>=end_index:continue #this makes sure we only add article from begin to end
    the_temperature = 0.7
    the_max_tokens = 2000
    all_params = akeyword,prefix,the_temperature,the_max_tokens
    print("we are writing post #",e,", using keyword:",akeyword)
    gptchat_article = gpt_chat(all_params)
    title = akeyword.title()
 
    try:
        gptchat_article_list = gptchat_article.split("\n")
        if gptchat_article_list[0].count(".")<=1 and gptchat_article_list[1].strip()=="":
            title = gptchat_article_list[0]
            gptchat_article = gptchat_article.replace(title,"").strip()
            if title_is_keyword=="yes":
                title = akeyword.title()
    except:pass
 
 
    #now we post to out wordpres site
    try:
        the_response = make_post(title,gptchat_article,your_user,your_password,your_site,wordpress_category)
        the_link = the_response['guid']['rendered']
        print("the_link",the_link,"word count",len(gptchat_article.split()),"the_title:",title)
    except Exception as err:
        print("we have an error",err)

This should look like this:

View attachment 274243



03) The next step is to create a file with keyword and paste the keywords we already have. We click on folder on the left, and create a file in folder sample_data called keywords. Then we click on keywords and there we add out keywords.
View attachment 274235



05) So now the keyword part is done we just need to add out WordPress credentials and we are ready to go.

View attachment 274241

We go to Users/profile in the WordPress dashboard and then we create New Application Password Name. This is just name of password, we do not use it anywhere else. So after we add it, we click on Add New Application Password. This is the password that we need to copy and add to the Google Colab.

06) Then we add our settings that we have just created:

View attachment 274238

To control how many posts to you post you change begin_index and end_index. For example if begin_index=0 and end_index=1 it will post just the first posts. To post to your site, you need to click on run button in every cell. There are 3 cells, so you need to click each time.
Thanks for this well written guide, I've been looking for such tutorial for quite some time now-
 
That's great guide .
I am interested in blogging automation but I don't know how to start and do it .
This sharing helped me a lot .
Thank you for the detailed guide bro .
 
That's great guide .
I am interested in blogging automation but I don't know how to start and do it .
This sharing helped me a lot .
Thank you for the detailed guide bro .
I am glad to help.
 
Nice share. Thanks for your share
 
we are writing post # 3 , using keyword: what is chatgpt
we have an error 'guid'
we are writing post # 4 , using keyword: chatgpt detector
we have an error 'guid'
we are writing post # 5 , using keyword: chatgpt api
we have an error 'guid'

Can any one help me with this? I am a newbie.
 
we are writing post # 3 , using keyword: what is chatgpt
we have an error 'guid'
we are writing post # 4 , using keyword: chatgpt detector
we have an error 'guid'
we are writing post # 5 , using keyword: chatgpt api
we have an error 'guid'

Can any one help me with this? I am a newbie.

Hello, this means that post is generated, but there is a problem when posting to WordPress. Did you put correct credentials?
username = username of the WordPress (the username you use to login to your WordPress site)
password = application pasword that you created
url = need to end with .com (or .org) without anything added after.


Also what is the version of wordpress that you use?
 
Hello, this means that post is generated, but there is a problem when posting to WordPress. Did you put correct credentials?
username = username of the WordPress (the username you use to login to your WordPress site)
password = application pasword that you created
url = need to end with .com (or .org) without anything added after.


Also what is the version of wordpress that you use?
It's working now,
The username was incorrect so that's why.
Thank you!
I'm using WordPress 6.2
 
Nice tutorial, I will take a look at it later. How many words does chatGPT produce for wash post. I know it's next to impossible to get a 1000+ post from the API without some sort of iteration.

Thanks,
 
I get the following message:

we are writing post # 11 , using keyword: indoor apartment vegetable gardening
we have an error 'guid'

i have checked wordpress credentials multiple times and know for a fact they are right. also made an application password. it evens shows the application password just underneath the button you have to click to create it, so i know i made the password

Could it be because my website url ends with .blog ?


EDIT: solved. somehow it did not generate a password because i seemed to not use enough or the right kind of characters? so i made up a much more difficult name, and it generated the password.
 
Last edited:
I get the following message:

we are writing post # 11 , using keyword: indoor apartment vegetable gardening
we have an error 'guid'

i have checked wordpress credentials multiple times and know for a fact they are right. also made an application password. it evens shows the application password just underneath the button you have to click to create it, so i know i made the password

Could it be because my website url ends with .blog ?


EDIT: solved. somehow it did not generate a password because i seemed to not use enough or the right kind of characters? so i made up a much more difficult name, and it generated the password.

Awesome, it can end with anything, just if you put a slash after the domain extension, it does not work.

The aplication name is not important, this is only confusing thing about this setup. You need to use a wordpress username.
 
what exactly is the purpose of using colab? why not just run the python code locally. thats what i did and it works just fine.

It is much easier for people who do not know how to install python. Also connection is better via Google colab usually.

Yes in the long run is easier to just use Python on your PC.
 
Worked well for me, but is there a way to generate image into the content as well?

And also do i need to run the code each time i need to write a post or it automatically loop through the keywords.txt and writes to wordpress?
 
Back
Top