Skip to content

Instantly share code, notes, and snippets.

@avogel3
Created May 5, 2022 03:51
Show Gist options
  • Save avogel3/428785104b2c0d63d3f6834fcca30bbd to your computer and use it in GitHub Desktop.
Save avogel3/428785104b2c0d63d3f6834fcca30bbd to your computer and use it in GitHub Desktop.
Get a list of mechanical keyboard switches by scraping KeyBumps
# Full Credit to Keybumps for putting together this list
#
# This script assumes you have ruby and the bundler gem installed
#
# ruby switch_scraper.rb
#
# Will write a switches.json file that is an array of switches
# with attributes model,feel,actuation,pre-travel,total-travel,mount
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'nokogiri'
end
require 'open-uri'
require 'json'
doc = Nokogiri::HTML(URI.open('https://keybumps.com/switch-list.html'))
table_headers = doc.css('table > thead > tr > th').map do |th|
th.text.strip.downcase.gsub(' ', '_').gsub('-', '_')
end
vals = doc.css('table > tbody > tr').map do |tr|
row_val = table_headers.each_with_index.map do |th, index|
next '' if th.empty?
tr.css('td').at(index).text.strip
end.reject(&:empty?)
table_headers.reject(&:empty?).zip(row_val).to_h
end
File.open('switches.json', 'w') do |f|
f.write(JSON.pretty_generate(vals))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment