I recently have been trying to automate many of the smaller tasks that I need to perform when publishing a blog post. One those tasks is submitting my new sitemap to the various search engines and blogging services. Right now I either do it manually or wait until the search engines crawl my site again.
I quickly found several rake tasks that can automate submitting my sitemap. While the code for the Rake tasks did work I saw some things that needed to be improved, so I set about refactoring them. I was able to come up with a nice set of well behaved Rake tasks for each of the services I wanted to notify of my new content. I now have Rake tasks for Google, Bing, and Ping-o-Matic.
Each Rake task makes a request to the service’s API then checks the status code in the response. If it’s successful it prints a success message, otherwise it prints an error message along with the response body and fails the Rake task. This ensures the Rake tasks will always print an error and then crash when a submission fails, so there will never be any false positives. The complete set of Rake tasks is shown below.
require 'net/http'
require 'uri'
require 'cgi'
require 'xmlrpc/client'
desc 'Ping pingomatic'
task :pingomatic do
cfg = YAML.load_file("_config.yml")
rss_url = "#{cfg['url']}#{cfg['subscribe_rss']}"
puts '* Pinging ping-o-matic'
response = XMLRPC::Client.new('rpc.pingomatic.com', '/').call('weblogUpdates.extendedPing', "#{cfg['title']}" , "#{cfg['url']}", rss_url)
puts response["message"]
if response["flerror"] == true
puts "Ping-o-Matic error: #{response}"
fail
else
puts "Successfully RSS feed to Ping-o-Matic"
end
end
desc 'Notify Google of the new sitemap'
task :sitemapgoogle do
cfg = YAML.load_file("_config.yml")
puts '* Pinging Google about our sitemap'
response = Net::HTTP.get_response('www.google.com', '/webmasters/tools/ping?sitemap=' + URI.escape("#{cfg['url']}/sitemap.xml"))
if response.code == "200"
puts "Successfully submitted sitemap to Google"
else
puts "Google Sitemap response: #{response.body}"
fail
end
end
desc 'Notify Bing of the new sitemap'
task :sitemapbing do
cfg = YAML.load_file("_config.yml")
puts '* Pinging Bing about our sitemap'
response = Net::HTTP.get_response('www.bing.com', '/webmaster/ping.aspx?siteMap=' + URI.escape("#{cfg['url']}/sitemap.xml"))
if response.code == "200"
puts "Successfully submitted sitemap to Bing"
else
puts "Bing Sitemap response: #{response.body}"
fail
end
end
desc 'Notify pubsubhubbub server.'
task :pingpubsubhubbub do
cfg = YAML.load_file("_config.yml")
data = 'hub.mode=publish&hub.url=' + CGI::escape("#{cfg['url']}#{cfg['subscribe_rss']}")
headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
puts '* Pinging pubsubhubbub server'
request = Net::HTTP.new('pubsubhubbub.appspot.com', 80)
response = request.post('http://pubsubhubbub.appspot.com/publish', data, headers)
if response.code == "200" || response.code == "204"
puts "Successfully submitted sitemap to pubsubhubbub"
else
puts "pubsubhubbub response: #{response.body}"
fail
end
end
desc 'Notify all the services about new content'
task :notify => [:pingomatic, :sitemapgoogle, :sitemapbing, :pingpubsubhubbub] do
end
desc "Generate website, deploy, and notify web services of changes"
task :gen_deploy_notify => [:gen_deploy, :notify] do
end
For each of these we check the status code in the HTTP response and then either print the success message, or an error message containing the actual response.