Skip to content

Instantly share code, notes, and snippets.

@justinburdett
Last active May 3, 2016 09:03
Show Gist options
  • Save justinburdett/4410194 to your computer and use it in GitHub Desktop.
Save justinburdett/4410194 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
@justinburdett
Copy link
Author

@steveklabnik Thanks! I've updated the script with some of your suggestions. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment