Files
dungeon-sheets/SaveSpells.ipynb
T
2019-03-21 16:39:45 -04:00

235 lines
5.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from tqdm import tqdm\n",
"import string\n",
"import os"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# scrape and save all spells"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from DnD4py.lookup_5e import DnDSpell, Roll20Spell"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('all_spells.csv')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"spells = {}\n",
"failed = {}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"names = sorted([s.lower().replace('', '').replace('/', '').replace('\\'', '') for s in df['Name'].values])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 461/461 [06:02<00:00, 1.27it/s]\n"
]
}
],
"source": [
"for i, name in tqdm(enumerate(names), total=len(names)):\n",
" if name in spells:\n",
" continue\n",
" try:\n",
" spells[name] = DnDSpell(name).as_dungeonsheets_class()\n",
" except Exception as e1:\n",
" try:\n",
" spells[name] = Roll20Spell(name).as_dungeonsheets_class()\n",
" except Exception as e2:\n",
" failed[name] = (e1, e2)\n",
" print(f'Failed: {name}')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def save_spell(name, text):\n",
" filename = f'dungeonsheets/spells/spells_{name[0].lower()}.py'\n",
" if os.path.isfile(filename):\n",
" with open(filename, 'r') as f:\n",
" current_text = f.read().strip()\n",
" else:\n",
" with open(filename, 'a') as f:\n",
" f.write('from .spells import Spell\\n\\n\\n')\n",
" current_text = ''\n",
" if text.strip() in current_text:\n",
" raise ValueError('Text already exists in the file')\n",
" with open(filename, 'a') as f:\n",
" f.write(text + '\\n')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"for c in string.ascii_lowercase:\n",
" filename = f'dungeonsheets/spells/spells_{c}.py'\n",
" if os.path.isfile(filename):\n",
" os.remove(filename)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 460/460 [00:00<00:00, 2537.91it/s]\n"
]
}
],
"source": [
"for name in tqdm(sorted(spells.keys())):\n",
" save_spell(name, spells[name])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Check no errors on import"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import dungeonsheets"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from dungeonsheets.spells import *"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"from dungeonsheets.make_sheets import create_spellbook_pdf"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"tests = {}\n",
"for k, v in dungeonsheets.spells.__dict__.items():\n",
" if k == 'Spell':\n",
" continue\n",
" if isinstance(v, type) and issubclass(v, dungeonsheets.spells.Spell):\n",
" tests[k] = v()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"test_char = dungeonsheets.Character()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"test_char._spells = test_char._spells_prepared = list(tests.values())"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"create_spellbook_pdf(test_char, 'Entire_Spellbook') # Create a test Entire_Spellbook.pdf to check for printing issues"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}