jcouture / ogp

Simple Ruby library for parsing Open Graph prototocol information. See http://ogp.me for more information.
MIT License
36 stars 22 forks source link

OGP::MalformedSourceError when trying to parse Twitter posts/users #11

Open akoskovacs opened 3 years ago

akoskovacs commented 3 years ago

Hi! It seems OGP is unable to parse Twitter's OpenGraph properties.

I think the problem might be that the meta tag's attributes are in an unusual order. This code shows the problem:

require 'http'
require 'ogp'

class OGPTest
  attr_accessor :href

  def initialize(href: nil)
    @href = href
  end

  def analyze_opengraph
    return nil if @href.nil?

    begin
      connection = HTTP
        .follow(max_hops: 5)
        .timeout(30)
        .get(@href)

      if connection.status == 200
        return OGP::OpenGraph.new(connection.body.to_str)
      end
    rescue Exception => e
      puts "Error while trying to access '#{@href}'"
      raise
      return nil
    end
  end
end

c = OGPTest.new href: 'https://twitter.com/jack'
puts c.analyze_opengraph

Outputs:

Error while trying to access 'https://twitter.com/jack'
/home/akos/.rvm/gems/ruby-3.0.0/gems/ogp-0.5.0/lib/ogp/open_graph.rb:27:in `initialize': OGP::MalformedSourceError (OGP::MalformedSourceError)
    from a.rb:22:in `new'
    from a.rb:22:in `analyze_opengraph'
    from a.rb:33:in `<main>'
azeemh commented 3 years ago

I have this same problem. It also happens on soundcloud links.

azeemh commented 3 years ago

so i found that twitter doesn't actually implement OGP when discussing this at Open Graph Reader. Open Graph Reader also worked on soundcloud links. just an fyi for anyone else who runs into this. peace