Skip to content

Instantly share code, notes, and snippets.

@SkylerLipthay
Last active August 29, 2015 14:23
Show Gist options
  • Save SkylerLipthay/764edd050a9c6c75afb9 to your computer and use it in GitHub Desktop.
Save SkylerLipthay/764edd050a9c6c75afb9 to your computer and use it in GitHub Desktop.
Fontspring WOFF downloader
require 'nokogiri'
require 'open-uri'
require 'base64'
url = ARGV[0]
abort 'Supply a Fontspring URL' if url.nil?
page = Nokogiri::HTML(open(url)) rescue nil
abort 'Fontspring URL is invalid' if page.nil?
faces = page.css('#ffdemo_select option').to_a
abort 'No fonts faces found' if faces.empty?
faces.each do |node|
name = node.content
id = node['value']
print "Downloading #{name}... "
url = "http://www.fontspring.com/ffdemo/iframe/#{id}"
demo = Nokogiri::HTML(open(url)) rescue nil
next puts 'webfont demo page not found; skipping!' if demo.nil?
link = demo.css('link').first
next puts 'webfont link tag not found; skipping!' if link.nil?
link = "http://www.fontspring.com#{link['href']}"
face = open(link) { |f| f.read } rescue nil
next puts 'font face not found; skipping!' if face.nil?
matches = face.match(/url\(data:font\/woff;charset=utf-8;base64,([^\)]*)\)/)
enc = matches && matches[1]
next puts 'invalid font face; skipping!' if enc.nil? || enc.empty?
raw = Base64.decode64(enc) rescue nil
next puts 'invalid base 64; skipping!' if raw.nil?
File.open("#{name}.woff", 'wb') { |f| f.write(raw) }
puts 'done!'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment