Created
August 8, 2019 02:07
-
-
Save dicene/509b9c8d6b48ce060ccc9315148f0cc1 to your computer and use it in GitHub Desktop.
Mudlet Spell Scraper
This file contains 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
function downloadSpell() | |
print("Downloading...") | |
--downloadFile(getMudletHomeDir().."/spell.html", [[https://www.dndbeyond.com/spells?filter-class=0&filter-search=acid+splash&filter-verbal=&filter-somatic=&filter-%20material=&filter-concentration=&filter-ritual=&filter-sub-class=]]) | |
downloadFile(getMudletHomeDir().."/spell.html", [[http://regalgoblins.com/spells-5e.php]]) | |
end | |
function trim(s) | |
return (s:gsub("^%s*(.-)%s*$", "%1")) | |
end | |
function magiclines(s) | |
if s:sub(-1)~="\n" then s=s.."\n" end | |
return s:gmatch("(.-)\n") | |
end | |
function scrapeSpell(_, filename) | |
-- is the file that downloaded ours? | |
if not filename:match("spell", 1, true) then return end | |
-- parse our downloaded file for the player count | |
io.input(filename) | |
local s = io.read("*all") | |
spellList = {} | |
--display(s) | |
local currentSpell = {} | |
for line in magiclines(s) do | |
--display(line) | |
if descriptionNext then | |
currentSpell.description = line:trim() | |
end | |
descriptionNext = false | |
if line:match([[<header class="clearfix">]]) then | |
--echo("start of spell") | |
if currentSpell and currentSpell.name then | |
spellList[currentSpell.name] = currentSpell | |
end | |
currentSpell = {} | |
elseif line:match([[<div id="level">%d+</div>]]) then | |
currentSpell.level = line:match([[<div id="level">(%d+)</div>]]) | |
elseif line:match([[<div id="level"><img src="/images/star.png" class="star"/></div>]]) then | |
currentSpell.level = "cantrip" | |
elseif line:match([[<div id="basics">(.+)</div>]]) then | |
currentSpell.name = line:match([[<div id="basics">(.+)</div>]]) | |
elseif line:match([[<div id="components" class=" (.+)">]]) then | |
local list = line:match([[<div id="components" class=" (.+)">]]) | |
if list:match("somatic") then currentSpell.somatic = true end | |
if list:match("verbal") then currentSpell.verbal = true end | |
if list:match("material") then currentSpell.material = true end | |
elseif line:match([[<dt>Casting Time</dt><dd>(.+) </dd>]]) then | |
currentSpell.castingTime = line:match([[<dt>Casting Time</dt><dd>(.+) </dd>]]) | |
elseif line:match([[<dt>Casting Time</dt><dd>(.+) </dd>]]) then | |
currentSpell.range = line:match([[<dt>Range</dt><dd>(.+) </dd>]]) | |
elseif line:match([[<dt>Duration</dt><dd>(.+) </dd>]]) then | |
currentSpell.duration = line:match([[<dt>Duration</dt><dd>(.+) </dd>]]) | |
elseif line:match([[<dt>Materials</dt><dd>(.+)</dd>]]) then | |
currentSpell.materials = line:match([[<dt>Materials</dt><dd>(.+) </dd>]]) | |
elseif line:match([[<div id="description%-content">]]) then | |
descriptionNext = true | |
--echo("Starting description!\n") | |
elseif line:match([[<dt>School</dt><dd>(.+) </dd>]]) then | |
currentSpell.school = line:match([[<dt>School</dt><dd>(.+) </dd>]]) | |
elseif line:match([[<dt>Book</dt><dd>(.+) </dd>]]) then | |
currentSpell.book = line:match([[<dt>Book</dt><dd>(.+) </dd>]]) | |
end | |
end | |
for name, spell in pairs(spellList) do | |
echo(name .. " -> " .. spell.level .. "\n") | |
end | |
end | |
registerAnonymousEventHandler("sysDownloadDone", "scrapeSpell") |
Author
dicene
commented
Aug 8, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment