Skip to content

Instantly share code, notes, and snippets.

@basicfeatures
Created June 16, 2023 17:59
Show Gist options
  • Save basicfeatures/d135e7a15dbf8210e29d2b9edda996ee to your computer and use it in GitHub Desktop.
Save basicfeatures/d135e7a15dbf8210e29d2b9edda996ee to your computer and use it in GitHub Desktop.
$ cat ai/seo_tool.rb
# ruby seo_script.rb www.yourwebsite.com your_keyword
require "ahrefs"
require "openai"
Ahrefs.configure do |config|
config.token = "your_ahrefs_token"
end
Openai.api_key = "your_openai_api_key"
# Define your website and target keyword
website = ARGV[0]
keyword = ARGV[1]
def get_seo_data(website)
result = {}
# Get backlink data
result[:backlinks] = Ahrefs::Backlinks.new(website).get
# Get competitor data
result[:competitors] = Ahrefs::Keywords.new(website).competitors
# Get top pages data
result[:top_pages] = Ahrefs::Pages.new(website).top
result
end
def suggest_backlink_actions(data)
# Extract the information from the data
new_backlinks = data[:backlinks].filter { |link| link["date"] > (Date.today - 30) }
lost_backlinks = data[:backlinks].filter { |link| link["date"] < (Date.today - 30) }
# Generate a prompt for GPT-3 based on the data
prompt = "We have gained #{new_backlinks.count} new backlinks and lost #{lost_backlinks.count} backlinks. Please provide detailed suggestions for improving our backlink profile."
# Call the OpenAI API
response = Openai::Gpt3.complete(
model: "text-davinci-002",
prompt: prompt,
max_tokens: 150
)
# Print out the generated suggestions
puts "Suggestions for improving backlink profile:"
puts response["choices"][0]["text"]
end
def suggest_competitor_actions(data)
# Generate a prompt for GPT-3 based on the data
prompt = "Here are the top competitors for our website: #{data[:competitors].join(", ")}. Please provide detailed suggestions for outperforming them."
# Call the OpenAI API
response = Openai::Gpt3.complete(
model: "text-davinci-002",
prompt: prompt,
max_tokens: 150
)
# Print out the generated suggestions
puts "Suggestions for outperforming competitors:"
puts response["choices"][0]["text"]
end
def suggest_top_page_actions(data)
# Generate a prompt for GPT-3 based on the data
prompt = "Here are the top pages on our website: #{data[:top_pages].join(", ")}. Please provide detailed suggestions for improving their SEO performance."
# Call the OpenAI API
response = Openai::Gpt3.complete(
model: "text-davinci-002",
prompt: prompt,
max_tokens: 150
)
# Print out the generated suggestions
puts "Suggestions for improving top page performance:"
puts response["choices"][0]["text"]
end
# Call the functions to get the SEO data and generate suggestions
data = get_seo_data(website)
suggest_backlink_actions(data)
suggest_competitor_actions(data)
suggest_top_page_actions(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment