Sha256: db352dd7f846b04b3b12fd5e4f6537ef0808e8cdf62166d7ecb65092bf36be30

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

require 'cgi'
require 'uri'

require 'murlsh'

module Murlsh

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

    @hook = 'add_pre'

    StorageDir = File.join(File.dirname(__FILE__), '..', 'public', 'img',
      'thumb')

    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|
          if node and not 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(StorageDir,
                :user_agent => config['user_agent'])

              stored_filename = 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 = "img/thumb/#{CGI.escape(stored_filename)}"
            end
          end
        end
      end
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
murlsh-1.4.1 plugins/add_pre_50_open_graph_image.rb
murlsh-1.4.0 plugins/add_pre_50_open_graph_image.rb
murlsh-1.3.1 plugins/add_pre_50_open_graph_image.rb
murlsh-1.3.0 plugins/add_pre_50_open_graph_image.rb