Created
October 21, 2012 21:40
-
-
Save FiXato/3928600 to your computer and use it in GitHub Desktop.
Favicon Grabber
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
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
# Favicon Grabber by Filip H.F. "FiXato" Slagter | |
require 'open-uri' | |
require 'nokogiri' | |
require 'fileutils' | |
ICONS_DIR = File.expand_path(File.join("~",'favicons')) | |
favicon_urls = Hash.new{|h,k|h[k] = []} | |
urls = { | |
'wikipedia' => 'http://en.wikipedia.org/wiki/Favicon#HTML5_recommendation_for_icons_in_multiple_sizes', | |
'arstechnica' => 'http://arstechnica.com/security/2012/08/passwords-under-assault/', | |
'tutsplus' => 'http://net.tutsplus.com/tutorials/html-css-techniques/object-oriented-css-what-how-and-why/', | |
'fixato.org' => 'http://fixato.org', | |
'netvibes' => 'http://netvibes.com' | |
} | |
urls.each do |service_name,url| | |
found_url = false | |
doc = Nokogiri::HTML(open(url)) rescue next | |
next if doc.nil? | |
doc.css('link').each do |link| | |
if %['shortcut icon', 'icon', 'shortcut'].include?(link['rel']) | |
favicon_urls[service_name] << [URI::join(url, link['href']).to_s, (link['sizes'].split rescue [])] | |
found_url = true | |
end | |
end | |
favicon_urls[service_name] << [URI::join(url, '/favicon.ico').to_s, []] unless found_url | |
end | |
FileUtils.mkpath(ICONS_DIR) | |
favicon_urls.each do |service_name,icons| | |
puts "#{service_name}:" | |
if icons.compact.size == 0 | |
puts " !!!No icons!!!" | |
puts | |
next | |
end | |
puts " #{icons.compact.size} icons found:" | |
icons.compact.each do |url, sizes| | |
md = URI(URI.encode(url)).path.match(/(.+)(\..+)/) | |
if md.nil? | |
puts " !!!#{url} doesn't match pattern" | |
next | |
end | |
ext = md.captures.last | |
puts " #{url} -> #{[sizes].flatten.compact.join(', ')}" | |
dir_names = [] | |
if ext.downcase == '.ico' | |
dir_names << 'ico' | |
elsif [sizes].flatten.compact.size > 0 | |
dir_names += [sizes].flatten.compact | |
else | |
dir_names << 'unknown' | |
end | |
dir_names.each do |dir_name| | |
dir = File.join(ICONS_DIR, dir_name) | |
FileUtils.mkpath(dir) | |
target = File.join(dir, "#{service_name}#{ext}") | |
puts " Downloading #{url} -> #{target}" | |
open("#{target}", "wb") do |file| | |
open(url) do |uri| | |
file.write(uri.read) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment