-
Notifications
You must be signed in to change notification settings - Fork 6
/
ddbtoxml.py
executable file
·479 lines (471 loc) · 24.7 KB
/
ddbtoxml.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/local/bin/python3
import os
import sys
import json
import math
import uuid
import requests
import tempfile
import shutil
import re
import base64
from json import JSONDecodeError
def getJSON(theurl):
rawjson = ""
if theurl.startswith("https://www.dndbeyond.com/"):
#Pretend to be firefox
user_agent = "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:70.0) Gecko/20100101 Firefox/70.0"
headers = {'User-Agent': user_agent}
urlcomponents = theurl.split("/")
charid = urlcomponents[-1]
if charid == "json":
charid = urlcomponents[-2]
url = "https://www.dndbeyond.com/character/{}/json".format(charid)
response = requests.get(url,headers=headers)
if response.status_code != 200:
print (theurl)
print ("Could not download this character from D&D Beyond: {}".format(response.status_code))
print ("Make sure the character is public")
return
else:
if "character" in response.json():
character = response.json()["character"]
else:
character = response.json()
return character
else:
print ("This is not a url for D&D Beyond: {}".format(theurl))
return
def genXML(character):
level = 0
characterXML = "\t<player>\n"
characterXML += "\t\t<name>{}</name>\n".format(character["name"])
#characterXML += "\t\t<slug>ddb-{}</slug>\n".format(character["id"])
if len(character["classes"]) > 1:
allclasses = []
for cclass in character["classes"]:
level += cclass["level"]
allclasses.append("{} {}".format(cclass["definition"]["name"],cclass["level"]))
characterXML += "\t\t<class>{}</class>\n".format('/'.join(allclasses))
else:
characterclass = character["classes"][0]["definition"]["name"]
level = character["classes"][0]["level"]
characterXML += "\t\t<class>{}</class>\n".format(characterclass)
characterXML += "\t\t<level>{}</level>\n".format(level)
characterXML += "\t\t<xp>{}</xp>\n".format(character["currentXp"])
hitpoints = character["baseHitPoints"]
armorclass = 0
stat_str = character["stats"][0]["value"]
stat_dex = character["stats"][1]["value"]
stat_con = character["stats"][2]["value"]
stat_int = character["stats"][3]["value"]
stat_wis = character["stats"][4]["value"]
stat_cha = character["stats"][5]["value"]
race = character["race"]["fullName"]
speed = character["race"]["weightSpeeds"]["normal"]["walk"]
modifiers = character["modifiers"]
senses = []
for modifier in (modifiers["race"]+modifiers["class"]+modifiers["background"]+modifiers["item"]+modifiers["feat"]+modifiers["condition"]):
if modifier["isGranted"] == True and modifier["type"].lower() == "bonus":
if modifier["subType"].lower() == "strength-score":
stat_str += modifier["value"]
elif modifier["subType"].lower() == "dexterity-score":
stat_dex += modifier["value"]
elif modifier["subType"].lower() == "constitution-score":
stat_con += modifier["value"]
elif modifier["subType"].lower() == "inteligence-score":
stat_int += modifier["value"]
elif modifier["subType"].lower() == "wisdom-score":
stat_wis += modifier["value"]
elif modifier["subType"].lower() == "charisma-score":
stat_cha += modifier["value"]
hitpoints += math.floor((stat_con - 10)/2)*level
initiative = math.floor((stat_dex - 10)/2)
equipment = []
for equip in character["inventory"]:
# for i in range(equip["quantity"]):
# equipment.append(equip["definition"]["name"])
# if equip["quantity"] > 1:
# equipment.append("{} (x{:d})".format(equip["definition"]["name"],equip["quantity"]))
# else:
# equipment.append(equip["definition"]["name"])
if "armor" in equip["definition"]["type"].lower() and "armor" not in equip["definition"]["name"].lower():
equipment.append(equip["definition"]["name"] + " Armor")
else:
equipment.append(equip["definition"]["name"])
if equip["equipped"] == True and "armorClass" in equip["definition"]:
armorclass += equip["definition"]["armorClass"]
if armorclass == 0:
armorclass = 10
armorclass += math.floor((stat_dex - 10)/2)
light = ""
languages = []
resistence = []
immunity = []
skill = {}
str_save = math.floor((stat_str - 10)/2)
skill["Athletics"] = str_save
dex_save = math.floor((stat_dex - 10)/2)
skill["Acrobatics"] = dex_save
skill["Sleight of Hand"] = dex_save
skill["Stealth"] = dex_save
con_save = math.floor((stat_con - 10)/2)
int_save = math.floor((stat_int - 10)/2)
skill["Arcana"] = int_save
skill["History"] = int_save
skill["Investigation"] = int_save
skill["Nature"] = int_save
skill["Religion"] = int_save
wis_save = math.floor((stat_wis - 10)/2)
skill["Animal Handling"] = wis_save
skill["Insight"] = wis_save
skill["Medicine"] = wis_save
skill["Perception"] = wis_save
skill["Survival"] = wis_save
cha_save = math.floor((stat_cha - 10)/2)
skill["Deception"] = cha_save
skill["Intimidation"] = cha_save
skill["Performance"] = cha_save
skill["Persuasion"] = cha_save
for modifier in (modifiers["race"]+modifiers["class"]+modifiers["background"]+modifiers["item"]+modifiers["feat"]+modifiers["condition"]):
if modifier["type"].lower() == "half-proficiency":
bonus = math.ceil(((level/4)+1)/2)
if modifier["subType"].lower() == "athletics" or modifier["subType"].lower() == "ability-checks":
skill["Athletics"] = math.floor((stat_str - 10)/2) + bonus
if modifier["subType"].lower() == "acrobatics" or modifier["subType"].lower() == "ability-checks":
skill["Acrobatics"] = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "sleight-of-hand" or modifier["subType"].lower() == "ability-checks":
skill["Sleight of Hand"] = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "stealth" or modifier["subType"].lower() == "ability-checks":
skill["Stealth"] = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "arcana" or modifier["subType"].lower() == "ability-checks":
skill["Arcana"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "history" or modifier["subType"].lower() == "ability-checks":
skill["History"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "investigation" or modifier["subType"].lower() == "ability-checks":
skill["Investigation"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "nature" or modifier["subType"].lower() == "ability-checks":
skill["Nature"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "religion" or modifier["subType"].lower() == "ability-checks":
skill["Religion"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "animal-handling" or modifier["subType"].lower() == "ability-checks":
skill["Animal Handling"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "insight" or modifier["subType"].lower() == "ability-checks":
skill["Insight"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "medicine" or modifier["subType"].lower() == "ability-checks":
skill["Medicine"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "perception" or modifier["subType"].lower() == "ability-checks":
skill["Perception"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "survival" or modifier["subType"].lower() == "ability-checks":
skill["Survival"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "deception" or modifier["subType"].lower() == "ability-checks":
skill["Deception"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "intimidation" or modifier["subType"].lower() == "ability-checks":
skill["Intimidation"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "performance" or modifier["subType"].lower() == "ability-checks":
skill["Performance"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "persuasion" or modifier["subType"].lower() == "ability-checks":
skill["Persuasion"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "initiative":
initiative = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "strength-saving-throws":
str_save = math.floor((stat_str - 10)/2) + bonus
if modifier["subType"].lower() == "dexterity-saving-throws":
dex_save = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "constitution-saving-throws":
con_save = math.floor((stat_con - 10)/2) + bonus
if modifier["subType"].lower() == "inteligence-saving-throws":
int_save = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "wisdom-saving-throws":
wis_save = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "charisma-saving-throws":
cha_save = math.floor((stat_cha - 10)/2) + bonus
for modifier in (modifiers["race"]+modifiers["class"]+modifiers["background"]+modifiers["item"]+modifiers["feat"]+modifiers["condition"]):
if modifier["type"].lower() == "proficiency":
bonus = math.ceil((level/4)+1)
if modifier["subType"].lower() == "athletics" or modifier["subType"].lower() == "ability-checks":
skill["Athletics"] = math.floor((stat_str - 10)/2) + bonus
if modifier["subType"].lower() == "acrobatics" or modifier["subType"].lower() == "ability-checks":
skill["Acrobatics"] = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "sleight-of-hand" or modifier["subType"].lower() == "ability-checks":
skill["Sleight of Hand"] = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "stealth" or modifier["subType"].lower() == "ability-checks":
skill["Stealth"] = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "arcana" or modifier["subType"].lower() == "ability-checks":
skill["Arcana"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "history" or modifier["subType"].lower() == "ability-checks":
skill["History"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "investigation" or modifier["subType"].lower() == "ability-checks":
skill["Investigation"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "nature" or modifier["subType"].lower() == "ability-checks":
skill["Nature"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "religion" or modifier["subType"].lower() == "ability-checks":
skill["Religion"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "animal-handling" or modifier["subType"].lower() == "ability-checks":
skill["Animal Handling"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "insight" or modifier["subType"].lower() == "ability-checks":
skill["Insight"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "medicine" or modifier["subType"].lower() == "ability-checks":
skill["Medicine"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "perception" or modifier["subType"].lower() == "ability-checks":
skill["Perception"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "survival" or modifier["subType"].lower() == "ability-checks":
skill["Survival"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "deception" or modifier["subType"].lower() == "ability-checks":
skill["Deception"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "intimidation" or modifier["subType"].lower() == "ability-checks":
skill["Intimidation"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "performance" or modifier["subType"].lower() == "ability-checks":
skill["Performance"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "persuasion" or modifier["subType"].lower() == "ability-checks":
skill["Persuasion"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "initiative":
initiative = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "strength-saving-throws":
str_save = math.floor((stat_str - 10)/2) + bonus
if modifier["subType"].lower() == "dexterity-saving-throws":
dex_save = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "constitution-saving-throws":
con_save = math.floor((stat_con - 10)/2) + bonus
if modifier["subType"].lower() == "inteligence-saving-throws":
int_save = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "wisdom-saving-throws":
wis_save = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "charisma-saving-throws":
cha_save = math.floor((stat_cha - 10)/2) + bonus
for modifier in (modifiers["race"]+modifiers["class"]+modifiers["background"]+modifiers["item"]+modifiers["feat"]+modifiers["condition"]):
if modifier["type"].lower() == "expertise":
bonus = math.ceil(((level/4)+1)*2)
if modifier["subType"].lower() == "athletics" or modifier["subType"].lower() == "ability-checks":
skill["Athletics"] = math.floor((stat_str - 10)/2) + bonus
if modifier["subType"].lower() == "acrobatics" or modifier["subType"].lower() == "ability-checks":
skill["Acrobatics"] = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "sleight-of-hand" or modifier["subType"].lower() == "ability-checks":
skill["Sleight of Hand"] = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "stealth" or modifier["subType"].lower() == "ability-checks":
skill["Stealth"] = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "arcana" or modifier["subType"].lower() == "ability-checks":
skill["Arcana"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "history" or modifier["subType"].lower() == "ability-checks":
skill["History"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "investigation" or modifier["subType"].lower() == "ability-checks":
skill["Investigation"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "nature" or modifier["subType"].lower() == "ability-checks":
skill["Nature"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "religion" or modifier["subType"].lower() == "ability-checks":
skill["Religion"] = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "animal-handling" or modifier["subType"].lower() == "ability-checks":
skill["Animal Handling"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "insight" or modifier["subType"].lower() == "ability-checks":
skill["Insight"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "medicine" or modifier["subType"].lower() == "ability-checks":
skill["Medicine"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "perception" or modifier["subType"].lower() == "ability-checks":
skill["Perception"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "survival" or modifier["subType"].lower() == "ability-checks":
skill["Survival"] = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "deception" or modifier["subType"].lower() == "ability-checks":
skill["Deception"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "intimidation" or modifier["subType"].lower() == "ability-checks":
skill["Intimidation"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "performance" or modifier["subType"].lower() == "ability-checks":
skill["Performance"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "persuasion" or modifier["subType"].lower() == "ability-checks":
skill["Persuasion"] = math.floor((stat_cha - 10)/2) + bonus
if modifier["subType"].lower() == "initiative":
initiative = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "strength-saving-throws":
str_save = math.floor((stat_str - 10)/2) + bonus
if modifier["subType"].lower() == "dexterity-saving-throws":
dex_save = math.floor((stat_dex - 10)/2) + bonus
if modifier["subType"].lower() == "constitution-saving-throws":
con_save = math.floor((stat_con - 10)/2) + bonus
if modifier["subType"].lower() == "inteligence-saving-throws":
int_save = math.floor((stat_int - 10)/2) + bonus
if modifier["subType"].lower() == "wisdom-saving-throws":
wis_save = math.floor((stat_wis - 10)/2) + bonus
if modifier["subType"].lower() == "charisma-saving-throws":
cha_save = math.floor((stat_cha - 10)/2) + bonus
for modifier in (modifiers["race"]+modifiers["class"]+modifiers["background"]+modifiers["item"]+modifiers["feat"]+modifiers["condition"]):
if modifier["type"].lower() == "set" and modifier["subType"].lower() == "unarmored-armor-class":
if modifier["statId"] == 3:
armorclass += math.floor((stat_con - 10)/2)
if modifier["value"] is not None:
armorclass += modifier["value"]
if modifier["type"].lower() == "ignore" and modifier["subType"].lower() == "unarmored-dex-ac-bonus":
armorclass -= math.floor((stat_dex - 10)/2)
if modifier["type"].lower() == "set-base" and modifier["subType"].lower() == "darkvision":
senses.append("{} {} ft.".format(modifier["subType"].lower(),modifier["value"]))
light = "\t\t<light id=\"{}\">\n\t\t\t<enabled>YES</enabled>\n\t\t\t<radiusMin>0</radiusMin>\n\t\t\t<radiusMax>{}</radiusMax>\n\t\t\t<color>#ffffff</color>\n\t\t\t<opacity>0.5</opacity>\n\t\t\t<alwaysVisible>YES</alwaysVisible>\n\t\t</light>\n".format(uuid.uuid4(),modifier["value"])
if modifier["type"].lower() == "language":
languages.append(modifier["friendlySubtypeName"])
if modifier["type"].lower() == "resistance":
resistence.append(modifier["friendlySubtypeName"])
if modifier["type"].lower() == "immunity":
immunity.append(modifier["friendlySubtypeName"])
spells = []
for spell in character["spells"]["race"]:
spells.append(spell["definition"]["name"])
for spell in character["spells"]["class"]:
spells.append(spell["definition"]["name"])
for spell in character["spells"]["item"]:
spells.append(spell["definition"]["name"])
for spell in character["spells"]["feat"]:
spells.append(spell["definition"]["name"])
for classsp in character["classSpells"]:
for spell in classsp["spells"]:
spells.append(spell["definition"]["name"])
party = ""
if "campaign" in character and character["campaign"] is not None:
party = character["campaign"]["name"]
background = ""
if "background" in character and character["background"] is not None and character["background"]["definition"] is not None:
background = character["background"]["definition"]["name"]
bg_def = character["background"]["definition"]
feats = []
for feat in character["feats"]:
feats.append(feat["definition"]["name"])
feat_def = feat["definition"]
personality = character["traits"]["personalityTraits"]
bonds = character["traits"]["bonds"]
ideals = character["traits"]["ideals"]
flaws = character["traits"]["flaws"]
appearance = character["traits"]["appearance"]
if appearance is None:
appearance = ""
characterXML += "\t\t<race>{}</race>\n".format(race)
characterXML += "\t\t<initiative>{}</initiative>\n".format(initiative)
characterXML += "\t\t<ac>{}</ac>\n".format(armorclass)
characterXML += "\t\t<hp>{}</hp>\n".format(hitpoints)
characterXML += "\t\t<speed>{}</speed>\n".format(speed)
characterXML += "\t\t<str>{}</str>\n".format(stat_str)
characterXML += "\t\t<dex>{}</dex>\n".format(stat_dex)
characterXML += "\t\t<con>{}</con>\n".format(stat_con)
characterXML += "\t\t<int>{}</int>\n".format(stat_int)
characterXML += "\t\t<wis>{}</wis>\n".format(stat_wis)
characterXML += "\t\t<cha>{}</cha>\n".format(stat_cha)
characterXML += "\t\t<descr>{}\n<i><a href="https://www.dndbeyond.com/profile/username/characters/{}">Imported from D&D Beyond</a></i></descr>\n".format(appearance,character["id"])
characterXML += "\t\t<party>{}</party>\n".format(party)
characterXML += "\t\t<faction>{}</faction>\n".format("")
characterXML += "\t\t<passive>{}</passive>\n".format(skill["Perception"]+10)
characterXML += "\t\t<spells>{}</spells>\n".format(", ".join(spells))
characterXML += "\t\t<senses>{}</senses>\n".format(", ".join(senses))
characterXML += "\t\t<languages>{}</languages>\n".format(", ".join(languages))
characterXML += "\t\t<equipment>{}</equipment>\n".format(", ".join(equipment))
characterXML += "\t\t<image>{}</image>\n".format(character["avatarUrl"].split('/')[-1])
characterXML += "\t\t<personality>{}</personality>\n".format(personality)
characterXML += "\t\t<ideals>{}</ideals>\n".format(ideals)
characterXML += "\t\t<bonds>{}</bonds>\n".format(bonds)
characterXML += "\t\t<flaws>{}</flaws>\n".format(flaws)
skills = []
for sk in sorted(skill.keys()):
skills.append("{} {:+d}".format(sk,skill[sk]))
characterXML += "\t\t<skill>{}</skill>\n".format(", ".join(skills))
characterXML += "\t\t<save>Str {:+d}, Dex {:+d}, Con {:+d}, Int {:+d}, Wis {:+d}, Cha {:+d}</save>\n".format(str_save,dex_save,con_save,int_save,wis_save,cha_save)
characterXML += "\t\t<resist>{}</resist>\n".format(", ".join(resistence))
characterXML += "\t\t<immune>{}</immune>\n".format(", ".join(immunity))
characterXML += "\t\t<background>{}</background>\n".format(background)
characterXML += "\t\t<feats>{}</feats>\n".format(", ".join(feats))
if light != "":
characterXML += light
characterXML += "\t</player>\n"
return characterXML
def findURLS(fp):
fp.seek(0, 0)
characters = []
regex = re.compile("<a[^>]*href=\"(/profile/.*/[0-9]+)\"[^>]*class=\"ddb-campaigns-character-card-header-upper-details-link\"[^>]*>")
for line in fp:
m = regex.search(line)
if m:
characterurl = m.group(1)
if not characterurl.startswith("https://www.dndbeyond.com/"):
characters.append("https://www.dndbeyond.com"+characterurl)
else:
characters.append(characterurl)
return characters
def main():
tempdir = tempfile.mkdtemp(prefix="ddbtoxml_")
comependiumxml = os.path.join(tempdir, "compendium.xml")
playersdir = os.path.join(tempdir, "players")
os.mkdir(playersdir)
with open(comependiumxml,mode='a',encoding='utf-8') as comependium:
comependium.write("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<compendium>\n")
args = sys.argv
if len(args) == 2 and args[1].startswith("--campaign"):
regex = re.compile("<a[^>]*href=\"(/profile/.*/[0-9]+)\"[^>]*class=\"ddb-campaigns-character-card-header-upper-details-link\"[^>]*>")
if args[1] == "--campaign":
readin = sys.stdin
elif os.path.isfile(args[1][11:]):
readin = open(args[1][11:],'r')
else:
readin = base64.b64decode(args[1][11:]).decode("utf-8").splitlines()
args = [sys.argv[0]]
for line in readin:
m = regex.search(line)
if m:
characterurl = m.group(1)
if not characterurl.startswith("https://www.dndbeyond.com/"):
args.append("https://www.dndbeyond.com"+characterurl)
else:
args.append(characterurl)
try:
readin.close()
except:
pass
characters = []
for i in range(len(args)):
if args[i] == __file__:
continue
if os.path.isfile(args[i]):
with open(args[i]) as infile:
try:
json.load(infile)
characters.append(args[i])
except JSONDecodeError:
found = findURLS(infile)
characters.extend(found)
else:
characters.append(args[i])
if len(characters) == 0:
characters.append("-")
for i in range(len(characters)):
if characters[i] == '-' or os.path.isfile(characters[i]):
if os.path.isfile(characters[i]):
with open(characters[i],"r") as jsonfile:
charjson = json.loads(jsonfile.read())
else:
charjson = json.loads(sys.stdin.read())
if "character" in charjson:
character = charjson["character"]
else:
character = charjson
else:
character = getJSON(characters[i])
if character is not None:
xmloutput = genXML(character)
with open(comependiumxml,mode='a',encoding='utf-8') as comependium:
comependium.write(xmloutput)
if character["avatarUrl"] != "":
local_filename = os.path.join(playersdir,character["avatarUrl"].split('/')[-1])
r = requests.get(character["avatarUrl"], stream=True)
if r.status_code == 200:
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
with open(comependiumxml,mode='a',encoding='utf-8') as comependium:
comependium.write("</compendium>")
zipfile = shutil.make_archive("ddbxml","zip",tempdir)
os.rename(zipfile,os.path.join(os.getcwd(),"ddbxml.compendium"))
zipfile = os.path.join(os.getcwd(),"ddbxml.compendium")
try:
import console
console.open_in (zipfile)
except ImportError:
print(zipfile)
try:
shutil.rmtree(tempdir)
except:
print("Warning: error trying to delete the temporal directory:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
if __name__== "__main__":
main()