Skip to content

Instantly share code, notes, and snippets.

@jogaco
Last active July 7, 2016 14:57
Show Gist options
  • Save jogaco/fd7a746747c43343d4418951b33c5f04 to your computer and use it in GitHub Desktop.
Save jogaco/fd7a746747c43343d4418951b33c5f04 to your computer and use it in GitHub Desktop.
class SitemapSettings
DEFAULT_HOST = 'http://www.example.com'
PUBLIC_PATH = 'private/'
SITEMAPS_PATH_NEW = 'sitemapsnew/'
SITEMAPS_PATH_PUBLISHED = 'sitemaps/'
DEFAULT_PRIORITY_MAP = [0.5]
end
require 'rubygems'
require 'sitemap_generator'
last_sitemap = "#{Rails.root}/#{SitemapSettings::PUBLIC_PATH}#{SitemapSettings::SITEMAPS_PATH_NEW}last_sitemap.xml.gz"
FileUtils.cp("#{Rails.root}/#{SitemapSettings::PUBLIC_PATH}#{SitemapSettings::SITEMAPS_PATH_PUBLISHED}sitemap.xml.gz", last_sitemap)
last_lastmod = nil
last_partial_sitemap = nil
sitemap_index_doc = nil
File.open(last_sitemap) do |f|
gz = Zlib::GzipReader.new(f)
sitemap_index_doc = Nokogiri::XML(gz)
last_lastmod = sitemap_index_doc.css('sitemap').last.at_css('lastmod').text
last_partial_sitemap = sitemap_index_doc.css('sitemap').last.at_css('loc').text
gz.close
end
unless last_lastmod
Rails.logger.warn "Sitemap update cannot be generated: could not find lastmod in #{last_sitemap}"
exit 1
end
new_lastmod = Time.now.to_formatted_s(:iso8601)
SitemapGenerator::Sitemap.default_host = SitemapSettings::DEFAULT_HOST
SitemapGenerator::Sitemap.public_path = SitemapSettings::PUBLIC_PATH
SitemapGenerator::Sitemap.sitemaps_path = SitemapSettings::SITEMAPS_PATH_NEW
SitemapGenerator::Sitemap.verbose
sitemap_created = SitemapGenerator::Sitemap.create do
# Obtain the list of updated items since last sitemap entry from sitemap index
updated_items = get_updated_items(last_lastmod, new_lastmod)
updated_items.each do |i|
priority = my_priority(i)
add item_path(i), changefreq: 'never', priority: priority, lastmod: i.updated_at
end
end
Rails.logger.info "Updated Sitemap lastmod: #{new_lastmod}"
last_partial_sitemap =~ /sitemap(\d+)\.xml/
last_sitemap_idx = $1.to_i
if last_sitemap_idx > 0
num_added_files = sitemap_created.sitemap_index.link_count
1.upto(num_added_files) do |i|
FileUtils.mv("#{Rails.root}/#{SitemapSettings::PUBLIC_PATH}#{SitemapSettings::SITEMAPS_PATH_NEW}sitemap#{i}.xml.gz", "#{Rails.root}/#{SitemapSettings::PUBLIC_PATH}#{SitemapSettings::SITEMAPS_PATH_PUBLISHED}sitemap#{last_sitemap_idx + i}.xml.gz")
sitemap_index_doc.css('sitemap').last.add_next_sibling "<sitemap><loc>#{SitemapSettings::DEFAULT_HOST}/#{SitemapSettings::SITEMAPS_PATH_PUBLISHED}sitemap#{last_sitemap_idx + i}.xml.gz</loc><lastmod>#{new_lastmod}</lastmod></sitemap>"
end
new_sitemap_index = "#{Rails.root}/#{SitemapSettings::PUBLIC_PATH}#{SitemapSettings::SITEMAPS_PATH_PUBLISHED}sitemap_new.xml"
File.open(new_sitemap_index, 'w') { |file| file.write sitemap_index_doc.serialize(save_with:0).sub("\n", '') }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment