FANDOM

Bot for User:Jbzdarkid.

To make improvement suggestions, simply leave a message on my talk page.


Template:MonsterMARDEK

# -*- coding: utf-8 -*-
from re import search, sub
from wikitools.category import Category
from wikitools.api import APIRequest
from wikitools.wiki import Wiki
 
param_subs = {'Attack':'atk', 'image':''} # Used for renaming parameters, or removing old/broken parameters.
param_order = '''
name
no
enemy
ImageName
ImageSize
desc
 
element
type
class
exp
battleMusic
location
 
hp
hpmod
mp
mpmod
str
vit
spr
agl
atk
def
mdef
eva
variants
 
FireResist
FireResistMod
WaterResist
WaterResistMod
AirResist
AirResistMod
EarthResist
EarthResistMod
LightResist
LightResistMod
DarkResist
DarkResistMod
AetherResist
AetherResistMod
FigResist
FigResistMod
PhysicalResist
PhysicalResistMod
ThaumaResist
ThaumaResistMod
PoisonResist
PoisonResistMod
ParalysisResist
ParalysisResistMod
NumbResist
NumbResistMod
SilenceResist
SilenceResistMod
CurseResist
CurseResistMod
ConfusionResist
ConfusionResistMod
SleepResist
SleepResistMod
BlindnessResist
BlindnessResistMod
BleedResist
BleedResistMod
ZombieResist
ZombieResistMod
BerserkResist
BerserkResistMod
 
moves
equipment
loot
notes
trivia
related
sprites
'''.split('\n')
pc_links = {
	'Deugan': 'Deugan Selmae Eh-Deredu',
	'Donovan': 'Donovan Gonoru Fa-Alregis',
	'Elwyen': 'Elwyen Sirene Wu-Nympha',
	'Emela': 'Emela Andra Wu-Jardu',
	'Gloria': 'Gloria Eh-Korey',
	'Mardek': 'Mardek_Innanu_El-Enkidu',
	'Meraeador': 'Meraeador Dunsin Fa-Doch',
	'Sharla': 'Sharla Qualle Ae-Andrias',
	'Sslen\'ck': 'Sslen\'ck Ea-Sslenal',
	'Vehrn': 'Vehrn Juonour El-Ganobyi',
	'Zach': 'Zach Ursus Ae-Jarino',
	'Legion': 'Legion',
	'Solaar': 'Solaar',
}
 
wiki = Wiki('http://figverse.wikia.com/api.php')
wiki.login(username='BOT Jbzdarkid', password='******')
 
cat = Category(wiki, "MARDEK_Monsters")
for article in cat.getAllMembersGen(namespaces=[0]):
	if article.title in ['Monsters (MARDEK)/Strategies', 'Monsters (MARDEK)']:
		continue
	article.getWikiText()
	article.wikitext = article.wikitext.replace('<br />', '<br>')
	article.wikitext = article.wikitext.replace('<br/>', '<br>')
	if '<tabber>' in article.wikitext:
		parts = article.wikitext.split('\n}}')
	else:
		parts = [article.wikitext, '']
	output3 = ''
	for part in parts[:-1]: # This gets rid of the final </tabber> section
		output1 = {}
		buffer = ''
		param = ''
		# Parsing old template names
		split = None
		if 'MonsterMARDEK' in part:
			split = part.split('{{MonsterMARDEK', 1)
		elif 'MonsterMardek' in part:
			split = part.split('{{MonsterMardek', 1)
		else:
			print part
		output2 = split[0] + '{{MonsterMARDEK\n' # This ugliness allows for comments at the head of the page to be preserved.
		for line in split[1].split('\n'):
			if len(line) == 0:
				continue
			if line == '}}':
				continue
			if line[0] == '|':
				output1[param] = buffer # This adds a {'':''} in the first iteration, which is removed later.
				buffer = ''
				m = search('\\|(.*?)=', line)
				param = m.group(1).strip()
				if param in param_subs:
					param = param_subs[param]
				line = line.split('=', 1)[1]
			else:
				if param != 'related':
					buffer += '\n'
			line = line.strip(' ')
			_line = line
			line = sub('<br( )*/?>', '<br>', line)
			line = sub(r"'s]]", "]]'s", line)
			if param in ['enemy', 'location', 'variants']:
				for pc in pc_links.keys():
					if pc == pc_links[pc]:
						line = sub(r'(?<!\[)(?<!\|)'+pc, '[['+pc+r']]', line)
						continue
					else:
						line = sub(r'(?<!\[)(?<!\|)'+pc, '[['+pc_links[pc]+'|'+pc+r']]', line)
				line = sub('Ch(\d)', r'Chapter \1', line)
				line = sub('survival', 'Survival', line)
				line = sub('tournament', 'Tournament', line)
				line = sub(r'\[\[Cambria( Arena|)(\|Cambria Arena|)]]', '[[Cambria#Arena|Cambria Arena]]', line)
				# This line replaces "Sharla's Ch3 survival, Solaar's Ch3 survival" with "Sharla and Solaar's Chapter 3 Survival Tournaments"
				line = sub(r"(.*?)'s.*?Survival.*?(,|and) (.*?)'s.*?Survival", r"\1 and \3's Chapter 3 Survival Tournaments", line)
				# This line replaces "Zach's and Donovan's" with "Zach and Donovan's"
				line = sub(r"(.*?)'s(.*?) (.*?)'s", r"\1\2 \3's", line)
				# We run this twice to fix lists of length 3 and 4.
				line = sub(r"(.*?)'s(.*?) (.*?)'s", r"\1\2 \3's", line)
				# :(
				line = sub(r'Saul Dungeon', "Saul's Dungeon", line)
				# Avoids Moric (Chapter 2 Boss)
				line = sub(r'(?<!\()Chapter 2 ', '[[MARDEK Chapter 2: A New Hero|Chapter 2]] ', line)
				# Avoids matching "Dreamrealm"
				line = sub(r'(?<!l)(?<!m) - (.*)}}', r' (\1)}}', line)
				line = sub(r'(?<!l)(?<!m) - (.*)', r' (\1)', line)
				line = sub(r'\(Champion', '(Chapter 3 Champion', line)
				line = sub('Chapter 3 Tournament', 'Chapter 3 Champion Tournament', line)
				line = sub(r'Chapter 3 Survival\)', 'Chapter 3 Survival Tournament)', line)
				line = sub(r'Chapter 3\)', 'Chapter 3 Champion Tournament)', line)
				line = sub('Chapter 1 ', '[[MARDEK Chapter 1: A Fallen Star|Chapter 1]] ', line)
				line = sub('Chapter 3 ', '[[MARDEK Chapter 3: Keystones|Chapter 3]] ', line)
			line = sub(r']] - \[\[', ']] {{·}} [[', line)
			if param == 'variants':
				m = search(r'{{MVariant\|(\d*)\|(\d*)\|', line)
				if m is not None:
					level = m.group(1)
					hp = output1['hp']
					# Fixes problems with 80(+30)
					hp = hp.split('(')[0].strip()
					# Fixes problems with Base: 80
					hp = hp.split(' ')[-1]
					# Fixes 15,700 -> 15700
					hp = hp.replace(',', '')
					hpmod = 0
					try:
						hpmod = output1['hpmod']
						m = search(r' (\d+)\+(\d+)', hpmod)
						if m is not None:
							hp = m.group(1)
							hpmod = m.group(2)
						elif hpmod.strip() == '':
							hpmod = 0
					except KeyError:
						hpmod = 0
					calc_hp = str(int(hp) + (int(level)-1)*int(hpmod))
					line = sub(r'{{MVariant\|\d*\|\d*', '{{MVariant|'+level+'|'+calc_hp, line)
			if line != _line:
				print _line
				print line
			buffer += line
		output1[param] = buffer
		del output1['']
		keys = sorted(output1.keys(), key=lambda s: param_order.index(s))
		for key in keys:
			if '\n' not in output1[key]:
				output2 += '| {param} = '.format(param=key)
			else:
				output2 += '| {param} ='.format(param=key)
				if key == 'notes' and output1[key][:1] != '\n': # Ensure that multiline notes follow the correct style
					output2 += '\n'
			output2 += output1[key] + '\n'
		output2 += '}}'
		output3 += output2
 
	if '<tabber>' in output3:
		output3 += '</tabber>'
 
	if article.wikitext == output3: # Don't upload files that haven't changed
		continue
 
	# print output3
	# raw_input()
	# continue
 
	params = { # Wikia uses custom APIs, so I can't use the wiki.getEditToken() method.
		'action':'query',
		'prop':'info',
		'intoken':'edit',
		'inprop':'pageid',
		'titles':article.title,
	}
	req = APIRequest(wiki, params)
	response = req.query(False)
	step1 = response['query']['pages']
	token = step1[step1.keys()[0]]['edittoken']
 
	params = { # Wikia uses custom APIs, so I can't use the page.edit() method.
		'action':'edit',
		'token':token,
		'bot':'true',
		'watchlist':'unwatch',
		'title':article.title,
		'text':output3,
		'summary':'Automatic style/content revision by [[User:BOT Jbzdarkid]].',
	}
	req = APIRequest(wiki, params)
	response = req.query(False)
	print response

