import Image, re, os, urllib, subprocess
from subprocess import PIPE, Popen
import wikitools
from uploadFile import *
vdfstring = open('items_game.txt', 'rb').read()
vdfstring = vdfstring.decode('utf-8')
locfile = open('tf_english.txt', 'rb').read()
locfile = locfile.decode('utf-16')
rString = re.compile(r'"([^"]+)"[ \t]*"([^"]*)"')
rDictionary = re.compile(r'"([^"]+)"\s*\{([^"{}]+)\}')
rValues = re.compile(r'~OHAI~(\d+)~')
valueCount = -1
values = []
def registerString(m):
global valueCount
valueCount += 1
values.append((m.group(1), m.group(2)))
return '~OHAI~' + str(valueCount) + '~'
vdfstring = rString.sub(registerString, vdfstring)
d = {}
while True:
m = rDictionary.search(vdfstring)
if m is None:
break
d = {}
for v in rValues.finditer(m.group(2)):
val = values[int(v.group(1))]
d[val[0]] = val[1]
valueCount += 1
values.append((m.group(1), d))
vdfstring = vdfstring[:m.start()] + '~OHAI~' + str(valueCount) + '~' + vdfstring[m.end():]
finalParsed = values[-1]
def get_file_list(folder):
return os.listdir(folder)
def crop_image(inputimage, folder, newimgname):
img = Image.open(folder + os.sep + inputimage)
img = img.crop((0, 64, 512, 64 + 384))
img.save(folder + os.sep + newimgname, 'PNG')
def get_item_name(filename):
imgname = re.sub(r'_large\.png', '', filename)
print 'Processing:', imgname
for item in finalParsed[1]['items']:
if 'item_name' in finalParsed[1]['items'][item]:
try:
itemimgname = finalParsed[1]['items'][item]['image_inventory']
itemimgname = os.path.split(itemimgname)[1]
if itemimgname == imgname:
itemnametoken = finalParsed[1]['items'][item]['item_name']
tfenglishRE = re.compile(r'"%s"[\s\t]*"(.[^"]+)"' % itemnametoken[1:], re.IGNORECASE)
itemname = tfenglishRE.search(locfile).group(1)
print 'Processed as', itemname
return itemname
except:
continue
return None
def upload_item_icons(wikiUsername, wikiPassword, folder, wikiAddress = r'http://wiki.tf2.com/w/', wikiApi = r'http://wiki.tf2.com/w/api.php'):
uploader = wikiUpload.wikiUploader(wikiUsername, wikiPassword, wikiAddress)
wiki = wikitools.wiki.Wiki(wikiApi)
wiki.login(wikiUsername, wikiPassword)
for file in get_file_list(folder):
try:
itemname = get_item_name(file).encode('utf8')
except:
f = open('faileditemiconuploads.txt', 'ab').write(file + '\n')
continue
newfilename = r'Item icon %s.png' % itemname
crop_image(file, folder, newfilename)
process = Popen(['pngcrush', '-rem', 'gAMA', '-rem', 'cHRM', '-rem', 'iCCP', '-rem', 'sRGB', '-brute', folder + os.sep + newfilename, folder + os.sep + newfilename + 'temp'], stdout = subprocess.PIPE).communicate()[0]
os.remove(folder + os.sep + newfilename)
os.rename(folder + os.sep + newfilename + 'temp', folder + os.sep + newfilename)
success = False
n = 0
while n < 5 and not success:
try:
uploader.upload(folder + os.sep + newfilename, 'File:' + newfilename, 'Uploaded new TF2B icon', '', overwrite=True)
success = True
except:
n += 1
if not success:
print 'Could not upload', newfilename
upload_item_icons('pootisusername', 'pootispassword', r'pootisfolderpath')