Advertisement
Fugiman

openbound2_dialogue.py

Sep 27th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.65 KB | None | 0 0
  1. import xml.etree.ElementTree as ET
  2. import urllib2, re
  3.  
  4. levels = 'http://mspaintadventures.com/storyfiles/hs2/05305/levels/openbound_p2/'
  5. resources = 'http://mspaintadventures.com/storyfiles/hs2/05305/resources/openbound_p2/'
  6. dialogue_files = ['cronusDialog.xml','expoDialog.xml','karkatDialog.xml','kurlozDialog.xml','meenahDialog.xml','meulinDialog.xml','mitunaDialog.xml','tereziDialog.xml']
  7. asset_files = ['etc/ui.xml','rooms/fourthRoom.xml','chars/andrew.xml','chars/aranea.xml','chars/bicyclops.xml','chars/cronus.xml','chars/kankri.xml','chars/karkat.xml','chars/kurloz.xml','chars/meenah.xml','chars/meulin.xml','chars/mituna.xml','chars/seahorsedad.xml','chars/terezi.xml']
  8.  
  9. assets = {}
  10. for f in asset_files:
  11.     data = urllib2.urlopen(levels + f).read()
  12.     tree = ET.fromstring(data)
  13.     for asset in tree.iter('asset'):
  14.         if asset.get('type') != 'graphic':
  15.             continue
  16.         assets[asset.get('name')] = resources + asset.text
  17.  
  18. conversations = []
  19. styles = {"dialogBox":True}
  20. for f in dialogue_files:
  21.     data = urllib2.urlopen(levels + 'dialogs/' + f).read()
  22.     tree = ET.fromstring(data)
  23.     for talk in tree.iter('action'):
  24.         if talk.get('command') != 'talk':
  25.             continue
  26.         args = talk.find('args')
  27.         if args is None:
  28.             print "Failure :(",f,repr(list(talk))
  29.             continue
  30.         convo = []
  31.         for match in re.finditer('@([^:\s~]*)(?:(?:~([^\s]*))|(?::([^\s]*))? (?:([^@:]*):)?([^@]*))', args.text):
  32.             sprite, image, hashtags, actor, dialogue = match.groups()
  33.             sprite, chaff, background = sprite.partition("%")
  34.             if image:
  35.                 image, chaff, override = image.partition(":")
  36.                 if override and not hashtags:
  37.                     hashtags = override
  38.             image = "%sinterface/backgrounds/%s.png" % (resources, image) if image else ''
  39.             hashtags = hashtags.replace('-',' ').replace('#',' #').strip() if hashtags else ''
  40.             actor = actor.lower() if actor else ''
  41.             dialogue = dialogue.strip() if dialogue else ''
  42.             # Check for andrew
  43.             if sprite == "!":
  44.                 sprite = ""
  45.             elif sprite:
  46.                 styles[sprite] = True
  47.             if not sprite and not actor:
  48.                 actor = "andrew"
  49.             if background:
  50.                 styles[background] = True
  51.             convo.append({'sprite': sprite, 'image': image, 'hashtags': hashtags, 'actor': actor, 'dialogue': "%s: %s" % (actor.upper(), dialogue), 'background': background if background else "dialogBox"})
  52.         conversations.append(convo)
  53.  
  54. css = []
  55. template = "\t\t.%s { background-image: url('%s'); }"
  56. for key in styles.iterkeys():
  57.     if key in assets:
  58.         css.append(template % (key, assets[key]))
  59. css = "\n".join(css)
  60.  
  61. text = ""
  62. html = """<!doctype html>
  63. <html><head>
  64.    <meta charset="UTF-8">
  65.    <title>Openbound Dialogue</title>
  66.    <style>
  67.        @font-face { font-family: Sburb; src: url('http://mspaintadventures.com/storyfiles/hs2/05260/resources/openbound/fonts/cour.ttf'), url('http://mspaintadventures.com/storyfiles/hs2/05260/resources/openbound/fonts/cour.woff'); }
  68.        body { font-size: 14px; font-weight: bold; font-family: Courier New, Courier, Sburb; }
  69.        .conversation { clear: both; border-bottom: 3px solid black; }
  70.        .dialogue { float: left; margin: 10px; }
  71.        .sprite { height: 325px; width: 230px; float: left; background-size: 100%% auto; background-repeat: no-repeat; }
  72.        .words { float: left; height: 325px; width: 537px; }
  73.        .text { height: 230px; width: 497px; padding: 20px; }
  74.        .text span { overflow: auto; }
  75.        .text img { margin: -25px; }
  76.        .hashtags { position: relative; color: black; height: 29px; width: 497px; padding: 13px 20px; background: url('http://mspaintadventures.com/storyfiles/hs2/05260/resources/openbound/interface/hashtagbar.png'); }
  77.        .permalink { position: absolute; bottom: 10px; right: 10px; font-size: 10px; font-weight: normal; }
  78.        .meenah { color: #77003c; } .aranea { color: #005682; } .porrim { color: #008141; } .kankri { color: #ff0000; font-size: 12px; }
  79.        .latula { color: #008282; } .aradia { color: #a10000; } .nepeta { color: #416600; } .vriska { color: #005682; }
  80.        .tavros { color: #a15000; } .eridan { color: #6a006a; } .feferi { color: #77003c; } .karkat { color: #626262; }
  81.        .equius { color: #000056; } .kanaya { color: #008141; } .terezi { color: #008282; } .sollux { color: #a1a100; }
  82.        .gamzee { color: #2b0057; } .dave { color: #e00707; } .rose { color: #b536da; }
  83.        .cronus { color: #6a006a; } .mituna { color: #a1a100; } .kurloz { color: #6c00da; } .meulin { color: #416600; }
  84. %s
  85.    </style>
  86. </head><body>
  87. """ % css
  88. id = 1
  89. for convo in conversations:
  90.     html += '\t<div class="conversation">\n'
  91.     for c in convo:
  92.         template = '\t\t<div id="dialogue-%d" class="dialogue %s"><div class="sprite %s"></div><div class="words"><div class="text %s">%s</div><div class="hashtags">%s<a class="permalink" href="#dialogue-%d">Permalink</a></div></div></div>\n'
  93.         args = (id, c['actor'], c['sprite'], c["background"], '<img src="%s" />' % c['image'] if c['image'] else '<span>%s</span>' % c['dialogue'], c['hashtags'], id)
  94.         html += template % args
  95.         text += "%s %s\n" % (c['image'], c['hashtags']) if c['image'] else "%s %s\n" % (c['dialogue'], c['hashtags'])
  96.         id += 1
  97.     html += '\t</div>\n'
  98.     text += "\n"
  99. html += '</body></html>'
  100.  
  101. with open("openbound2_dialogue.html","w") as f:
  102.     f.write(html.encode("utf-8"))
  103.  
  104. with open("openbound2_dialogue.txt","w") as f:
  105.     f.write(text.encode("utf-8"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement