Created
October 12, 2017 16:09
-
-
Save brandoncordell/bcdb9fe14474ea37ec9339894258a021 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'pry' | |
spells = {} | |
main_page = Nokogiri::HTML(open('https://www.dnd-spells.com/spells')) | |
spell_rows = main_page.css('#example tr') | |
spell_row_number = 0 | |
spell_rows.each do |row| | |
spell_row_number += 1 | |
spell_link_cell = row.css('td a:first').first | |
level_cell = row.css('td:nth-child(3)').first | |
school_cell = row.css('td:nth-child(4)').first | |
casting_time_cell = row.css('td:nth-child(5)').first | |
ritual_cell = row.css('td:nth-child(6)').first | |
concentration_cell = row.css('td:nth-child(7)').first | |
unless spell_link_cell.nil? | |
title = spell_link_cell.content.strip | |
next if title == "Trap the Soul" | |
puts "Parsing ##{spell_row_number} - #{title}" | |
level = level_cell.content.strip | |
school = school_cell.content.strip | |
casting_time = casting_time_cell.content.strip | |
ritual = (ritual_cell.content.strip == 'Yes') ? true : false | |
concentration = (concentration_cell.content.strip == 'Yes') ? true : false | |
spell = {} | |
spell[title] = { | |
castingTime: casting_time, | |
concentration: concentration, | |
level: level, | |
ritual: ritual, | |
school: school | |
} | |
href = spell_link_cell.attribute('href') | |
page = Nokogiri::HTML(open(href)) | |
range = page.xpath('//*[@id="content"]/div/div/div/div/p[2]/strong[3]').first.children.first.content.strip | |
components = page.xpath('//*[@id="content"]/div/div/div/div/p[2]/strong[4]').first.children.first.content.strip | |
duration = page.xpath('//*[@id="content"]/div/div/div/div/p[2]/strong[5]').first.children.first.content.strip | |
description = page.xpath('//*[@id="content"]/div/div/div/div/p[3]').first.children.map { |element| element.content.strip unless element.matches?('br') }.compact | |
if page.xpath('//*[@id="content"]/div/div/div/div/h4[1]/span').empty? | |
higherLevelsDescription = '' | |
source = page.xpath('//*[@id="content"]/div/div/div/div/p[4]').first.children.first.content.strip | |
else | |
if page.xpath('//*[@id="content"]/div/div/div/div/p[4]').first.children.empty? | |
higherLevelsDescription = page.xpath('//*[@id="content"]/div/div/div/div/p[6]').first.children.map { |element| element.content.strip unless element.matches?('br') }.compact | |
source = page.xpath('//*[@id="content"]/div/div/div/div/p[7]').first.children.first.content.strip | |
else | |
higherLevelsDescription = page.xpath('//*[@id="content"]/div/div/div/div/p[4]').first.children.map { |element| element.content.strip unless element.matches?('br') }.compact | |
source = page.xpath('//*[@id="content"]/div/div/div/div/p[5]').first.children.first.content.strip | |
end | |
end | |
spell[title]['components'] = components | |
spell[title]['description'] = description | |
spell[title]['higherLevelsDescription'] = higherLevelsDescription | |
spell[title]['range'] = range | |
spell[title]['source'] = source | |
spells.merge! spell | |
end | |
end | |
# File.write('final-spells.json', JSON.generate(spells)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment