Skip to content

Commit a505b01

Browse files
committed
Added script to parse monster from a dnd website
1 parent 525da02 commit a505b01

File tree

1 file changed

+276
-0
lines changed

1 file changed

+276
-0
lines changed

openpnp/monster/scripts.py

+276
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
import re
2+
3+
import requests
4+
from bs4 import BeautifulSoup
5+
from monster.models import *
6+
7+
root_url = "https://dnd-5e.herokuapp.com/monsters/"
8+
9+
10+
def main():
11+
html = requests.get(root_url).text
12+
parsed_html = BeautifulSoup(html, features="html.parser")
13+
list_entries_html = parsed_html.find("ul", attrs={"id": "monsters"}).find_all("a")
14+
list_entries = [entry["href"] for entry in list_entries_html]
15+
for entry in list_entries:
16+
monster_html = requests.get(root_url + entry).text
17+
monster_parsed_html = BeautifulSoup(monster_html, features="html.parser")
18+
monster_box = monster_parsed_html.find("div", attrs={"class": "monster-box"})
19+
m_name = monster_box.find("h1").text
20+
if len(Monster.objects.filter(name=m_name)) == 0:
21+
m = Monster()
22+
m.name = monster_box.find("h1").text
23+
m.save()
24+
else:
25+
m = Monster.objects.get(name=m_name)
26+
print(m.name)
27+
p_list = monster_box.find_all("p")
28+
29+
size_tmp = p_list[0].text.split(" ")[0].lower()
30+
m.size = Size.objects.get(size=size_tmp)
31+
32+
race_temp = " ".join(p_list[0].text.split(" ")[1:]).split(",")[0]
33+
if "(" in race_temp:
34+
race_name = race_temp.split(" ")[0]
35+
race_comment = "".join(race_temp.split(" ")[-1][1:])
36+
if race_comment.endswith(")"):
37+
race_comment = race_comment[:-1]
38+
if len(Race.objects.filter(race=race_name, comment=race_comment)) == 0:
39+
r = Race()
40+
r.race = race_name
41+
r.comment = race_comment
42+
r.save()
43+
m.race = Race.objects.get(race=race_name, comment=race_comment)
44+
else:
45+
if len(Race.objects.filter(race=race_temp, comment="")) == 0:
46+
r = Race()
47+
r.race = race_temp
48+
r.save()
49+
m.race = Race.objects.get(race=race_temp, comment="")
50+
51+
al = p_list[0].text.split(",")[-1].lower().strip()
52+
if len(Alignment.objects.filter(alignment=al)) == 0:
53+
a = Alignment()
54+
a.alignment = al
55+
a.save()
56+
m.alignment = Alignment.objects.get(alignment=al)
57+
m.armor_class = int(p_list[1].text.split(" ")[2:][0])
58+
m.armor_description = " ".join(p_list[1].text.split(" ")[3:]).strip("(").rstrip(")")
59+
m.hit_points = int(p_list[2].text.split(" ")[2:][0])
60+
m.hit_points_alt = " ".join(p_list[2].text.split(" ")[3:]).split("(")[1].rstrip(")")
61+
m.speed_base = " ".join(p_list[3].text.split(" ")[1:]).split(",")[0].split(" ")[0]
62+
if len(" ".join(p_list[3].text.split(" ")[1:]).split(",")) > 2:
63+
m.speed_alt = ", ".join(" ".join(p_list[3].text.split(" ")[1:]).split(", ")[1:])
64+
65+
li_list = monster_box.find_all("li")
66+
m.strength = li_list[0].text.split(" ")[1]
67+
# m.strength_mod = int(li_list[0].text.split(" ")[2][1:-1])
68+
m.dexterity = li_list[1].text.split(" ")[1]
69+
# m.dexterity_mod = int(li_list[1].text.split(" ")[2][1:-1])
70+
m.constitution = li_list[2].text.split(" ")[1]
71+
# m.constitution_mod = int(li_list[2].text.split(" ")[2][1:-1])
72+
m.intelligence = li_list[3].text.split(" ")[1]
73+
# m.intelligence_mod = int(li_list[3].text.split(" ")[2][1:-1])
74+
m.wisdom = li_list[4].text.split(" ")[1]
75+
# m.wisdom_mod = int(li_list[4].text.split(" ")[2][1:-1])
76+
m.charisma = li_list[5].text.split(" ")[1]
77+
# m.charisma_mod = int(li_list[5].text.split(" ")[2][1:-1])
78+
79+
for p_element in p_list[4:]:
80+
if p_element.text.startswith("Saving Throws"):
81+
tmp = " ".join(p_element.text.split(" ")[2:])
82+
if "," in tmp:
83+
tmp_list = tmp.split(", ")
84+
for x in [item.split(" ") for item in tmp_list]:
85+
if len(SavingThrow.objects.filter(attribute=x[0], value=int(x[1]))) == 0:
86+
st = SavingThrow()
87+
st.attribute = x[0]
88+
st.value = int(x[1])
89+
st.save()
90+
m.saving_throws.add(SavingThrow.objects.get(attribute=x[0], value=int(x[1])))
91+
else:
92+
if len(SavingThrow.objects.filter(attribute=tmp.split(" ")[0], value=int(tmp.split(" ")[1]))) == 0:
93+
st = SavingThrow()
94+
st.attribute = tmp.split(" ")[0]
95+
st.value = int(tmp.split(" ")[1])
96+
st.save()
97+
m.saving_throws.add(SavingThrow.objects.get(attribute=tmp.split(" ")[0], value=int(tmp.split(" ")[1])))
98+
99+
elif p_element.text.startswith("Senses"):
100+
tmp = " ".join(p_element.text.split(" ")[1:])
101+
if "," in tmp:
102+
for x in tmp.split(","):
103+
if len(Sense.objects.filter(sense=x)) == 0:
104+
s = Sense()
105+
s.sense = x
106+
s.save()
107+
m.senses.add(Sense.objects.get(sense=x))
108+
else:
109+
if len(Sense.objects.filter(sense=tmp)) == 0:
110+
s = Sense()
111+
s.sense = tmp
112+
s.save()
113+
m.senses.add(Sense.objects.get(sense=tmp))
114+
elif p_element.text.startswith("Languages"):
115+
tmp = " ".join(p_element.text.split(" ")[1:])
116+
if "," in tmp:
117+
for language in tmp.split(","):
118+
if len(Language.objects.filter(language=language)) == 0:
119+
l = Language()
120+
l.language = language
121+
l.save()
122+
m.languages.add(l)
123+
else:
124+
m.languages.add(Language.objects.get(language=language))
125+
else:
126+
if tmp != "-":
127+
if len(Language.objects.filter(language=tmp)) == 0:
128+
l = Language()
129+
l.language = tmp
130+
l.save()
131+
m.languages.add(Language.objects.get(language=tmp))
132+
elif p_element.text.startswith("Challenge"):
133+
challenge_tmp = p_element.text.split(" ")[1:]
134+
if challenge_tmp[0] == "1/8":
135+
challenge_rating = 0.125
136+
elif challenge_tmp[0] == "1/4":
137+
challenge_rating = 0.25
138+
elif challenge_tmp[0] == "1/2":
139+
challenge_rating = 0.5
140+
else:
141+
challenge_rating = challenge_tmp[0]
142+
m.challenge = float(challenge_rating)
143+
m.challenge_xp = int(challenge_tmp[1].strip("(").replace(",", ""))
144+
elif p_element.text.startswith("Skills"):
145+
tmp = " ".join(p_element.text.split(" ")[1:])
146+
if "," in tmp:
147+
for x in [re.split(r"(?!\w)\s[+]", item) for item in tmp.split(", ")]:
148+
if len(Skill.objects.filter(skill=x[0], value=int(x[1]))) == 0:
149+
s = Skill()
150+
s.skill = x[0]
151+
s.value = int(x[1])
152+
s.save()
153+
m.skills.add(Skill.objects.get(skill=x[0], value=int(x[1])))
154+
else:
155+
if len(Skill.objects.filter(skill=tmp.split(" ")[0], value=int(tmp.split(" ")[1]))) == 0:
156+
s = Skill()
157+
s.skill = tmp.split(" ")[0]
158+
s.value = int(tmp.split(" ")[1])
159+
s.save()
160+
m.skills.add(Skill.objects.get(skill=tmp.split(" ")[0], value=int(tmp.split(" ")[1])))
161+
elif p_element.text.startswith("Damage Immunities"):
162+
tmp = " ".join(p_element.text.split(" ")[2:]).lower()
163+
if tmp.lower().startswith("one of the following: "):
164+
tmp = "".join(tmp[22:])
165+
if ";" in tmp:
166+
x = tmp.split("; ")
167+
if len(DamageImmunity.objects.filter(damage_immunity=x[1])) == 0:
168+
di = DamageImmunity()
169+
di.damage_immunity = x[1].lower()
170+
di.save()
171+
m.damage_immunities.add(DamageImmunity.objects.get(damage_immunity=x[1]))
172+
for y in x[0].split(", "):
173+
if y.startswith("or "):
174+
y = "".join(y[3:])
175+
if y.startswith("and "):
176+
y = "".join(y[4:])
177+
if len(DamageImmunity.objects.filter(damage_immunity=y)) == 0:
178+
di = DamageImmunity()
179+
di.damage_immunity = y.lower()
180+
di.save()
181+
m.damage_immunities.add(DamageImmunity.objects.get(damage_immunity=y))
182+
elif "," in tmp:
183+
for x in tmp.split(", "):
184+
if x.startswith("or "):
185+
x = "".join(x[3:])
186+
if x.startswith("and "):
187+
x = "".join(x[4:])
188+
if len(DamageImmunity.objects.filter(damage_immunity=x)) == 0:
189+
di = DamageImmunity()
190+
di.damage_immunity = x.lower()
191+
di.save()
192+
m.damage_immunities.add(DamageImmunity.objects.get(damage_immunity=x))
193+
else:
194+
if len(DamageImmunity.objects.filter(damage_immunity=tmp)) == 0:
195+
di = DamageImmunity()
196+
di.damage_immunity = tmp.lower()
197+
di.save()
198+
m.damage_immunities.add(DamageImmunity.objects.get(damage_immunity=tmp))
199+
elif p_element.text.startswith("Damage Resistances"):
200+
tmp = " ".join(p_element.text.split(" ")[2:]).lower()
201+
if tmp.lower().startswith("one of the following: "):
202+
tmp = "".join(tmp[22:])
203+
if ";" in tmp:
204+
x = tmp.split("; ")
205+
if len(DamageResistance.objects.filter(damage_resistance=x[1])) == 0:
206+
di = DamageResistance()
207+
di.damage_resistance = x[1].lower()
208+
di.save()
209+
m.damage_resistances.add(DamageResistance.objects.get(damage_resistance=x[1]))
210+
for y in x[0].split(", "):
211+
if y.startswith("or "):
212+
y = "".join(y[3:])
213+
if y.startswith("and "):
214+
y = "".join(y[4:])
215+
if len(DamageResistance.objects.filter(damage_resistance=y)) == 0:
216+
di = DamageResistance()
217+
di.damage_resistance = y.lower()
218+
di.save()
219+
m.damage_resistances.add(DamageResistance.objects.get(damage_resistance=y))
220+
elif "," in tmp:
221+
for x in tmp.split(", "):
222+
if x.startswith("or "):
223+
x = "".join(x[3:])
224+
if x.startswith("and "):
225+
x = "".join(x[4:])
226+
if len(DamageResistance.objects.filter(damage_resistance=x)) == 0:
227+
di = DamageResistance()
228+
di.damage_resistance = x.lower()
229+
di.save()
230+
m.damage_resistances.add(DamageResistance.objects.get(damage_resistance=x))
231+
else:
232+
if len(DamageResistance.objects.filter(damage_resistance=tmp)) == 0:
233+
di = DamageResistance()
234+
di.damage_resistance = tmp.lower()
235+
di.save()
236+
m.damage_resistances.add(DamageResistance.objects.get(damage_resistance=tmp))
237+
elif p_element.text.startswith("Condition Immunities"):
238+
tmp = " ".join(p_element.text.split(" ")[2:]).lower()
239+
if tmp.lower().startswith("one of the following: "):
240+
tmp = "".join(tmp[22:])
241+
if ";" in tmp:
242+
x = tmp.split("; ")
243+
if len(ConditionImmunity.objects.filter(condition_immunity=x[1])) == 0:
244+
di = ConditionImmunity()
245+
di.condition_immunity = x[1].lower()
246+
di.save()
247+
m.condition_immunities.add(ConditionImmunity.objects.get(condition_immunity=x[1]))
248+
for y in x[0].split(", "):
249+
if y.startswith("or "):
250+
y = "".join(y[3:])
251+
if y.startswith("and "):
252+
y = "".join(y[4:])
253+
if len(ConditionImmunity.objects.filter(condition_immunity=y)) == 0:
254+
di = ConditionImmunity()
255+
di.condition_immunity = y.lower()
256+
di.save()
257+
m.condition_immunities.add(ConditionImmunity.objects.get(condition_immunity=y))
258+
elif "," in tmp:
259+
for x in tmp.split(", "):
260+
if x.startswith("or "):
261+
x = "".join(x[3:])
262+
if x.startswith("and "):
263+
x = "".join(x[4:])
264+
if len(ConditionImmunity.objects.filter(condition_immunity=x)) == 0:
265+
di = ConditionImmunity()
266+
di.condition_immunity = x.lower()
267+
di.save()
268+
m.condition_immunities.add(ConditionImmunity.objects.get(condition_immunity=x))
269+
else:
270+
if len(ConditionImmunity.objects.filter(condition_immunity=tmp)) == 0:
271+
di = ConditionImmunity()
272+
di.condition_immunity = tmp.lower()
273+
di.save()
274+
m.condition_immunities.add(ConditionImmunity.objects.get(condition_immunity=tmp))
275+
m.tag = Tag.objects.get(tag="monster")
276+
m.save()

0 commit comments

Comments
 (0)