Advertisement
Fugiman

openbound_dialogue.py

Aug 31st, 2012
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.80 KB | None | 0 0
  1. import xml.etree.ElementTree as ET
  2. import urllib2, re
  3.  
  4. levels = 'http://mspaintadventures.com/storyfiles/hs2/05260/levels/openbound/'
  5. resources = 'http://mspaintadventures.com/storyfiles/hs2/05260/resources/openbound/'
  6. dialogue_files = ['firstRoomDialog.xml','secondRoomDialog.xml','thirdRoomDialog.xml','fourthRoomDialog.xml']
  7. asset_files = ['openbound.xml','firstRoom.xml','secondRoom.xml','thirdRoom.xml','fourthRoom.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 = {}
  20. for f in dialogue_files:
  21.     data = urllib2.urlopen(levels + f).read()
  22.     tree = ET.fromstring(data)
  23.     for talk in tree.iter('action'):
  24.         if talk.get('command') != 'talk':
  25.             continue
  26.         convo = []
  27.         for match in re.finditer('@([^:\s~]*)(?:(?:~([^\s]*))|(?::([^\s]*))? (?:([^@:]*):)?([^@]*))', talk.find('args').text):
  28.             sprite, image, hashtags, actor, dialogue = match.groups()
  29.             image = "%sdialogs/%s.png" % (resources, image) if image else ''
  30.             hashtags = hashtags.replace('-',' ').replace('#',' #').strip() if hashtags else ''
  31.             actor = actor.lower() if actor else ''
  32.             dialogue = dialogue.strip() if dialogue else ''
  33.             # Check for andrew
  34.             if sprite == "!":
  35.                 sprite = ""
  36.             elif sprite:
  37.                 styles[sprite] = True
  38.             if not sprite and not actor:
  39.                 actor = "andrew"
  40.             convo.append({'sprite': sprite, 'image': image, 'hashtags': hashtags, 'actor': actor, 'dialogue': "%s: %s" % (actor.upper(), dialogue)})
  41.         conversations.append(convo)
  42.  
  43. css = []
  44. template = "\t\t.%s { background-image: url('%s'); }"
  45. for key in styles.iterkeys():
  46.     if key in assets:
  47.         css.append(template % (key, assets[key]))
  48. css = "\n".join(css)
  49.  
  50. text = ""
  51. html = """<!doctype html>
  52. <html><head>
  53.    <title>Openbound Dialogue</title>
  54.    <style>
  55.        @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'); }
  56.        body { font-size: 14px; font-weight: bold; font-family: Courier New, Courier, Sburb; }
  57.        .conversation { clear: both; border-bottom: 3px solid black; }
  58.        .dialogue { float: left; margin: 10px; }
  59.        .sprite { height: 325px; width: 230px; float: left; background-size: 100%% auto; background-repeat: no-repeat; }
  60.        .words { float: left; height: 325px; width: 537px; }
  61.        .text { height: 230px; width: 497px; padding: 20px; background: url('http://mspaintadventures.com/storyfiles/hs2/05260/resources/openbound/interface/dialogBoxBig.png'); }
  62.        .text span { overflow: auto; }
  63.        .text img { margin: -25px; }
  64.        .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'); }
  65.        .permalink { position: absolute; bottom: 10px; right: 10px; font-size: 10px; font-weight: normal; }
  66.        .meenah { color: #77003c; } .aranea { color: #005682; } .porrim { color: #008141; } .kankri { color: #ff0000; font-size: 12px; }
  67.        .latula { color: #008282; } .aradia { color: #a10000; } .nepeta { color: #416600; } .vriska { color: #005682; }
  68.        .tavros { color: #a15000; } .eridan { color: #6a006a; } .feferi { color: #77003c; } .karkat { color: #626262; }
  69.        .equius { color: #000056; } .kanaya { color: #008141; } .terezi { color: #008282; } .sollux { color: #a1a100; }
  70.        .gamzee { color: #2b0057; } .dave { color: #e00707; } .rose { color: #b536da; }
  71. %s
  72.    </style>
  73. </head><body>
  74. """ % css
  75. id = 1
  76. for convo in conversations:
  77.     html += '\t<div class="conversation">\n'
  78.     for c in convo:
  79.         template = '\t\t<div id="dialogue-%d" class="dialogue %s"><div class="sprite %s"></div><div class="words"><div class="text">%s</div><div class="hashtags">%s<a class="permalink" href="#dialogue-%d">Permalink</a></div></div></div>\n'
  80.         args = (id, c['actor'], c['sprite'], '<img src="%s" />' % c['image'] if c['image'] else '<span>%s</span>' % c['dialogue'], c['hashtags'], id)
  81.         html += template % args
  82.         text += "%s %s\n" % (c['image'], c['hashtags']) if c['image'] else "%s %s\n" % (c['dialogue'], c['hashtags'])
  83.         id += 1
  84.     html += '\t</div>\n'
  85.     text += "\n"
  86. html += '</body></html>'
  87.  
  88. with open("openbound_dialogue.html","w") as f:
  89.     f.write(html)
  90.  
  91. with open("openbound_dialogue.txt","w") as f:
  92.     f.write(text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement