1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
require 'nokogiri'
require 'redirect_follower'
require "addressable/uri"
require 'uri'
class OpenGraph
attr_accessor :src, :url, :type, :title, :description, :images, :metadata, :response, :original_images
def initialize(src, fallback = true, options = {})
if fallback.is_a? Hash
options = fallback
fallback = true
end
@src = src
@body = nil
@images = []
@metadata = {}
parse_opengraph(options)
load_fallback if fallback
check_images_path
end
private
def parse_opengraph(options = {})
begin
if @src.include? '</html>'
@body = @src
else
@body = RedirectFollower.new(@src, options).resolve.body
end
rescue
@title = @url = @src
return
end
if @body
attrs_list = %w(title url type description)
doc = Nokogiri.parse(@body)
doc.css('meta').each do |m|
if m.attribute('property') && m.attribute('property').to_s.match(/^og:(.+)$/i)
m_content = m.attribute('content').to_s.strip
metadata_name = m.attribute('property').to_s.gsub("og:", "")
@metadata = add_metadata(@metadata, metadata_name, m_content)
case metadata_name
when *attrs_list
self.instance_variable_set("@#{metadata_name}", m_content) unless m_content.empty?
when "image"
add_image(m_content)
end
end
end
end
end
def load_fallback
if @body
doc = Nokogiri.parse(@body)
if @title.to_s.empty? && doc.xpath("//head//title").size > 0
@title = doc.xpath("//head//title").first.text.to_s.strip
end
@url = @src if @url.to_s.empty?
if @description.to_s.empty? && description_meta = doc.xpath("//head//meta[@name='description']").first
@description = description_meta.attribute("content").to_s.strip
end
fetch_images(doc, "//head//link[@rel='image_src']", "href") if @images.empty?
fetch_images(doc, "//img", "src") if @images.empty?
end
end
def check_images_path
@original_images = @images.dup
uri = Addressable::URI.parse(@src)
imgs = @images.dup
@images = []
imgs.each do |img|
if Addressable::URI.parse(img).host.nil?
full_path = uri.join(img).to_s
add_image(full_path)
else
add_image(img)
end
end
end
def add_image(image_url)
@images << image_url unless @images.include?(image_url) || image_url.to_s.empty?
end
def fetch_images(doc, xpath_str, attr)
doc.xpath(xpath_str).each do |link|
add_image(link.attribute(attr).to_s.strip)
end
end
def add_metadata(metadata_container, path, content)
path_elements = path.split(':')
if path_elements.size > 1
current_element = path_elements.delete_at(0)
path = path_elements.join(':')
if metadata_container[current_element.to_sym]
path_pointer = metadata_container[current_element.to_sym].last
index_count = metadata_container[current_element.to_sym].size
metadata_container[current_element.to_sym][index_count - 1] = add_metadata(path_pointer, path, content)
metadata_container
else
metadata_container[current_element.to_sym] = []
metadata_container[current_element.to_sym] << add_metadata({}, path, content)
metadata_container
end
else
metadata_container[path.to_sym] ||= []
metadata_container[path.to_sym] << {'_value'.to_sym => content}
metadata_container
end
end
end
|