Search code examples
rubyhttp-status-code-404instagramcloudsight

Escape/Bypass 404 error in Ruby (Instagram API)


I'm trying to get ruby to escape/bypass a 404 error but it doesn't seem to be working. Here's my code so far:

require 'rubygems'
require 'simple_oauth'
require 'cloudsight'
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'open-uri'
require 'openssl'
require 'hpricot'
require 'nokogiri'


#T FILE TO JPG

webby = 'https://t.co/5h92pCmoPM'

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

doc = Hpricot(open(webby)).to_s


jpgstart = '<meta property=\"og:image\" content=\"'
jpgstop = '" />'
jpglink = doc[/#{jpgstart}(.*?)#{jpgstop}/m, 1]
print jpglink

This is the error I get:

C:/Users/User/Desktop/test89:18: warning: already initialized constant OpenSSL::SSL::VERIFY_PEER
C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:353:in `open_http': 404 NOT FOUND (OpenURI::HTTPError)
    from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:724:in `buffer_open'
    from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:210:in `block in open_loop'
    from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:208:in `catch'
    from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:208:in `open_loop'
    from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:149:in `open_uri'
    from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:704:in `open'
    from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:34:in `open'
    from C:/Users/KVadher/Desktop/test89:20:in `<main>'
[Finished in 2.7s with exit code 1]

Does anyone have any idea how I could bypass the error?


Solution

  • Is this what you had in mind?

    require 'rubygems'
    require 'simple_oauth'
    require 'cloudsight'
    require 'rubygems'
    require 'net/http'
    require 'uri'
    require 'json'
    require 'open-uri'
    require 'openssl'
    require 'hpricot'
    require 'nokogiri'
    
    
    #T FILE TO JPG
    
    webby = 'https://t.co/5h92pCmoPM'
    
    OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
    
    begin
      doc = Hpricot(open(webby)).to_s
    
      jpgstart = '<meta property=\"og:image\" content=\"'
      jpgstop = '" />'
      jpglink = doc[/#{jpgstart}(.*?)#{jpgstop}/m, 1]
    
      print jpglink
    rescue OpenURI::HTTPError
      # do nothing. bypass error.
    end