Sha256: 20c103cd4330916bb5e9ff408aa0b6e7812e26e4a1f54bc1771b432848df0ec8

Contents?: true

Size: 1.04 KB

Versions: 7

Compression:

Stored size: 1.04 KB

Contents

require 'uri'

require 'murlsh'

module Murlsh

  # If document has <meta property="og:image"> use it as the thumbnail.
  class AddPre50OpenGraphImage < Plugin

    @hook = 'add_pre'

    def self.run(url, config)
      if not url.thumbnail_url and url.ask.doc
        url.ask.doc.xpath_search("//meta[@property='og:image']") do |node|
          unless node['content'].to_s.empty?
            og_image_url = node['content']
            Murlsh::failproof do
              # youtube leaves out scheme: for some reason
              if og_image_url[%r{^//}]
                og_image_url = "#{URI(url.url).scheme}:#{og_image_url}"
              end
              thumb_storage = Murlsh::ImgStore.new(config)

              stored_url = thumb_storage.store_url(og_image_url) do |i|
                max_side = config.fetch('thumbnail_max_side', 90)
                i.extend(Murlsh::ImageList).resize_down!(max_side)
              end

              url.thumbnail_url = stored_url  if stored_url
            end
          end
        end
      end
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
murlsh-1.9.3 plugins/add_pre_50_open_graph_image.rb
murlsh-1.9.2 plugins/add_pre_50_open_graph_image.rb
murlsh-1.9.1 plugins/add_pre_50_open_graph_image.rb
murlsh-1.9.0 plugins/add_pre_50_open_graph_image.rb
murlsh-1.8.0 plugins/add_pre_50_open_graph_image.rb
murlsh-1.7.1 plugins/add_pre_50_open_graph_image.rb
murlsh-1.7.0 plugins/add_pre_50_open_graph_image.rb