-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbuildFeats.py
122 lines (104 loc) · 3.99 KB
/
buildFeats.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
from bs4 import BeautifulSoup
import requests
import json
import datetime
import csv
import os
from pf2helpers import Pf2Helpers
feat_holder = {}
feat_holder['name'] = 'Pathfinder 2.0 feat list v3'
feat_holder['date'] = datetime.date.today().strftime("%B %d, %Y")
headers = {
'User-Agent': 'PF2 data to rest builder',
'From': 'jimbarnesrtp' # This is another valid field
}
class BuildFeatsV2:
#"Name","PFS","Source","Rarity","Traits","Level","Prerequisites","Benefits","Spoilers?"
pf = Pf2Helpers()
def normalize_feat_data(self, data_list):
norm_feats = []
link_list = ['name','source', 'rarity', 'family', 'type']
for data in data_list:
keys = list(data.keys())
new_data = {}
for key in keys:
if key in link_list:
new_data[key] = self.pf.norm_link(data[key])
elif key == 'pfs':
new_data[key] = self.pf.norm_pfs(data[key])
elif key == 'prerequisites':
new_data[key] = self.pf.norm_prereqs(data[key])
elif key == 'level':
new_data[key] = int(data[key])
elif key == 'traits':
new_data[key] = self.pf.norm_multi(data[key])
else:
new_data[key] = data[key]
new_data['link'] = self.pf.norm_url(data['name'])
new_data['text'] = self.get_details(new_data['link'])
norm_feats.append(new_data)
return norm_feats
def build_feats(self):
all_feats = []
cur_path = os.getcwd()
i = 1
while i < 7:
file_name = cur_path+"/featscsv/RadGridExport-%s.csv" % i
raw_feats = self.pf.load_csv(file_name)
all_feats.extend(self.normalize_feat_data(raw_feats))
i += 1
return all_feats
def save_feats(self, feats):
feat_holder['baseFeats'] = feats
json_data = json.dumps(feat_holder, indent=4)
# print(json_data)
filename = "json/feats-pf2-v3.json"
f = open(filename, "w")
f.write(json_data)
f.close
def get_details(self, link):
res = requests.get(link)
res.raise_for_status()
soup = BeautifulSoup(res.text, 'html5lib')
detail = soup.find("span", {'id':'ctl00_RadDrawer1_Content_MainContent_DetailedOutput'})
imgs = detail.find_all("img")
#print(detail.contents)
children = detail.contents
reached_break = False
detail_holder = []
details = {}
for child in children:
string_contents = str(child)
if string_contents.startswith("<"):
if string_contents == "<hr/>":
reached_break = True
if reached_break:
if child.name == "a":
detail_holder.append(child.text)
if child.name == "ul":
#print(child.text)
children3 = child.contents
for child3 in children3:
string_contents3 = str(child3)
if string_contents3.startswith("<") and child3.name == "li":
detail_holder.append(child3.text)
if child.name == "h2":
break
else:
if reached_break:
detail_holder.append(string_contents)
string = " "
details['text'] = string.join(detail_holder)
action_list = ['Reaction', 'Single Action', 'Two Actions', 'Three Actions']
for img in imgs:
if img['alt'] in action_list:
details['action_cost'] = img['alt']
else:
if "action_cost" not in details:
details['action_cost'] = "-"
return details
def main():
bf = BuildFeatsV2()
bf.save_feats(bf.build_feats())
if __name__ == "__main__":
main()