MItemLink -> MARDEK Item Link

# -*- coding: utf-8 -*-
from wikitools.api import APIRequest
from wikitools.wiki import Wiki
from wikitools.page import Page
 
wiki = Wiki('http://figverse.wikia.com/api.php')
wiki.login(username='BOT Jbzdarkid', password='******')
 
params = {
	'action': 'query',
	'list': 'allpages',
	'aplimit': '500',
}
req = APIRequest(wiki, params)
for result in req.queryGen():
	for article in result['query']['allpages']:
		page = Page(wiki, article['title'])
		page.getWikiText()
		output = page.wikitext.replace('{{MItemLink|', '{{subst:#invoke:MARDEK Item Link|text|')
		if page.wikitext == output: # Don't upload files that haven't changed
			continue
 
		params = { # Wikia uses custom APIs, so I can't use the wiki.getEditToken() method.
			'action':'query',
			'prop':'info',
			'intoken':'edit',
			'inprop':'pageid',
			'titles':page.title,
		}
		req = APIRequest(wiki, params)
		response = req.query(False)
		step1 = response['query']['pages']
		token = step1[step1.keys()[0]]['edittoken']
 
		params = { # Wikia uses custom APIs, so I can't use the page.edit() method.
			'action':'edit',
			'token':token,
			'bot':'true',
			'watchlist':'unwatch',
			'title':page.title,
			'text':output,
			'summary':'Replaced [[Template:MItemLink|]] with [[Module:MARDEK Item Link|]].',
		}
		req = APIRequest(wiki, params)
		response = req.query(False)
		print response

Template:MARDEKItem

# -*- coding: utf-8 -*-
from re import search, sub
from wikitools.category import Category
from wikitools.api import APIRequest
from wikitools.wiki import Wiki
 
param_subs = {'find':'chest', 'use':''} # Used for renaming parameters, or removing old/broken parameters.
param_order = [
		'name',
		'element',
		'type',
		'value',
		'desc',
		'quest',
		'craft',
		'prop',
		'skill',
		'join',
		'chest',
		'drop',
		'buy',
		'recipe',
		'obtain'
]
 
types = {
	"MARDEK Accessories": "ACCESSORY",
	"MARDEK Claws": "WEAPON: CLAW",
	"MARDEK Clothing": "ARMOUR: CLOTHING",
	"MARDEK Daggers": "WEAPON: DAGGER",
	"MARDEK Doubleswords": "WEAPON: DOUBLESWORD",
	"MARDEK Expendable Items": "EXPENDABLE ITEM",
	"MARDEK Full Helms": "HELMET: FULL HELM",
	"MARDEK Gemstones": "GEMSTONE",
	"MARDEK Greataxes": "WEAPON: GREATAXE",
	"MARDEK Greatmaces": "WEAPON: GREATMACE",
	"MARDEK Greatswords": "WEAPON: GREATSWORD",
	"MARDEK Guns": "WEAPON: GUN",
	"MARDEK Half Helms": "HELMET: HALF HELM",
	"MARDEK Harps": "WEAPON: HARP",
	"MARDEK Hats": "HELMET: HAT",
	"MARDEK Heavy Armour": "ARMOUR: HEAVY",
	"MARDEK Inventions": "INVENTION",
	"MARDEK Light Armour": "ARMOUR: LIGHT",
	"MARDEK Medium Armour": "ARMOUR: MEDIUM",
	"MARDEK Miscellaneous Items": "MISCELLANEOUS ITEM",
	"MARDEK Plot Items": "PLOT",
	"MARDEK R. Plating": "ARMOUR: R. PLATING",
	"MARDEK Robes": "ARMOUR: ROBE",
	"MARDEK Robotarms": "WEAPON: ROBOTARM",
	"MARDEK Rods": "WEAPON: ROD",
	"MARDEK Scythes": "WEAPON: SCYTHE",
	"MARDEK Sheet Music": "SHEET MUSIC",
	"MARDEK Shields": "SHIELD",
	"MARDEK Spears": "WEAPON: SPEAR",
	"MARDEK Staves": "WEAPON: STAFF",
	"MARDEK Stoles": "ARMOUR: STOLE",
	"MARDEK Swords": "WEAPON: SWORD",
	"MARDEK WalkingSticks": "WEAPON: WALKINGSTICK",
}
 
insertions = {
		"MARDEK Accessories": "in either Accessory slot",
		"MARDEK Claws": "in the Weapon slot",
		"MARDEK Clothing": "in the Armour slot",
		"MARDEK Daggers": "in the Weapon slot",
		"MARDEK Doubleswords": "in the Weapon slot",
		"MARDEK Full Helms": "in the Helmet slot",
		"MARDEK Gemstones": "in either Accessory slot",
		"MARDEK Greataxes": "in the Weapon slot",
		"MARDEK Greatmaces": "in the Weapon slot",
		"MARDEK Greatswords": "in the Weapon slot",
		"MARDEK Guns": "in the Weapon slot",
		"MARDEK Half Helms": "in the Helmet slot",
		"MARDEK Harps": "in the Weapon slot",
		"MARDEK Hats": "in the Helmet slot",
		"MARDEK Heavy Armour": "in the Armour slot",
		"MARDEK Inventions": "in the Weapon and Shield slots",
		"MARDEK Light Armour": "in the Armour slot",
		"MARDEK Medium Armour": "in the Armour slot",
		"MARDEK R. Plating": "in the Armour slot",
		"MARDEK Robes": "in the Armour slot",
		"MARDEK Robotarms": "in the Weapon slot",
		"MARDEK Rods": "in the Weapon slot",
		"MARDEK Scythes": "in the Weapon slot",
		"MARDEK Sheet Music": "in the Shield slot",
		"MARDEK Shields": "in the Shield slot",
		"MARDEK Spears": "in the Weapon slot",
		"MARDEK Staves": "in the Weapon slot",
		"MARDEK Stoles": "in the Armour slot",
		"MARDEK Swords": "in the Weapon slot",
		"MARDEK WalkingSticks": "in the Weapon slot",
}
 
def safeindex(list, item):
	index = -1
	try:
		index = list.index(item)
	except ValueError:
		pass
	return index
 
wiki = Wiki('http://figverse.wikia.com/api.php')
wiki.login(username='BOT Jbzdarkid', password='******')
 
