from ox.text import findRe
import json
import codecs



def JSONtoHTML():
    data = json.loads(open("radia.json").read())
    html = ''
    counter = 1
    for d in data:
        html += '<div class="container" data-name="%s" data-padma="%s">\n' % (d['name'], d['hid'],)
        html += '  <div class="title"><span class="counter">%d&gt;</span> %s</div>\n' % (counter, d['title'],)
        html += '  <div class="time">%s</div>\n' % (d['dt-time'],)
        html += "</div>\n\n"
        counter += 1
    indexFile = codecs.open("index.html", mode="w", encoding="utf-8")
    header = open("header.html").read()
    footer = open("footer.html").read()
    indexFile.write(header)
    indexFile.write(html)
    indexFile.write(footer)
    indexFile.close()  

def do():
    f = codecs.open("ChronoArr_Srt.txt", mode="r", encoding="utf-8")
    data = json.loads(open("radia.json").read())
    txt = f.read()
    tapes = txt.split("***")
    indexHtml = u''
    counter = 0
    for t in tapes:
        if counter >= len(data):
            break
        meta = data[counter]
        padmaId = meta['hid']
        audioFile = meta['name'] + u'.wav'
        fullTxt = t.strip()
        titleReg = r'Title: (.*?)\n'
        timeReg = r'Time: (.*?)\n'
        time = findRe(fullTxt, timeReg)
        outfile = "tapes/%d.html" % (counter,)
        title = findRe(fullTxt, titleReg)
        print title
        indexHtml += "<a target='_blank' href='%s'>%s</a><br />Time: %s<br/>\n" % (outfile, title, time,)
        indexHtml += "<a target='_blank' href='http://pad.ma/%s'>View on Padma</a><br />\n" % (padmaId,)
#        audioHtml = "<audio controls='controls' src='wav/%s' preload='none'></audio><br /><br />\n\n" % (audioFile,)
        print audioHtml
        indexHtml += audioHtml
        out = codecs.open(outfile, mode="w", encoding="utf-8")
        out.write("<audio controls='controls' src='../wav/%s'></audio><br /><br /><pre>%s</pre>" % (audioFile, fullTxt,))
        out.close()
        counter += 1
    indexFile = codecs.open("index.html", mode="w", encoding="utf-8")
    header = open("header.html").read()
    footer = open("footer.html").read()
    indexFile.write(header)
    indexFile.write(indexHtml)
    indexFile.write(footer)
    indexFile.close()
    f.close()
    
     
