-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbuildDragonsTest.py
363 lines (310 loc) · 14 KB
/
buildDragonsTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from bs4 import BeautifulSoup
from bs4 import NavigableString
import requests
import json
import datetime
import re
link = 'http://pf2.d20pfsrd.com/monster/silver-dragon/'
res = requests.get(link)
res.raise_for_status()
soup = BeautifulSoup(res.text, 'lxml')
monster = soup.find_all("div", {'class':'article-content'})
children = monster[0].findChildren(recursive=False)
monsters = {}
monsterIndex = 0
lastSpellName = ""
isFirstLine = True
aboutHolder = []
aboutText = False
dragonType = ""
for child in children:
isLineProcessed = False
#print(child.name)
tagName = child.name
if tagName == "h4":
classHolder = child.get("class")
print("in here:", child.text)
if classHolder is None:
text = child.text
if text.startswith("Young G"):
dragonType = "Young Gold Dragon"
elif text.startswith("Adult G"):
dragonType = "Adult Gold Dragon"
elif text.startswith("Ancient Gold"):
dragonType = "Ancient Gold Dragon"
continue
if 'monsters' not in monsters:
monsters['monsters'] = []
else:
monsterIndex += 1
monsters['monsters'].append({})
monsters['monsters'][monsterIndex]['name'] = child.contents[0]
monsters['monsters'][monsterIndex]['level'] = child.find("span", {'class':'monster-level'}).text
monsters['monsters'][monsterIndex]['link'] = link
monsters['monsters'][monsterIndex]['type'] = child.find("span", {'class':'monster-type'}).text
monsters['monsters'][monsterIndex]['monsterText'] = []
monsters['monsters'][monsterIndex]['specials'] = []
isLineProcessed = True
if tagName == "div":
print("checking for special")
try:
pClass = child['class'][0]
#print(pClass)
except:
pClass = ""
print("specialCheck:", pClass)
if pClass == "special-abilities":
specialScratch = child.findChildren(recursive=False)
if len(specialScratch) > 0:
print("in here")
for scratch in specialScratch:
monsters['monsters'][monsterIndex]['specials'].append(scratch.text)
if tagName == "p":
text = child.text
if text == 'About':
aboutText = True
try:
pClass = child['class'][0]
#print(pClass)
except:
pClass = ""
if pClass == "traits":
traitsScratch = child.find_all("span", {'class':'trait'})
traits = []
if traitsScratch is not None:
if(len(traitsScratch) < 2):
monsters['monsters'][monsterIndex]['monsterText'].append(traitsScratch[0].text)
else:
monsters['monsters'][monsterIndex]['alignment'] = child.find("span", {'class':'alignment'}).text
monsters['monsters'][monsterIndex]['size'] = child.find("span", {'class':'size'}).text
frequency = child.find("span", {'class':'frequency'})
if frequency is not None:
monsters['monsters'][monsterIndex]['frequency'] = frequency.text
for trait in traitsScratch:
traits.append(trait.text)
monsters['monsters'][monsterIndex]['traits'] = traits
print(monsters['monsters'][monsterIndex]['name'])
isLineProcessed = True
if "Senses" in text:
start = text.find("Senses") + 7
monsters['monsters'][monsterIndex]['senses'] = re.split('; |, ',text[start:])
isLineProcessed = True
if "Languages" in text:
start = text.find("Languages") + 10
monsters['monsters'][monsterIndex]['languages'] = re.split('; |, ',text[start:])
isLineProcessed = True
if "Skills" in text:
start = text.find("Skills") + 7
monsters['monsters'][monsterIndex]['skills'] = re.split('; |, ',text[start:])
isLineProcessed = True
if text.startswith("Str"):
monsters['monsters'][monsterIndex]['abilities'] = text.split(',')
isLineProcessed = True
if "Items" in text:
start = text.find("Items") + 6
end = text.find(";", start)
endAC = text.find("AC", start)
if end < 0:
if not endAC < 0:
end = AC
else:
if endAC < end:
end = endAC
if end < 0 :
monsters['monsters'][monsterIndex]['items'] = text[start:].split(',')
else:
monsters['monsters'][monsterIndex]['items'] = text[start:end].split(',')
isLineProcessed = True
if "AC" in text:
start = text.find("AC") + 3
end = text.find(";", start)
if end < start:
end = text.find("Fort", start)
monsters['monsters'][monsterIndex]['ac'] = text[start:end]
isLineProcessed = True
if "Fort " in text:
start = text.find("Fort")
end = text.find(";", start)
if end < 0 :
monsters['monsters'][monsterIndex]['saves'] = text[start:].split(",")
else:
monsters['monsters'][monsterIndex]['saves'] = text[start:end].split(",")
isLineProcessed = True
if "Will +" in text:
start = text.find("Will +") + 7
end = text.find(";", start) + 2
if end < start:
print("no extra saves")
else:
monsters['monsters'][monsterIndex]['saves'].append(text[end:])
isLineProcessed = True
if "HP" in text:
if 'hp' not in monsters['monsters'][monsterIndex]:
start = text.find("HP") + 3
end = text.find(";", start)
if end < 0 :
monsters['monsters'][monsterIndex]['hp'] = text[start:].split(',')
else:
monsters['monsters'][monsterIndex]['hp'] = text[start:end].split(',')
isLineProcessed = True
if "Immunities" in text:
start = text.find("Immunities") + 11
end = text.find(";", start)
if end < 0 :
monsters['monsters'][monsterIndex]['immunities'] = text[start:].split(',')
else:
monsters['monsters'][monsterIndex]['immunities'] = text[start:end].split(',')
isLineProcessed = True
if "Weaknesses" in text:
start = text.find("Weaknesses") + 11
end = text.find(";", start)
endResistences = text.find("Resistances", start)
if end < 0:
if not endResistences < 0:
end = endResistences
else:
if endResistences < end:
end = endResistences
if end < 0 :
monsters['monsters'][monsterIndex]['weaknesses'] = text[start:].split(',')
else:
monsters['monsters'][monsterIndex]['weaknesses'] = text[start:end].split(',')
isLineProcessed = True
if "Resistances" in text:
start = text.find("Resistances") + 11
end = text.find(";", start)
if end < 0 :
monsters['monsters'][monsterIndex]['resistances'] = text[start:].split(',')
else:
monsters['monsters'][monsterIndex]['resistances'] = text[start:end].split(',')
isLineProcessed = True
if "Speed" in text:
if 'speed' not in monsters['monsters'][monsterIndex]:
start = text.find("Speed") + 6
end = text.find(";", start)
if end < 0 :
monsters['monsters'][monsterIndex]['speed'] = text[start:].split(',')
else:
monsters['monsters'][monsterIndex]['speed'] = text[start:end].split(',')
isLineProcessed = True
if text.startswith("Melee"):
if 'actions' not in monsters['monsters'][monsterIndex]:
actionHolder = []
actionHolder.append(text)
monsters['monsters'][monsterIndex]['actions'] = actionHolder
else:
monsters['monsters'][monsterIndex]['actions'].append(text)
isLineProcessed = True
if text.startswith("Ranged"):
if 'actions' not in monsters['monsters'][monsterIndex]:
actionHolder = []
actionHolder.append(text)
monsters['monsters'][monsterIndex]['actions'] = actionHolder
else:
monsters['monsters'][monsterIndex]['actions'].append(text)
isLineProcessed = True
if text.startswith("Breath Weapon"):
if 'breathWeapon' not in monsters['monsters'][monsterIndex]:
actionHolder = []
actionHolder.append(text)
monsters['monsters'][monsterIndex]['breathWeapon'] = actionHolder
else:
monsters['monsters'][monsterIndex]['breathWeapon'].append(text)
isLineProcessed = True
if (text.startswith("Divine Innate Spells")) or (text.startswith("Arcane Innate Spells")):
spells = {}
start = text.find("Innate Spells") + 13
end = text.find(";",start)
if end < 0 :
spellAttack = text[start:].split(",")
else:
spellAttack = text[start:end].split(",")
spells['dc'] = spellAttack[0][3:]
if len(spellAttack) > 1:
spells['attack'] = spellAttack[1][7:]
spells['spells'] = text[end+1:].split(";")
monsters['monsters'][monsterIndex]['innateSpells'] = spells
lastSpellName = 'innateSpells'
isLineProcessed = True
if (text.startswith("Occult Spontaneous Spells")) or (text.startswith("Divine Spontaneous Spells")) or (text.startswith("Arcane Spontaneous Spells")):
spells = {}
start = text.find("Spontaneous Spells") + 19
end = text.find(";",start)
if end < 0 :
spellAttack = text[start:].split(",")
else:
spellAttack = text[start:end].split(",")
spells['dc'] = spellAttack[0][3:]
if len(spellAttack) > 1:
spells['attack'] = spellAttack[1][7:]
if "Note:" in text:
spells['note'] = spellAttack[2][5:]
else:
spells['spells'] = text[end+1:].split(";")
monsters['monsters'][monsterIndex]['spontaneousSpells'] = spells
lastSpellName = 'spontaneousSpells'
isLineProcessed = True
if (text.startswith("Primal Prepared Spells")) or (text.startswith("Arcane Prepared Spells")) or (text.startswith("Divine Prepared Spells")) :
spells = {}
start = text.find("Prepared Spells") + 16
end = text.find(";",start)
if end < 0 :
spellAttack = text[start:].split(",")
else:
spellAttack = text[start:end].split(",")
if "" != dragonType:
position = 0
for dragon in monsters['monsters']:
if dragonType == dragon['name']:
monsterIndex = position
print("monsterIndex:", monsterIndex)
else:
position += 1
spells['dc'] = spellAttack[0][3:]
if len(spellAttack) > 1:
spells['attack'] = spellAttack[1][7:]
if "Note" in text:
spells['note'] = text[end+6:]
else:
if "dragon, plus" in text:
start = text.find("dragon, plus") + 13
print("in here 2", dragonType)
previousIndex = monsterIndex - 1
print (monsterIndex)
listHolder = text[start:].split(";") + monsters['monsters'][previousIndex]['preparedSpells']['spells']
spells['spells'] = listHolder
else:
spells['spells'] = text[end+1:].split(";")
monsters['monsters'][monsterIndex]['preparedSpells'] = spells
lastSpellName = 'preparedSpells'
isLineProcessed = True
if not isLineProcessed:
x = re.search("\A\d+(th|st|rd|nd)", text)
if x:
monsters['monsters'][monsterIndex][lastSpellName]['spells'] = text.split(";")
isLineProcessed = True
if "dragon, plus" in text:
start = text.find("dragon, plus") + 13
previousIndex = monsterIndex - 1
listHolder = text[start:].split(";") + monsters['monsters'][previousIndex][lastSpellName]['spells']
monsters['monsters'][monsterIndex][lastSpellName]['spells'] = listHolder
if not isLineProcessed:
if aboutText:
aboutHolder.append(text)
else:
try:
#print(para.parent['class'])
parentClass = para.parent['class'][0]
except:
#print("no Parent")
parentClass = ""
if "special-abilities" != parentClass:
#print(text)
monsters['monsters'][monsterIndex]['monsterText'].append(child.text)
else:
print("skipped")
for monster in monsters['monsters']:
listHolder = monster['monsterText'] + aboutHolder
monster['monsterText'] = listHolder
json_data = json.dumps(monsters, indent=4)
print(json_data)