FANDOM


import httplib 
import re
import os
import os.path

conn = httplib.HTTPConnection('pvx.wikia.com')

def main():
	print
	conn.request('GET', '/index.php?title=Main_Page')
	r1 = conn.getresponse()
	conn.close()
	if r1.status == 200:
		print "Holy shit! Wikia is actually working. Now let's start getting that build data."
		start_package()
	else:
		print "Wikia's servers are busy blowing some fat guy for crack. Try again later."

def start_package():
	global conn

	CATEGORIES = ['AB','CM','GvG','HA','RA','PvP_team','general','farming','running','hero','SC','PvE_team']
	RATINGS = ['Great','Good']
	
	if not os.path.isdir('./PvX Build Packs'):
		os.mkdir('./PvX Build Packs')
	
	#Let's start with AB
	for cat in CATEGORIES:
		 if len(cat) > 3 and cat.find('team') == -1:
		 	cat_dir = './PvX Build Packs/' + cat.title()
		 else:
		 	cat_dir = './PvX Build Packs/' + cat.replace('_',' ')
		 if not os.path.isdir(cat_dir):
		 	os.mkdir(cat_dir)
		 for rat in RATINGS:
		 	print "Attempting " + rat + " " + cat + "..."
		 	conn.request('GET', '/index.php?title=Category:' + rat + '_working_' + cat + '_builds')
		 	response = conn.getresponse()
		 	page = response.read()
		 	conn.close()
		 	if response.status == 200:
		 		rat_dir = cat_dir + '/' + rat
		 		if not os.path.isdir(rat_dir):
		 			os.mkdir(rat_dir)
		 		pagelist = category_page_list(page)
		 		get_builds_and_write(pagelist, rat_dir)
		 		print rat + " " + cat + " complete."
		 	else:
		 		print rat + " " + cat + " failed."
	print "Script complete."
                
def get_builds_and_write(pagelist, rat_dir):
	for i in pagelist:
		cur_dir = rat_dir
		print "Attempting " + i + "..."
		conn.request('GET', '/index.php?title=' + i.replace(' ','_'))
		response = conn.getresponse()
		page = response.read()
		conn.close()
		if response.status == 200:
			codes = find_template_code(page)
			#Check to see if the build is a team build
			if i.find('Team') >= 1 and len(codes) > 1:
				num = 0
				cur_dir += '/' + i.replace('Build:','').replace('/','_')
				if not os.path.isdir(cur_dir):
					os.mkdir(cur_dir)
				for j in codes:
					num += 1
					outfile = open(cur_dir + '/' + i.replace('Build:','').replace('/','_') + ' - ' + str(num) + '.txt','wb')
					outfile.write(j)
			else:
				outfile = open(cur_dir + '/' + i.replace('Build:','').replace('/','_') + '.txt','wb')
				outfile.write(codes[0])
			print i + " complete."
		else:
			print i + " failed."	

def category_page_list(page):
     pagelist = re.findall(">Build:.*?<", page)
     current = ''
     newlist = []
     for i in pagelist:
         current = i.replace('​','').replace('>','').replace('<','')
         newlist += [current]
     return newlist

def find_template_code(page):
	codelist = re.findall('<input id="gws_template_input" type="text" value=".*?"', page)
	current = ''
	newlist = []
	for i in codelist:
	    current = i.replace('<input id="gws_template_input" type="text" value="','').replace('"','')
	    newlist += [current]
	return newlist

if __name__ == "__main__":
	main()
Community content is available under CC-BY-NC-SA 2.5 unless otherwise noted.