forked from theShmoo/DSA5RegelWikiParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperties_parser.py
More file actions
206 lines (162 loc) · 6.68 KB
/
properties_parser.py
File metadata and controls
206 lines (162 loc) · 6.68 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
import re
import logging
from bs4 import BeautifulSoup
class PropertiesParser(object):
"""docstring for PropertiesParser"""
def __init__(self, info, spell_class):
super(PropertiesParser, self).__init__()
self.info = info
self.spell_class = spell_class
def filterPropertiesText(self, text, prop_rx):
# filter unnessary html format stuff
replace_operations = [
r"</?div( \w+\=\".+?\")?>",
r"</?span( \w+\=\".+?\")?>",
r"<p>\s*<\/p>",
r"<h1>(.*)</h1>"
]
cleanup_operations = [
r"\\n",
r"\n"
]
for op in replace_operations:
text = re.sub(op, "", text, 0, re.U)
# remove the strongs
str_rx = r"\s*<strong>\s*" + prop_rx + r":?\s*</strong>\s*:?\s*"
text = re.sub(str_rx, r"\1: ", text, 0, re.U)
# parse the Publikation
pub_rx = r"(<p>)?.*Publikation:\s?([\w\d ]+).*(</p>)?"
m = re.search(pub_rx, text, re.U)
if m is not None:
publication = m.group(2)
else:
publication = None
# filter out end keywords
end_keywords = [
r"Zaubererweiterungen",
r"Publikation"
]
end_rx = r"("
for key in end_keywords[:-1]:
end_rx += key + r"|"
end_rx += end_keywords[-1] + r")(.|\n)*"
text = re.sub(end_rx, "", text, 0, re.U)
for op in cleanup_operations:
text = re.sub(op, "", text, 0, re.U)
return (text, publication)
def createPropRegEx(self, properties):
prop_rx = r"("
for key in properties[:-1]:
prop_rx += key + r"|"
prop_rx += properties[-1] + r")\s*"
return prop_rx
def filterProp(self, prop):
prop_operations = [
r"^((<p>)?\s*((</?p>)|(</?br>)))+",
r"((<p>)?\s*((</?p>)|(</?br>)))+$"
]
cleanup_ops = [
r"\\n",
r"\n",
r"<(.*)>"
]
for rx in prop_operations:
prop = re.sub(rx, "", prop, 0, re.U)
# remove last bits of html junk
for clOp in cleanup_ops:
prop = re.sub(clOp, "", prop, 0, re.U)
# remove trailing whitespaces
prop = prop.rstrip()
return prop
def parseByText(self, selector, item):
props = self.info["properties"]
spell_properties = {}
# create a or regex of all properties
prop_rx = self.createPropRegEx(props)
# get the text of the selector
text = selector.extract_first()
# text = unicode(text, "utf-8")
# use unicode fag everywhere! (re.U (Unicode) flag)
# filter unnessary html format stuff
(text, pub) = self.filterPropertiesText(text, prop_rx)
spell_properties["Publikation"] = pub
prop_rx += r": "
# find the existing properties
found_props = re.findall(prop_rx, text, re.U)
# split the text by the properties
found_descr = re.split(prop_rx, text)
first_found = -1
# iterate over the found descriptions
for idx, elem in enumerate(found_descr):
# if the description matches a property
if elem in found_props:
first_found = idx if first_found < 0 else first_found
# save the next description as its descritpion
spell_properties[elem] = self.filterProp(found_descr[idx + 1])
if self.info['pre-text'] is 1:
if first_found > 0:
descr = self.filterProp(found_descr[first_found - 1])
spell_properties['Wirkung'] = descr
else:
logging.warning("Pre Text Error! " + item["name"])
item["properties"] = spell_properties
def parseProperties(self, selector, item):
"""parse the properties of a spell and returns it as a dict"""
self.parseByText(selector, item)
default = self.info["default-verbreitung"]
if default:
item["properties"]["Verbreitung"] = default
# Verbreitung default cases
if self.spell_class == "Zaubertrick":
if "Anmerkung" in item["properties"]:
item["properties"]["Verbreitung"] = item[
"properties"]["Anmerkung"]
item["properties"].pop("Anmerkung", None)
if "Verbreitung" not in item["properties"]:
item["properties"]["Verbreitung"] = "Allgemein"
# Merkmal default cases
if "Merkmal" not in item["properties"]:
item["properties"]["Merkmal"] = "Keines"
def parseAbility(self, selector, item):
"""parse the properties of an ability and returns it as a dict"""
self.parseByText(selector, item)
def parseKarmaExtensions(self, selector, item):
"""parse the karma extensions of a liturgy and returns it as a dict"""
karma_extensions = {}
if self.info['extension'] is 1:
extension_content_query = ".//em[contains(.,'#')]/" + \
"following-sibling::text()[1]"
extension_title_query = ".//em/text()[contains(.,'#')]"
title_selector = selector.xpath(extension_title_query)
content_selector = selector.xpath(extension_content_query)
titles = title_selector.extract()
contents = content_selector.extract()
if len(titles) >= 2 and len(contents) >= 2:
for i in range(0, 3):
karma_extensions[i] = (titles[i][1:], contents[i])
else:
logging.warning("No Spell extensions found!")
item['karmaextensions'] = karma_extensions
def parseSpellExtensions(self, selector, item):
"""parse the spell extensions of a spell and returns it as a dict"""
spell_extensions = {}
if self.info['extension'] is 1:
extension_query = ".//p[contains(.,'#')]"
content_selector = selector.xpath(extension_query)
contents = content_selector.extract()
conLen = len(contents)
if conLen > 0:
for i in range(0, conLen):
soup = BeautifulSoup(contents[i])
spell_extensions[i] = soup.get_text()
else:
logging.warning("No Spell extensions found!")
item['spellextensions'] = spell_extensions
def parseName(self, selector, item):
"""tries to parse the name of spell and returns it as a string"""
name_selector = selector.xpath(".//h1/text()")
name = name_selector.extract_first()
if name is None:
logging.warning("No name found!")
else:
item['name'] = name