User:Moussekateer/voicelines.py

import os, re
 * 1) -*- coding: utf-8 -*-

wavfilesLocation = """""" #Set location of wav files here.

game = 'Portal 2' #Set game here. Either 'Portal' or 'Portal 2'. language = 'fi' #Set language code here. See scipts dictionary below for list of codes. character = 'Wheatley' #Set Character here. See characters dictionary below for list of names. Not used for Portal.

portalscripts = { 'da': 'closecaption_danish.txt', 'nl': 'closecaption_dutch.txt', 'en': 'closecaption_english.txt', 'fi': 'closecaption_finnish.txt', 'fr': 'closecaption_french.txt', 'de': 'closecaption_german.txt', 'it': 'closecaption_italian.txt', 'ja': 'closecaption_japanese.txt', 'ko': 'closecaption_korean.txt', 'ka': 'closecaption_koreana.txt', 'no': 'closecaption_norwegian.txt', 'pl': 'closecaption_polish.txt', 'po': 'closecaption_portuguese.txt', 'ru': 'closecaption_russian.txt', 'zh-hans': 'closecaption_schinese.txt', 'es': 'closecaption_spanish.txt', 'sw': 'closecaption_swedish.txt', 'zh-hant': 'closecaption_tchinese.txt', }

portal2scripts = { 'cz': 'subtitles_czech.txt', 'da': 'subtitles_danish.txt', 'nl': 'subtitles_dutch.txt', 'en': 'subtitles_english.txt', 'fi': 'subtitles_finnish.txt', 'fr': 'subtitles_french.txt', 'de': 'subtitles_german.txt', 'hu': 'subtitles_hungarian.txt', 'it': 'subtitles_italian.txt', 'ja': 'subtitles_japanese.txt', 'ko': 'subtitles_korean.txt', 'ka': 'subtitles_koreana.txt', 'no': 'subtitles_norwegian.txt', 'pl': 'subtitles_polish.txt', 'po': 'subtitles_portuguese.txt', 'ro': 'subtitles_romanian.txt', 'ru': 'subtitles_russian.txt', 'zh-hans': 'subtitles_schinese.txt', 'es': 'subtitles_spanish.txt', 'sw': 'subtitles_swedish.txt', 'zh-hant': 'subtitles_tchinese.txt', 'th': 'subtitles_thai.txt', 'tu': 'subtitles_turkish.txt' }

characters = { 'Cave Johnson': 'cavejohnson', 'Caroline': 'glados.caroline', 'GLaDOS': 'glados', 'Wheatley': 'sphere03', 'Adventure core': 'core03', 'Fact core': 'core02', 'Space core': 'core01', 'Turret': 'turret', 'Floor Turret': 'npc_floorturret', 'Defective Turret': 'turret', 'Announcer': 'announcer' }

def utf8encode(file): s1 = open(file, "rb").read s2 = unicode(s1, "utf-16") s3 = s2.encode("utf-8") open (file, "wb").write(s3)

if game == 'Portal': utf8encode(portalscripts[language]) script = open(portalscripts[language], 'rb').read elif game == 'Portal 2': # utf8encode(portal2scripts[language]) script = open(portal2scripts[language], 'rb').read

files = os.listdir(wavfilesLocation) files.sort

failedmatches = []

