From b06f42717fb1293efe829fa4ec079f9cac3bf1db Mon Sep 17 00:00:00 2001 From: viktor Date: Sat, 26 Apr 2025 01:33:01 -0300 Subject: [PATCH] Add spell lookup feature and fix savename option --- services/spell_lookup_service.py | 43 ++++++++++++++++++++++++++++++++ writer.py | 7 ++++++ 2 files changed, 50 insertions(+) create mode 100644 services/spell_lookup_service.py diff --git a/services/spell_lookup_service.py b/services/spell_lookup_service.py new file mode 100644 index 0000000..8d6366c --- /dev/null +++ b/services/spell_lookup_service.py @@ -0,0 +1,43 @@ +import json +import requests + +class SpellLookupService: + @staticmethod + def lookup(spell_name): + try: + with open('spells.json', 'r') as file: + data = json.load(file) + except FileNotFoundError: + print("spells.json file not found.") + return + + spell_name = spell_name.lower() + + spells = data.get('results', []) + + for spell in spells: + if spell.get('name', '').lower() == spell_name: + print(f"\nSpell Found!\n") + print(f"Name: {spell.get('name', 'Unknown')}") + print(f"Index: {spell.get('index', 'Unknown')}") + print(f"URL: {spell.get('url', 'Unknown')}") + + full_url = f"https://www.dnd5eapi.co{spell.get('url')}" + try: + response = requests.get(full_url) + if response.status_code == 200: + full_spell_data = response.json() + print(f"\n--- Full Spell Details ---") + print(f"Description: {full_spell_data.get('desc', ['No description'])[0]}") + print(f"Level: {full_spell_data.get('level', 'Unknown')}") + print(f"Casting Time: {full_spell_data.get('casting_time', 'Unknown')}") + print(f"Duration: {full_spell_data.get('duration', 'Unknown')}") + print(f"Range: {full_spell_data.get('range', 'Unknown')}") + else: + print("Failed to fetch full spell details (API error).") + except requests.RequestException: + print("Network error while trying to fetch full spell details.") + + return + + print("Spell not found.\n") diff --git a/writer.py b/writer.py index 0447f9f..c30265a 100644 --- a/writer.py +++ b/writer.py @@ -5,6 +5,7 @@ import matplotlib.pyplot as plt import os from tqdm.auto import tqdm +from services.spell_lookup_service import SpellLookupService cmap = plt.get_cmap('viridis') #---------Functions for creating unique binary numbers------ @@ -310,7 +311,12 @@ def draw_attribute(level = None,rang = None, area = None, parser.add_argument("--legend",help = "bool to print legend or not (0 = False,1 = True)") parser.add_argument("--breakdown",help = "bool to control whether to breakdown the lines with colour") parser.add_argument("-ah", "--arg_help",help = "Prints the available options for the chosen attributes",action=argparse.BooleanOptionalAction) + parser.add_argument("--lookup", help="Lookup a spell from spells.json") args = parser.parse_args() + if args.lookup: + SpellLookupService.lookup(args.lookup) + exit() + if args.arg_help: if args.range: @@ -375,6 +381,7 @@ def draw_attribute(level = None,rang = None, area = None, else: school = args.school + savename = args.savename draw_spell(level,rang,area,dtype,school,title = title,legend = legend, base_fn = bases.polygon,shape_fn = line_shapes.straight, breakdown = breakdown,savename = savename)