Skip to content

Instantly share code, notes, and snippets.

@slowpoke-lizard
Last active June 16, 2018 09:22
Show Gist options
  • Save slowpoke-lizard/ef018dff76cf19d0f95a3f4c59732f16 to your computer and use it in GitHub Desktop.
Save slowpoke-lizard/ef018dff76cf19d0f95a3f4c59732f16 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# time python pwb.py scripts/pagefromfile.py -notitle -force -file:/home/fgombault/Sync/duelyst/duelyst-api/wiki_articles.txt
import json
import re
def getCardImageName(p):
return p['name'].replace(" ", "_")
def getRace(p):
if (p['category'] == 'unit'):
return p['type'] # should contain "Minion", "Arcanyst", "Golem"...
else:
return u''
def getType(p):
return unicode.upper(p['category'][0]) + p['category'][1:]
def boldKeywords(str):
output = ""
matches = re.findall(r'<b>[^<>]*</b>', str)
for m in matches:
candidate = re.sub(r'<b>', '[[', re.sub(r'</b>', ']] ', m))
if (not candidate in output):
output = output + candidate
words = output.split()
return " ".join(sorted(set(words), key=words.index))
def getAbilities(p):
if (p['category'] != 'unit'):
return ""
try:
shortdesc = p['description'][0:p['description'].rindex('<br')]
except ValueError:
shortdesc = p['description']
return boldKeywords(shortdesc)
def getTags(p):
output = u"[[%s]] " % p['faction']
output += boldKeywords(p['description'])
# Find some keywords in the text
if (re.search(r'Dispel', p['description'], re.IGNORECASE)):
output += u' [[Dispel]]'
if (re.search(r'Draw', p['description'], re.IGNORECASE)):
output += u' [[Draw]]'
# Unify some word variants
output = re.sub(r"Stunned", "Stun", output)
return output
def getCardSet(p):
if (p['setName'] == 'Core'):
return 'Core'
if (p['setName'] == 'Basic'):
return 'Core'
if (p['setName'] == 'Ancients'):
return 'Ancient Bonds'
if (p['setName'] == 'ShimZar'):
return "Denizens of Shim'Zar"
if (p['setName'] == 'Unearthed'):
return 'Unearthed Prophecy'
if (p['setName'] == 'Immortal'):
return 'Immortal Vanguard'
if (p['setName'] == 'Mythron'):
return 'Trials of Mythron'
# default value
return p['setName']
def getDescription(p):
#TODO: replace *bold* words with [[wiki links]]
output = re.sub(r'<b>', "<b>[[", re.sub(r'</b>', ']]</b>', p['description']))
return output
def getCategories(p):
cats = getTags(p)
cats = re.sub(r'\[\[', "[[Category:", cats)
if (getType(p) != ''):
cats += " [[Category:%s]]" % getType(p)
if (getRace(p) != ''):
cats += " [[Category:%s]]" % getRace(p)
return cats
with open('cards.json') as json_file:
data = json.load(json_file)
for p in data:
if p['id'] < 1000000:
print(u"{{-start-}}")
print(u"'''Data:Cards/" + p['name'] + "'''")
print(u"This page is maintained by bots. MANUAL CHANGES '''WILL''' BE OVERWRITTEN.")
print(u'<onlyinclude>')
print(u'{{Card Metadata')
print(u'| name = ' + p['name'])
print(u'| image = ' + getCardImageName(p))
print(u'| set = ' + getCardSet(p))
print(u'| faction = ' + p['faction'])
print(u'| rarity = ' + p['rarity'])
print(u'| type = ' + getType(p))
print(u'| race = ' + getRace(p))
try:
print(u'| cost = %d' % p['mana'])
except: KeyError
try:
print(u'| attack = %d' % p['attack'])
print(u'| hp = %d' % p['hp'])
except: KeyError
print(u'| abilities = ' + getAbilities(p))
print(u'| tags = ' + getTags(p))
print(u'| desc = ' + getDescription(p))
print(u'| link = ')
print(u'| id = %d' % p['id'])
print(u'| rotation = standard')
print(u'| available = %s' % p['available'])
print(u'| hidden = %s' % p['hidden'])
print(u"}}") # End of Card Metadata
print(u'</onlyinclude>')
print(getCategories(p))
# print(u'\n{{Display Card Metadata}}')
print(u"{{-stop-}}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment