Repository navigation
Expand file tree
/
Copy pathmagic_items.py
More file actions
219 lines (185 loc) · 9.79 KB
/
Copy pathmagic_items.py
File metadata and controls
219 lines (185 loc) · 9.79 KB
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
import os
import sys
import shutil
import copy
import re
from bs4 import BeautifulSoup, Tag, NavigableString
from helpers.create_db import create_db
from helpers.mod_helpers import write_db
from helpers.mod_helpers import multi_level
from helpers.mod_helpers import power_construct
from helpers.mod_helpers import powers_format
from helpers.mod_helpers import props_format
if __name__ == '__main__':
alt_reward_expr = re.compile('(Alternative Reward|Battle Scars|Divine Boon|Echo of Power|Elemental gift|Elemental Gift|Fey Magic Gift|Glory Boon|Grandmaster Training|Legendary Boon|Lost Rune|Primal Blessing|Psionic Talent|Secret of the Way|Sorcerer-King\'s Boon|Templar Brand|Veiled Alliance Mystery|Wanderer\'s Secret)')
# Pull data from Portable Compendium
db = []
try:
db = create_db('ddiItem.sql', "','")
except:
print("Error reading data source.")
print(f"{len(db)} entries recovered")
# Convert data to FG format
print("converting to FG format...")
# Initialize all modules databases
export_list = []
if not db:
print("NO DATA FOUND IN SOURCES, MAKE SURE YOU HAVE COPIED YOUR 4E PORTABLE COMPENDIUM DATA TO SOURCES!")
input('Press enter to close.')
sys.exit(0)
for i, row in enumerate(db, start=1):
# Parse the HTML text
html = row['Txt']
html = html.replace('\\r\\n','\r\n').replace('\\','')
parsed_html = BeautifulSoup(html, features="html.parser")
# Retrieve the data with dedicated columns
name_str = row['Name'].replace('\\', '')
category_str = row['Category'].replace('\\', '')
rarity_str = row['Rarity'].replace('\\', '')
bonus_str = ''
class_str = ''
cost_str = ''
critical_str = ''
enhancement_str = ''
flavor_str = ''
group_str = ''
level_str = ''
mitype_str = ''
powers_str = ''
prerequisite_str = ''
properties_str = ''
props_str = ''
special_str = ''
subclass_str = ''
# Class
if (re.search('^(Alchemical Item|Alternative Reward|Ammunition|Arms|Companion|Consumable|Familiar|Feet|Hands|Head|Head and Neck|Neck|Mount|Ring|Waist|Wondrous)$', category_str) and re.search('^(Common|Uncommon|Rare)', rarity_str)):
class_str = rarity_str
mitype_str = 'other'
if mitype_str != '':
print(str(i) + ' ' + name_str)
# Ensure the category_str matches the bold label to avoid false matches e.g. 'Ring'
if re.search('(Arms|Familiar|Feet|Hands|Head|Head and Neck|Neck|Ring|Waist)', category_str):
category_str += ' Slot'
elif category_str == 'Wondrous':
category_str = 'Wondrous Item'
# Bonus / Cost / Level
multi_bonus = True
try:
multi_list = multi_level(parsed_html)
except:
multi_bonus = False
multi_list = []
multi_dict = {}
multi_dict['bonus'] = ''
multi_dict['cost'] = ''
multi_dict['level'] = ''
# Try to find a value for cost
if category_str == 'Alternative Reward':
if cost_tag := parsed_html.find(string=alt_reward_expr):
if cost_lbl := cost_tag.parent.next_sibling:
multi_dict['cost'] = re.search(r'([\d,]*( gp)*$)', cost_lbl).group(1)
if cost_tag := parsed_html.find(string=re.compile('^' + category_str)):
if re.search('^Artifact', cost_tag):
multi_dict['cost'] = ''
elif cost_lbl := cost_tag.parent.next_sibling:
multi_dict['cost'] = re.search(r'([\d,]*( gp)*$)', cost_lbl).group(1)
# Try to find a value for level
if level_lbl := parsed_html.find(id='headerlevel'):
multi_dict['level'] = re.search(r'(\d+)', level_lbl.string).group(1)
# If we find a Tier just set the level to the minimum for that Tier
elif level_lbl := parsed_html.find('span', class_='milevel'):
tier_str = re.search(r'(Heroic|Paragon|Epic)', level_lbl.string).group(1)
if tier_str == 'Heroic':
multi_dict['level'] = '1'
elif tier_str == 'Paragon':
multi_dict['level'] = '11'
elif tier_str == 'Epic':
multi_dict['level'] = '21'
multi_list.append(copy.deepcopy(multi_dict))
# Critical
if critical_lbl := parsed_html.find(string='Critical:'):
critical_str = critical_lbl.parent.next_sibling.get_text(separator = '\\n', strip = True)
# only weapons can have a 'critical' entry
if critical_str != '':
mitype_str = 'weapon'
# Enhancement
if enhancement_lbl := parsed_html.find(string=re.compile('^(Enhancement|Enhancement Bonus):$')):
enhancement_str = enhancement_lbl.parent.next_sibling.get_text(separator = '\\n', strip = True)
# Flavor
if flavor_lbl := parsed_html.find('p', class_='miflavor'):
flavor_str = re.sub('\s\s', ' ', flavor_lbl.get_text(separator = '\\n', strip = True))
# Subclass (category)
# look for a <p> that begins with the category_str
if subclass_cat := parsed_html.find('p', class_='mistat').find(string=re.compile('^' + category_str)):
# if the next tag contains 'Property' then there is nothing to find
if subclass_lbl := subclass_cat.parent.next_sibling:
if subclass_lbl.text == 'Property':
subclass_str = ''
# otherwise try to extract a string that is different to the category_str
else:
subclass_str = re.search(r'^([a-zA-Z \(\)]*)', subclass_lbl).group(1)
subclass_str = '' if subclass_str == category_str else subclass_str
# Subclass (Alternative Reward)
# There is no 'Alternative Reward' label, so check for the Subclass using a regex
if category_str == 'Alternative Reward':
subclass_str = parsed_html.find(string=alt_reward_expr)
subclass_str = re.sub('\s\s', ' ', subclass_str.strip())
# Powers
powers_str = powers_format(parsed_html)
# Prerequisite
if prerequisite_lbl := parsed_html.find(string=re.compile('^(Prerequisite|Requirement):$')):
prerequisite_str = prerequisite_lbl.parent.next_sibling.get_text(separator = '\\n', strip = True)
# Properties
if properties_lbl := parsed_html.find(string=re.compile('^(Property|Properties)')):
properties_str = re.sub('\s\s', ' ', properties_lbl.parent.next_sibling.get_text(separator = '\\n', strip = True))
properties_str = re.sub(r':\\n', ': ', properties_str)
# non-weapon items need a separate 'props' list for the 'Abilities' tab
if mitype_str != 'weapon':
props_str = props_format(properties_str)
properties_str = ''
# Special (Published In)
if special_lbl := parsed_html.find('p', class_='publishedIn'):
special_str = re.sub('\s\s', ' ', special_lbl.text)
# Build the item entry
for item in multi_list:
export_item = {}
export_item['bonus'] = item['bonus']
export_item['class'] = class_str
export_item['cost'] = item['cost']
export_item['critical'] = critical_str
export_item['enhancement'] = enhancement_str
export_item['flavor'] = flavor_str
export_item['group'] = group_str
export_item['level'] = item['level']
export_item['mitype'] = mitype_str
if multi_bonus and item['bonus'] == '':
export_item['name'] = name_str + ' (Level ' + item['level'] + ')'
else:
export_item['name'] = name_str + ' ' + item['bonus'] if item['bonus'] != '' else name_str
export_item['powers'] = powers_str
export_item['prerequisite'] = prerequisite_str
export_item['properties'] = properties_str
export_item['props'] = props_str
export_item['special'] = special_str
export_item['subclass'] = category_str + ': ' + subclass_str if subclass_str != '' else category_str
# Append a copy of generated entry
export_list.append(copy.deepcopy(export_item))
print(str(len(db)) + " entries parsed.")
print(str(len(export_list)) + " export entries.")
# Write FG XML database files
write_id = write_db('export/magic_items/data/db.xml', export_list)
print(str(write_id) + " module entries written. Job done.")
try:
os.remove('export/mods/4E_Magic_Items.mod')
except FileNotFoundError:
print("Cleanup not needed.")
try:
shutil.make_archive('export/mods/4E_Magic_Items', 'zip', 'export/magic_items/data/')
os.rename('export/mods/4E_Magic_Items.zip', 'export/mods/4E_Magic_Items.mod')
print("\nDatabase added and module generated!")
print("You can find it in the 'export\\mods' folder\n")
except Exception as e:
print(f"Error creating zipped .mod file:\n{e}")
print("\nManually zip the contents of the 'export\\magic_item\\data' folder to create the mod.")
print("Rename the complete filename (including path) to '4E_Magic_Items.mod'.\n")
input('Press enter to close.')