Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hanynowsky/669c9b541663374adc8aa355d88fe495 to your computer and use it in GitHub Desktop.
Save hanynowsky/669c9b541663374adc8aa355d88fe495 to your computer and use it in GitHub Desktop.
This Ruby script generates a random list of Magic: the Gathering cards, ideally for use in making a random Magic cube, and outputs the card list into a text file (suitable for importing into Magic Online or for use in bulk card shopping). For more information on cube drafting, see cubedrafting.com. You can modify the number of cards generated an…
require 'nokogiri'
require 'open-uri'
print "How many cards? "
CARDS_IN_CUBE = gets.strip.to_i
print "What format? "
LEGAL_IN_FORMAT = gets.strip
card_count = 1
# Create or open a text file in the same directory as this file and stores the card names there.
# If the file already exists, it will overwrite it, so you may want to change the name of the file for multiple random cubes.
File.open('random-cube.txt', 'w') do |file|
while card_count <= CARDS_IN_CUBE
# Open a random card
doc = Nokogiri::HTML(open('http://magiccards.info/random.html'))
# Check for the card's format
legal_formats = doc.css('li.legal').collect{|format| format.content}
if legal_formats.select{|v| v =~ /#{LEGAL_IN_FORMAT}/}
# Parse the title and display it
doc.xpath('//title').each do |link|
content = link.content.split("(").first
file.puts "1 " + content
puts "#" + card_count.to_s + ": " + content
end
card_count = card_count + 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment