FaviconGet is a Ruby gem for finding and retrieving website favicons (icons). It's a port of the popular favicon Python library.
Add this line to your application's Gemfile:
gem 'favicon_get'
And then execute:
$ bundle install
Or install it manually:
$ gem install favicon_get
require 'favicon_get'
icons = FaviconGet.get('https://www.ruby-lang.org/')
# => [Icon, Icon, Icon, ...]
# The first icon is usually the largest
icon = icons.first
puts "URL: #{icon.url}"
puts "Size: #{icon.width}x#{icon.height}"
puts "Format: #{icon.format}"
require 'favicon_get'
require 'open-uri'
icons = FaviconGet.get('https://www.ruby-lang.org/')
icon = icons.first
URI.open(icon.url) do |image|
File.open("/tmp/ruby-favicon.#{icon.format}", "wb") do |file|
file.write(image.read)
end
end
# => /tmp/ruby-favicon.png
require 'favicon_get'
# Custom headers
headers = {
'User-Agent' => 'My custom User-Agent'
}
# Timeout and other parameters
FaviconGet.get('https://www.ruby-lang.org/',
headers: headers,
timeout: 5)
Each icon is represented by the Icon
struct with the following attributes:
url
- Full URL to the icon filewidth
- Icon width in pixels (0 if unknown)height
- Icon height in pixels (0 if unknown)format
- File format/extension (e.g., 'ico', 'png')
Icons are sorted by size (larger first) and format priority.
After checking out the repo, run make setup
to install dependencies.
You can use the following Makefile commands for development:
make test
- Run testsmake console
- Get an interactive prompt with the gem loadedmake example
- Run the example scriptmake build
- Build the gemmake install
- Install the gem locallymake up
- Increment patch version (e.g., 0.1.0 → 0.1.1)make up!
- Increment minor version (e.g., 0.1.1 → 0.2.0)make push
- Push gem to RubyGems.org (requires permissions)
- nokogiri - for HTML parsing
- faraday - for HTTP requests
- faraday-follow_redirects - for following HTTP redirects
This gem is available as open source under the terms of the MIT License.
Bug reports and pull requests are welcome on GitHub at https://github.com/inem/favicon.