forked from jimbarnesrtp/pf2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildCompanions.py
217 lines (198 loc) · 8.03 KB
/
buildCompanions.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
from bs4 import BeautifulSoup
import requests
import json
import datetime
import codecs
import time
compHolder = {}
compHolder['name'] = 'Pathfinder 2.0 Companion Animal list'
compHolder['date'] = datetime.date.today().strftime("%B %d, %Y")
def get_multi(link):
items = []
string = " "
res2 = requests.get(link)
res2.raise_for_status()
soup2 = BeautifulSoup(res2.text, 'lxml')
main = soup2.find("span", {'id':'ctl00_MainContent_DetailedOutput'})
children = main.contents
reachedBreak = False
reachedItem = False
detailHolder = []
notFirstH1 = False
inHeader = False
item = {}
item['link'] = link
tagType = ""
itemDetailHolder = []
inSpecial = False
specialHolder = {}
inAttack = False
attackHolder = {}
for child in children:
stringContents = str(child)
if stringContents.startswith("<"):
if child.name == "hr":
if inSpecial:
tagType = ""
if child.name == "img":
if inSpecial:
specialHolder['actions'] = child['alt']
elif inAttack:
attackHolder['actions'] = child['alt']
else:
item['actions'] = child['alt']
if child.name == "span":
if inSpecial:
if 'traits' not in specialHolder:
specialHolder['traits'] = []
specialHolder['traits'].append(child.text)
if child.name == "h1":
if notFirstH1:
item['special'] = specialHolder
item['attacks'].append(attackHolder)
item['text'] = string.join(detailHolder) + string.join(itemDetailHolder)
items.append(item)
item = {}
item['link'] = link
item['attacks'] = []
itemDetailHolder = []
inSpecial = False
specialHolder = {}
else:
notFirstH1 = True
reachedItem = True
name = child.text
item['name'] = child.text[0:].strip()
print(item['name'])
if child.name == "h3":
inSpecial = True
specialHolder['name'] = child.text[0:].strip()
tagType = ""
if child.name == "b":
if(child.text != "Source"):
tagType = child.text.lower().replace(" ", "")
if tagType == "melee":
if 'damage' in attackHolder:
if 'attacks' not in item:
item['attacks'] = []
item['attacks'].append(attackHolder)
attackHolder = {}
inAttack = True
elif tagType == "damage":
inAttack = True
else:
inAttack = False
if child.name == "u":
if inSpecial:
if tagType != "":
if tagType in specialHolder:
specialHolder[tagType] += child.text
else:
specialHolder[tagType] = child.text
else:
if "text" in specialHolder:
specialHolder['text'] += child.text
else:
specialHolder['text'] = child.text
elif inAttack:
attackHolder['text'] += child.text
else:
if tagType in item:
item[tagType] += child.text
else:
item[tagType] = child.text
if child.name == "li":
if inSpecial:
if tagType != "":
if tagType in specialHolder:
specialHolder[tagType] += child.text
else:
specialHolder[tagType] = child.text
else:
if 'text' in specialHolder:
specialHolder['text'] += child.text
else:
specialHolder['text'] = child.text
if child.name == "a":
try:
if child['class'][0] == "external-link" :
item['source'] = child.text
except:
if inSpecial:
if tagType != "":
if tagType in specialHolder:
specialHolder[tagType] += child.text
else:
specialHolder[tagType] = child.text
else:
if 'text' in specialHolder:
specialHolder['text'] += child.text
else:
specialHolder['text'] = child.text
elif inAttack:
attackHolder['text'] += child.text
else:
if tagType in item:
item[tagType] += child.text
else:
item[tagType] = child.text
if child.name == "i":
if inSpecial:
if tagType in item:
item[tagType] += child.text
else:
item[tagType] = child.text
else:
if tagType in specialHolder:
specialHolder[tagType] += child.text
else:
specialHolder[tagType] = child.text
else:
if notFirstH1:
if inAttack:
#print(stringContents)
if tagType == "melee":
if 'text' not in attackHolder:
attackHolder['text'] = ""
attackHolder['text'] += stringContents.strip()
elif tagType == "damage":
attackHolder['damage'] = stringContents.strip()
elif inSpecial:
if tagType != "":
if tagType in specialHolder:
specialHolder[tagType] += stringContents.strip()
else:
specialHolder[tagType] = stringContents.strip()
else:
if "text" in specialHolder:
specialHolder['text'] += stringContents.strip()
else:
specialHolder['text'] = stringContents.strip()
else:
if not inAttack:
if tagType == "level":
item['level'] = int(stringContents.replace(";","").strip())
elif tagType != "":
if tagType in item:
item[tagType] += stringContents.strip()
else:
item[tagType] = stringContents.strip()
else:
if not stringContents.isspace():
itemDetailHolder.append(stringContents.strip())
##tagType = ""
item['attacks'].append(attackHolder)
item['special'] = specialHolder
item['text'] = string.join(detailHolder + itemDetailHolder)
items.append(item)
return items
def get_all():
compHolder['companions'] = get_multi("https://2e.aonprd.com/AnimalCompanions.aspx")
return compHolder
#print(get_all())
json_data = json.dumps(get_all(), indent=4)
#print(json_data)
filename = "companions-pf2.json"
f = open(filename, "w")
f.write(json_data)
f.close