Last active
August 27, 2016 09:12
-
-
Save altbdoor/43c0f901417336af5221d0768cbbdc52 to your computer and use it in GitHub Desktop.
Python3 script to roughly convert the transcripts into method calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# http://odelvidyascape.blogspot.com/2016/07/dialogue-transcriptions_3.html | |
# http://vidyascape.no-ip.org/kanboard/?controller=board&action=readonly&token=12bc8837b3e20f675f747fda8fabb18052bb904db415bf88e59cd8416b28 | |
# Requirements: | |
# - Python 3 | |
# - BeautifulSoup4 | |
# Usage: | |
# - vscape_dialog.py "{RS3_wiki_url}" | |
# - E.g. vscape_dialog.py "http://runescape.wikia.com/wiki/Transcript%3AThe_Feud" | |
# Output: | |
# - A chats.txt file will be created | |
from urllib.request import urlopen | |
from bs4 import BeautifulSoup, SoupStrainer | |
import re | |
import sys | |
page_url = None | |
try: | |
page_url = sys.argv[1] | |
except: | |
print('Please pass a page URL') | |
sys.exit(1) | |
# ======================================== | |
def get_character_name_from_node(node): | |
character = node.b | |
if character: | |
return character.get_text().strip() | |
else: | |
return '' | |
def get_dialog_text_from_node(node): | |
dialog_text = node.find_all(text=True, recursive=False) | |
dialog_text = ''.join(dialog_text) | |
dialog_text = dialog_text.strip() | |
dialog_text = dialog_text.replace('"', '\\"') | |
return dialog_text | |
# ======================================== | |
with urlopen(page_url) as response: | |
strainer = SoupStrainer(id='mw-content-text') | |
soup = BeautifulSoup(response.read(), 'html.parser', parse_only=strainer) | |
main_div = soup.find('div', id='mw-content-text') | |
chats = [] | |
choices = [] | |
re_slug_npc_name = re.compile('[^a-z]') | |
chat_nodes = main_div.find_all('li', recursive=True) | |
for node in chat_nodes: | |
name = get_character_name_from_node(node) | |
dialog_text = get_dialog_text_from_node(node) | |
dialog_prefix = '' | |
if dialog_text != '': | |
if name == '': | |
dialog_prefix = 'STATEMENT' | |
elif name == 'Player:': | |
dialog_prefix = 'PLAYER' | |
else: | |
slug_npc_name = re_slug_npc_name.sub('', name.lower()) | |
dialog_prefix = 'NPC__[' + slug_npc_name + ']' | |
dialog_text = dialog_prefix + '__' + dialog_text | |
chats.append(dialog_text) | |
choice_nodes = main_div.find_all('ul', recursive=True) | |
for node in choice_nodes: | |
child_nodes_list = node.find_all('li', recursive=False) | |
if len(child_nodes_list) > 1: | |
child_names = [get_character_name_from_node(i) for i in child_nodes_list] | |
child_names = set(child_names) | |
if len(child_names) == 1 and child_names.pop() == 'Player:': | |
choice_text_list = [] | |
for child_node in child_nodes_list: | |
dialog_text = get_dialog_text_from_node(child_node) | |
chats.remove('PLAYER__' + dialog_text) | |
choice_text_list.append(dialog_text) | |
if len(choice_text_list) > 0: | |
choices.append(choice_text_list) | |
with open('chats.txt', 'w') as f: | |
re_match_npc_name = re.compile('^\[([a-z]+)\]__') | |
prefixes = ( | |
{ | |
'name': 'PLAYER__', | |
'method': 'sendPlayerChat', | |
}, | |
{ | |
'name': 'NPC__', | |
'method': 'sendNpcChat', | |
}, | |
{ | |
'name': 'STATEMENT__', | |
'method': 'sendStatement', | |
}, | |
) | |
for chat in chats: | |
for prefix in prefixes: | |
if chat.startswith(prefix['name']): | |
chat = chat.replace(prefix['name'], '') | |
postfix = '' | |
if prefix['name'] == 'NPC__': | |
match = re_match_npc_name.search(chat) | |
chat = re_match_npc_name.sub('', chat) | |
postfix = ' // ' + match.group(1) | |
chat = 'd.' + prefix['method'] + '("' + chat + '");' + postfix | |
f.write(chat + '\n') | |
break | |
f.write('\n') | |
for choice in choices: | |
f.write('d.sendOption("' + '", "'.join(choice) + '");\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment