How to generate sitemap for videos on rails app?

113 Views Asked by At

I am using sitemap_generator for my rails app. I was able to generate sitemap for everything except for video links on the website. Following is my

sitemap.rb

Equipment.find_each do |e|
  add search_equipments_path(e), :changefreq => 'weekly'
 end

 Equipment.find_each do |more|
  add equipment_details_path(more), :changefreq => 'weekly'
 end

 Category.find_each do |c|
  add search_equipments_path(:category_id=>c.id), :changefreq => 'weekly'
 end

 Category.find_each do |sub|
  add search_equipments_path(:sub_category=>sub.id), :changefreq => 'weekly'
 end

 Service.find_each do |s|
  add service_details_path(s), :changefreq => 'weekly'
 end

 WantedEquipment.find_each do |f|
  add wanted_equipments_path(f), :changefreq => 'weekly'
 end

 StaticPage.find_each do |t|
  add benefits_for_buyers_path(t), :changefreq => 'weekly'
 end

 StaticPage.find_each do |sel|
  add benefits_for_sellers_path(sel), :changefreq=> 'weekly'
 end

 StaticPage.find_each do |ser|
  add benefits_for_service_providers_path(ser), :changefreq=> 'weekly'
 end

 add '/about-us'
 add '/terms-and-conditions'
 add '/pricing-plans'
 add '/how-it-work'

This what is given on the documentation on the gem page for generating video sitemap:

SitemapGenerator::Sitemap.create do
  add('/index.html', :video => {
    :thumbnail_loc => 'http://www.example.com/video1_thumbnail.png',
    :title => 'Title',
    :description => 'Description',
    :content_loc => 'http://www.example.com/cool_video.mpg',
    :tags => %w[one two three],
    :category => 'Category'
  })
end

I am not able to understand how to pass these values dynamically.

2

There are 2 best solutions below

0
Manish Nagdewani On BEST ANSWER

If you want to generate video contents on basis of dynamic data, try this:

Model.all.each do |e|
      video = VideoInfo.new(e.video_url)
      add("https://example.com/views/#{e.slug}", :video => {
        :thumbnail_loc => "#{video.thumbnail_medium}",
        :title => "#{video.title}",
        :description => "#{video.description}",
        :content_loc => "#{e.video_url}",
        :tags => e.slug.split('-'),
        :player_loc => video.embed_url  
        })
end

Here VideoInfo is gem used for getting video meta info.

5
Nezir On

Can you use this sample but change it to your model name:

 SitemapGenerator::Sitemap.create do
  add '/contact_us', 'changefreq': 'weekly'
  Article.find_each do |article|
    add article_path(article), lastmod: article.updated_at
  end
end

As you can see he makes find_each on articles model. So you can do the same on your video model.