cat = Category(wiki, "MARDEK_Items")
for article in cat.getAllMembersGen(namespaces=[0]):
	if 'MARDEK ' in article.title and 'MARDEK Item' not in article.title:
		continue
	if article.title in ['Gold (MARDEK)', 'Item Crafting (MARDEK)', 'Items (MARDEK)']:
		continue
	article.getWikiText()
	article.wikitext = article.wikitext.replace('<br />', '<br>')
	article.wikitext = article.wikitext.replace('<br/>', '<br>')
	article.getCategories()
	if '<tabber>' in article.wikitext:
		print 'Tabber found in article', article.title
		parts = article.wikitext.split('\n}}')
	else:
		parts = [article.wikitext, '']
	output3 = ''
	for part in parts[:-1]: # This gets rid of the final </tabber> section
		output1 = {}
		buffer = ''
		param = ''
		# Parsing old template names
		split = None
		if 'MARDEKItem' in part:
			split = part.split('{{MARDEKItem', 1)
		else:
			print part
		output2 = split[0] + '{{MARDEKItem\n' # This ugliness allows for comments at the head of the page to be preserved.
		footer = False
		footer_text = ''
		for line in split[1].split('\n'):
			if footer:
				footer_text += line+'\n'
				continue
			if len(line) == 0:
				continue
			if line == '}}':
				footer = True
				continue
			if line[0] == '|':
				output1[param] = buffer # This adds a {'':''} in the first iteration, which is removed later.
				buffer = ''
				m = search('\\|(.*?)=', line)
				param = m.group(1).strip()
				if param in param_subs:
					param = param_subs[param]
				line = line.split('=', 1)[1]
			else:
				buffer += '\n'
			line = line.strip(' ')
			_line = line
			line = sub('<br( )*/?>', '<br>', line)
			line = sub(r"'s]]", "]]'s", line)
			line = sub(r']] - \[\[', ']] {{·}} [[', line)
			if line != _line:
				print _line
				print line
			buffer += line
		output1[param] = buffer
		del output1['']
		for type in types.keys():
			if ('Category:'+type).decode('utf-8') in article.categories:
				output1['type'] = types[type]
				break
		for insertion in insertions.keys():
			if ('Category:'+insertion).decode('utf-8') in article.categories:
				output2 = sub('equipped by', 'equipped '+insertions[insertion]+' by', output2)
				break
 
		trivia = None
		if 'trivia' in output1.keys():
			trivia = output1['trivia']
			del output1['trivia']
 
		for key in set(output1.keys()):
			if key not in param_order:
				print key, 'not in param_order'
 
		keys = sorted(output1.keys(), key=lambda s: safeindex(param_order, s))
		for key in keys:
			if '\n' not in output1[key]:
				output2 += '| {param} = '.format(param=key)
			else:
				output2 += '| {param} ='.format(param=key)
				if key == 'notes' and output1[key][:1] != '\n': # Ensure that multiline notes follow the correct style
					output2 += '\n'
			output2 += output1[key] + '\n'
		output2 += '}}\n'
		if trivia:
			output2 += '\n== Trivia =='
			if len(trivia.split('\n')) > 1:
				output2 += trivia
			else:
				output2 += '\n*' + trivia
		output2 += footer_text
		output3 += output2
 
	if '<tabber>' in output3:
		output3 += '</tabber>'
 
	if article.wikitext.strip() == output3.strip(): # Don't upload files that haven't changed
		continue
 
	# print output3
	# raw_input()
	# continue
 
	params = { # Wikia uses custom APIs, so I can't use the wiki.getEditToken() method.
		'action':'query',
		'prop':'info',
		'intoken':'edit',
		'inprop':'pageid',
		'titles':article.title,
	}
	req = APIRequest(wiki, params)
	response = req.query(False)
	step1 = response['query']['pages']
	token = step1[step1.keys()[0]]['edittoken']
 
	params = { # Wikia uses custom APIs, so I can't use the page.edit() method.
		'action':'edit',
		'token':token,
		'bot':'true',
		'watchlist':'unwatch',
		'title':article.title,
		'text':output3,
		'summary':'Automatic style/content revision by [[User:BOT Jbzdarkid]].',
	}
	req = APIRequest(wiki, params)
	response = req.query(False)
	print response

Tracking down extra classes

classes = '''classes
go
here'''.split('\n')
 
from urllib2 import urlopen, quote
from json import loads
from re import compile
from threading import Thread
 
style = compile('class="(.*?)"')
url = 'http://figverse.wikia.com/api.php?action=query&list=allpages&aplimit=500&format=json&apfilterredir=nonredirects'
page = loads(urlopen(url).read())
pages = []
while True:
	for p in page['query']['allpages']:
		pages.append(p['title'])
	if 'query-continue' not in page:
		break
	page = loads(urlopen(url + '&apfrom=' + page['query-continue']['allpages']['apfrom']).read())
 
pages.sort(reverse=True)
 
matches = {}
def find_in_page():
	page = pages.pop()
	page = page.encode('utf-8')
	data = urlopen(u'http://figverse.wikia.com/wiki/'+quote(page)).read()
	for match in style.finditer(data):
		for c in match.group(1).split(';'):
			for c2 in c.split(' '):
				c2 = c2.strip()
				if c2 in classes:
					if c2 not in matches:
						matches[c2] = set()
					matches[c2].add(page)
 
threads = []
for _ in range(len(pages)):
	thread = Thread(target=find_in_page)
	threads.append(thread)
	thread.start()
for thread in threads:
	thread.join()
 
for key in matches:
	print ':'+key
	for page in sorted(list(matches[key])):
		print '*[['+page+']]'
Community content is available under CC-BY-SA unless otherwise noted.