-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
302 lines (259 loc) · 8.74 KB
/
utils.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
import configparser
import datetime
import urllib.error
import xml.etree.ElementTree as xml
from os.path import exists, isdir
from os import makedirs, listdir
from textblob import TextBlob
from shutil import copyfile
import codecs
import pymysql
def dbConnect(config: dict):
host = config['Parser']['db_host']
user = config['Parser']['db_user']
password = config['Parser']['db_pass']
database = config['Parser']['db_base']
charset = 'utf8mb4'
cursorclass = pymysql.cursors.DictCursor
conn = pymysql.connect(host=host,user=user,password=password,database=database,charset=charset,cursorclass=cursorclass)
link = conn.cursor()
link.execute("SELECT VERSION()")
data = link.fetchone()
print (f"Database version: {data['VERSION()']}")
return link, conn
def readConfig(ini: str) -> dict:
""" Read config, check for errors and store values
:param ini: Path to config_wa_gw.ini
:return: Config as dict
"""
# Read config
config = configparser.ConfigParser()
config.read(ini)
# Check debug mode
if config['Parser']['debug'] == "True":
print("Debug Mode Enabled")
return config
# noinspection PyArgumentList
def loadXML(path: str, comments=False, useBackup=False, debug=False):
if not exists(path): raise Exception(f'Path {path} doesn\'t exist!')
if useBackup: backup(path, debug)
parser = xml.XMLParser(target=xml.TreeBuilder(insert_comments=comments))
tree = xml.parse(path,parser)
root = tree.getroot()
if debug: print(f'Loaded {path}')
return tree, root
def saveXML(path: str, tree: xml.ElementTree, debug=False):
xml.indent(tree, space='\t')
tree.write(path, encoding='utf-8-sig',xml_declaration=True)
string = xml.tostring(tree.getroot(),'unicode').replace('\n','\r\n')
string = f"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n{string}"
if debug: print(f'Saved {path}')
with codecs.open(path,'w','utf-8-sig') as file:
file.write(string)
def saveTxt(path: str, string: str):
with open(path,'w') as file:
file.write(string)
return True
def backup(path, debug=False):
pybak = path+'.bak'
if not exists(pybak):
copyfile(path,pybak)
if debug: print(f'Created {pybak}')
return pybak
def getAllXML(path: str):
files = []
for file in listdir(f"./data/{path}"):
if file.endswith(".xml"):
files.append(file)
return files
def itemsRead(items: dict, debug=False):
print(f"Reading Items...")
type = "Item"
files = getAllXML(type)
itemNo = 0
for file in files:
treeItem, rootItem = loadXML(path=f'./data/{type}/{file}', debug=debug)
for item in rootItem.findall('Item'):
itemNo = itemNo + 1
itemId = item.get('id')
# append to dict
items[itemId] = {}
items[itemId]['id'] = itemId
items[itemId]['category'] = item.get('category') if item.get('category') else ''
items[itemId]['icon'] = item.get('icon') if item.get('icon') else ''
items[itemId]['name'] = item.get('name') if item.get('name') else ''
items[itemId]['grade'] = item.get('rareGrade') if item.get('rareGrade') else '0'
items[itemId]['level'] = item.get('level') if item.get('level') else '1'
items[itemId]['classes'] = item.get('requiredClass') if item.get('requiredClass') else ''
items[itemId]['races'] = item.get('requiredRace') if item.get('requiredRace') else ''
items[itemId]['gender'] = item.get('requiredGender') if item.get('requiredGender') else ''
items[itemId]['tradable'] = '1' if item.get('tradable') == "True" else '0'
items[itemId]['obtainable'] = '1' if item.get('obtainable') == "True" else '0'
items[itemId]['dyeable'] = '1' if item.get('changeColorEnable') == "True" else '0'
items[itemId]['period'] = item.get('periodInMinute') if item.get('periodInMinute') else '0'
items[itemId]['periodAdmin'] = '1' if item.get('periodByWebAdmin') == "True" else '0'
print(f"Reading Items complete: {itemNo}")
return items
def sortIterable(list: set) -> list:
print(f"Sorting Entities...")
listSorted = sorted(list)
listNo = len(listSorted)
print(f"Sorting Entities complete: {listNo}")
return listSorted
def getAttributes(typ: str, debug=False) -> set:
print(f"Getting Attributes...")
files = getAllXML(typ)
attributes = set()
for file in files:
treeItem, rootItem = loadXML(path=f'./data/{typ}/{file}', debug=debug)
for item in rootItem.findall('Item'):
attributes.update(item.attrib.keys())
# print(attributes)
attributesNo = len(attributes)
print(f"Getting Attributes complete: {attributesNo}")
return attributes
def itemsAddName(items: dict, debug=False) -> dict:
print(f"Adding Item Names...")
type = "StrSheet"
files = getAllXML(type)
itemNo = 0
for file in files:
treeItem, rootItem = loadXML(path=f'./data/{type}/{file}', debug=debug)
for item in rootItem.findall('String'):
itemId = item.get('id')
# append to dict
if itemId in items:
itemNo = itemNo + 1
items[itemId]['name_de'] = item.get('string')
items[itemId]['name_en'] = item.get('string')
print(f"Adding Item Names complete: {itemNo}")
return items
def itemsInsertDb(items: dict, link, conn) -> bool:
print(f"Inserting Items...")
save = True
itemNo = 0
for item, data in items.items():
id = data['id']
category = data['category']
icon = data['icon'].replace('.', '/')
name = data['name']
grade = data['grade']
level = data['level']
obtainable = data['obtainable']
tradable = data['tradable']
dyeable = data['dyeable']
period = data['period']
periodAdmin = data['periodAdmin']
name_de = data['name_de'].replace('"', "'")
name_en = data['name_en'].replace('"', "'")
classes = ""
if data['classes']:
for typ in data['classes'].split(';'):
classes = classes + str(getClassId(typ)) + ";"
classes = classes[:-1]
races = ""
if data['races']:
for typ in data['races'].split(';'):
races = races + str(getRaceId(typ)) + ";"
races = races[:-1]
gender = ""
if data['gender']:
gender = str(getGenderId(data['gender']))
query = f'INSERT INTO items (id, category, icon, name, grade, level, classes, races, gender, obtainable, tradable, dyeable, period, periodByWebAdmin, name_de, name_en) ' \
f'VALUES ("{id}", "{category}", "{icon}", "{name}", "{grade}", "{level}", "{classes}", "{races}", "{gender}", "{obtainable}", "{tradable}", "{dyeable}", "{period}", "{periodAdmin}", "{name_de}", "{name_en}")'
try:
link.execute(query)
conn.commit()
itemNo = itemNo + 1
except:
save = False
conn.rollback()
print("Error while writing into Database!")
if save: print(f"Inserting Items complete: {itemNo}")
return save
def performance(stage: str, time=False) -> datetime:
match stage:
case "start":
return datetime.datetime.now()
case "end":
return (datetime.datetime.now() - time).total_seconds()
def getGenderId(name: str) -> int:
match name.lower():
case "male":
return 0
case "female":
return 1
def getRaceId(name: str) -> int:
match name.lower():
case "human":
return 0
case "highelf":
return 1
case "aman":
return 2
case "castanic":
return 3
case "popori":
return 4
case "baraka":
return 5
def getClassId(name: str) -> int:
match name.lower():
case "warrior":
return 0
case "lancer":
return 1
case "slayer":
return 2
case "berserker":
return 3
case "sorcerer":
return 4
case "archer":
return 5
case "priest":
return 6
case "elementalist": # mystic
return 7
case "soulless": # reaper
return 8
case "engineer": # gunner
return 9
case "fighter": # brawler
return 10
case "assassin": # ninja
return 11
case "glaiver": # valkyrie
return 12
# itemSearch = item.get('searchable')
# itemCombatType = item.get('combatItemType')
# itemCombatSubType = item.get('combatItemSubType')
# itemRank = item.get('rank')
# itemConversion = item.get('conversion')
# itemSortNumber = item.get('sortingNumber')
# itemDropType = item.get('dropType')
# itemBuyPrice = item.get('buyPrice')
# itemSellPrice = item.get('sellPrice')
# itemTerritory = item.get('useOnlyTerritory')
# itemDivide = item.get('divide')
# itemUseCount = item.get('itemUseCount')
# itemDropMax = item.get('maxDropUnit')
# itemStackMax = item.get('maxStack')
# itemSlotLimit = item.get('slotLimit')
# itemCooldownGroup = item.get('coolTimeGroup')
# itemCooldown = item.get('coolTime')
# itemStorable = item.get('warehouseStorable')
# itemStorableGuild = item.get('guildWarehouseStorable')
# itemBoundType = item.get('boundType')
# itemDestroy = item.get('destroyable')
# itemDismantle = item.get('dismantlable')
# itemSellMerchant = item.get('storeSellable')
# itemRelocate = item.get('relocatable')
# itemArtisan = item.get('artisanable')
# itemEnchant = item.get('enchantEnable')
# itemGradeUnknown = item.get('unidentifiedItemGrade')
# itemMasterpieceRate = item.get('masterpieceRate')
# itemLinkEquipId = item.get('linkEquipmentId')
# itemLinkLookId = item.get('linkLookInfoId')
# itemChangeLook = item.get('changeLook')
# itemExtractLook = item.get('extractLook')