if game == 'Portal 2': for f in files: if character == 'Defective Turret' or character == 'Turret' or character == 'Floor Turret': pattern = "\"" + characters[character] + "." + f[:-4] + "\"\s\"]+)> (.[^:]+): (.[^\"]+)\""		else:			pattern = "\"" + characters[character] + "." + f[:-4] + "\"\s\"]+)>(.[^:]+): (.[^\"]+)\"" regexp = re.compile(pattern, re.IGNORECASE) result = regexp.search(script) try: if language == 'en': print ('*[[Media:' + character + '_' + f + '|\"' + result.group(3) + '"]]') else: print ('*[[Media:' + character + '_' + f[:-4] + '_' + language + f[-4:] + '|\"' + result.group(3) + '"]]') except AttributeError: failedmatches.append(f) if language == 'en': print ('*[[Media:' + character + '_' + f + '|\"\"]]') else: print ('*[[Media:' + character + '_' + f[:-4] + '_' + language + f[-4:] + '|\"\"]]') elif game == 'Portal': for f in files: pattern = "\"portal." + f[:-4] + "\"[\t]+\"]+)>(.[^\"]+)\""		regexp = re.compile(pattern, re.MULTILINE)		result = regexp.search(script)		#BRACE YOURSELF		if result is None:			pattern = "\"portal." + f[:-4] + "\".[^\"]+\"]+)>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None:			pattern = "\"" + f[:-4] + "\"[\t]+\"]+)>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None: pattern = "\"" + f[:-4] + "\".[^\"]+\"]+)>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None: pattern = "\"portal.glados." + f[:-4] + "\"[\t]+\"]+)>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None:			pattern = "\"portal.glados." + f[:-4] + "\".[^\"]+\"]+)>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None:			g = f[16:-4]			pattern = "\"portal.glados.core_" + g.replace('-', '_') + "\"[\t]+\"]+)>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None: g = f[16:-4] pattern = "\"portal.glados.core_" + g.replace('-', '_') + "\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None: g = f[17:-4] pattern = "\"portal.glados_core." + g.replace('-', '_') + "\"[\t]+\"<clr:(.[^>]+)><I>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None:			g = f[17:-4]			pattern = "\"portal.glados_core." + g.replace('-', '_') + "\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None:			g = f[17:-4]			pattern = "\"portal.glados_core." + g.replace('-', '_').replace('0', '') + "\"[\t]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None: g = f[17:-4] pattern = "\"portal.glados_core." + g.replace('-', '_').replace('0', '') + "\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) #Some funny file -> caption mappings if result is None and f[:25] == 'escape_02_sphere_cakemix-': pattern = "\"portal.glados_core." + f[:-4].replace('escape_02_sphere_cakemix-', 'crazy_') + "\"[\t]+\"<clr:(.[^>]+)><I>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None and f[:25] == 'escape_02_sphere_cakemix-':			pattern = "\"portal.glados_core." + f[:-4].replace('escape_02_sphere_cakemix-', 'crazy_') + "\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None and f[:21] == 'escape_02_spherenag1-':			stripzeropattern = "escape_02_spherenag1-0(\d).wav"			regexpzero = re.compile(stripzeropattern, re.IGNORECASE)			g = f			if regexpzero.search(f):				g = "escape_02_spherenag1-" + regexpzero.search(f).group(1) + ".wav"			pattern = "\"portal.glados." + g[:-4].replace('escape_02_spherenag1-', 'core_nag_') + "\"[\t]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None and f[:21] == 'escape_02_spherenag1-': stripzeropattern = "escape_02_spherenag1-0(\d).wav" regexpzero = re.compile(stripzeropattern, re.IGNORECASE) g = f			if regexpzero.search(f): g = "escape_02_spherenag1-" + regexpzero.search(f).group(1) + ".wav" pattern = "\"portal.glados." + g[:-4].replace('escape_02_spherenag1-', 'core_nag_') + "\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None and f[:22] == 'escape_02_spheredrop1-': stripzeropattern = "escape_02_spheredrop1-0(\d).wav" regexpzero = re.compile(stripzeropattern, re.IGNORECASE) g = "escape_02_spheredrop1-" + regexpzero.search(f).group(1) + ".wav" pattern = "\"portal.glados." + g[:-4].replace('escape_02_spheredrop1-', 'core_drop_') + "\"[\t]+\"<clr:(.[^>]+)><I>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None and f[:22] == 'escape_02_spheredrop1-':			stripzeropattern = "escape_02_spheredrop1-0(\d).wav"			regexpzero = re.compile(stripzeropattern, re.IGNORECASE)			g = "escape_02_spheredrop1-" + regexpzero.search(f).group(1) + ".wav"			pattern = "\"portal.glados." + g[:-4].replace('escape_02_spheredrop1-', 'core_drop_') + "\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None and f == 'escape_02_entry-00.wav':			pattern = "\"portal.glados.found_me\"[\t]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None and f == 'escape_02_entry-00.wav': pattern = "\"portal.glados.found_me\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None and f == 'escape_02_entry-01.wav': pattern = "\"portal.glados.broke_my_heart\"[\t]+\"<clr:(.[^>]+)><I>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None and f == 'escape_02_entry-01.wav':			pattern = "\"portal.glados.broke_my_heart\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None and f == 'escape_02_entry-02.wav':			pattern = "\"portal.glados.call_it_a_day\"[\t]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None and f == 'escape_02_entry-02.wav': pattern = "\"portal.glados.call_it_a_day\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None and f == 'escape_02_entry-03.wav': pattern = "\"portal.glados.not_gonna_happen\"[\t]+\"<clr:(.[^>]+)><I>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None and f == 'escape_02_entry-03.wav':			pattern = "\"portal.glados.not_gonna_happen\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\""			regexp = re.compile(pattern, re.MULTILINE)			result = regexp.search(script)		if result is None and f == 'escape_02_entry-04.wav':			pattern = "\"portal.glados.supprise_3_2_1\"[\t]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) if result is None and f == 'escape_02_entry-04.wav': pattern = "\"portal.glados.supprise_3_2_1\".[^\"]+\"<clr:(.[^>]+)><I>(.[^\"]+)\"" regexp = re.compile(pattern, re.MULTILINE) result = regexp.search(script) try: if language == 'en': print ('*[[Media:GLaDOS ' + f + '|\"' + result.group(2) + '"]]') else: print ('*[[Media:GLaDOS ' + f[:-4] + '_' + language + f[-4:] + '|\"' + result.group(2) + '"]]') except AttributeError: failedmatches.append(f) if language == 'en': print ('*[[Media:GLaDOS ' + f + '|\"\"]]') else: print ('*[[Media:GLaDOS ' + f[:-4] + '_' + language + f[-4:] + '|\"\"]]') print "Number of failed matches: " + str(len(failedmatches))