Skip to content

Instantly share code, notes, and snippets.

@cattywampus
Last active October 21, 2015 20:39
Show Gist options
  • Save cattywampus/3055d6a640f92efef702 to your computer and use it in GitHub Desktop.
Save cattywampus/3055d6a640f92efef702 to your computer and use it in GitHub Desktop.
Build SameTime emoticon palettes from GitHub Emoji
require 'mechanize'
require 'open-uri'
EMOJI_SITE = 'http://www.emoji-cheat-sheet.com'
COLLECTIONS = %w[people nature objects places symbols]
agent = Mechanize.new
page = agent.get(EMOJI_SITE)
COLLECTIONS.each do |set|
Dir.mkdir set unless Dir.exist? set
builder = Nokogiri::XML::Builder.new do |xml|
xml.palette('name' => "Emojis-#{set.capitalize}")
end
palette_node = builder.doc.xpath('//palette').first
elements = page.search("#emoji-#{set} > li > div")
elements.each do |element|
emoji_path = element.css('.emoji').first.attributes['data-src'].value
emoji_name = element.css('.name').first.text
filename = emoji_path.rpartition('/').last
File.open(File.join(set, filename), 'wb') do |saved_file|
open(EMOJI_SITE + '/' + emoji_path, 'rb') do |read_file|
saved_file.write(read_file.read)
end
end
Nokogiri::XML::Builder.with(palette_node) do |xml|
xml.item do |xml|
xml.name { |xml| xml.cdata emoji_name }
xml.imgfile { |xml| xml.text filename }
xml.alttext { |xml| xml.cdata emoji_name }
xml.keyboard { |xml| xml.cdata ":#{emoji_name}:" }
xml.kbrdregexp { |xml| xml.cdata "[:]#{emoji_name}[:]" }
end
end
end
File.open("#{set}/palette.xml", 'w') { |f| f.write builder.to_xml }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment