Last active
September 4, 2020 12:13
-
-
Save mattvh/6692349 to your computer and use it in GitHub Desktop.
Discovering and saving favicons with Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# by Matt Harzewski | |
# Read more: http://www.webmaster-source.com/2013/09/25/finding-a-websites-favicon-with-ruby/ | |
require "httparty" | |
require "nokogiri" | |
require "base64" | |
class Favicon | |
attr_reader :host | |
attr_reader :uri | |
attr_reader :base64 | |
def initialize(host) | |
@host = host | |
check_for_ico_file | |
check_for_html_tag | |
end | |
# Check /favicon.ico | |
def check_for_ico_file | |
uri = URI::HTTP.build({:host => @host, :path => '/favicon.ico'}).to_s | |
res = HTTParty.get(uri) | |
if res.code == 200 | |
@base64 = Base64.encode64(res.body) | |
@uri = uri | |
end | |
end | |
# Check "shortcut icon" tag | |
def check_for_html_tag | |
uri = URI::HTTP.build({:host => @host, :path => '/'}).to_s | |
res = HTTParty.get(uri) | |
doc = Nokogiri::HTML(res) | |
doc.xpath('//link[@rel="shortcut icon"]').each do |tag| | |
taguri = URI(tag['href']) | |
unless taguri.host.to_s.length < 1 | |
iconuri = taguri.to_s | |
else | |
iconuri = URI.join(uri, taguri).to_s | |
end | |
res = HTTParty.get(iconuri) | |
if res.code == 200 | |
@base64 = Base64.encode64(res.body) | |
@uri = iconuri | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
favicon = Favicon.new("arstechnica.com") | |
puts favicon.uri | |
#Outputs http://static.arstechnica.net/favicon.ico | |
puts favicon.base64 | |
#Outputs a bunch of base64-encoded gibberish, for Data URI usage | |
puts puts favicon.host | |
#Outputs arstechnica.com | |
# Read more at http://www.webmaster-source.com/2013/09/25/finding-a-websites-favicon-with-ruby/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing :)