This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maker.py
94 lines (80 loc) · 2.92 KB
/
maker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
# -*- coding: utf-8 -*-
# HTML5KaraokeMaker
#
# Copyright 2015 Guénaël Muller <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
import logging
import setting
import configparser
import re
import csv
from decimal import *
logging.basicConfig()
class Maker(object):
"""Make karaoke"""
def __init__(self,webpath,datapath,logLevel=setting.LOGLEVEL):
#logger
self.logger = logging.getLogger("Maker")
self.logLevel = logLevel
self.logger.setLevel(logLevel)
#file
self.webpath = webpath
self.datapath = datapath
self._openModel()
self.config = configparser.ConfigParser()
self.config.read(self.datapath+'/global.ini')
def _openModel(self):
with open(self.datapath+"/model.html", "r") as doc:
self.doc=doc.read()
def _replace(self,word):
tmp=self.config['DEFAULT'][word]
if tmp:
tmp=str(tmp)
self.doc=self.doc.replace('['+word+']',tmp)
def make(self):
#usable word
listWord=['title','subtitle','mp3file','oggfile','wavfile',
'footer']
listWord.extend(['initaudiomsg','error','audioerrormsg',
'formaterrormsg','speederrormsg','jserrormsg','speedratemsg'])
for word in listWord:
self._replace(word)
getcontext().prec = 7
tmpKaraokeStr="\n"
for elem in self.config.sections():
tmpfile=self.config[elem]['file']
try:
heading=self.config[elem]['heading']
except:
heading=""
if heading:
tmpKaraokeStr+="<h3 class=\"section-heading\">"
tmpKaraokeStr+=str(heading)+"</h3>\n"
tmpKaraokeStr+="<p>\n"
with open(self.datapath+"/"+tmpfile) as csvfile:
fieldnames = ['begin', 'end','word']
reader = csv.DictReader(csvfile,delimiter='\t',
fieldnames=fieldnames)
for row in reader:
beginStr=row['begin'].replace(",",".")
begin=Decimal(beginStr)
endStr=row['end'].replace(",",".")
end=Decimal(endStr)
dur=end-begin
tmpKaraokeStr+="\t<span data-dur="+str(dur)+" "
tmpKaraokeStr+="data-begin="+str(begin)+">"
tmpKaraokeStr+=row['word']+"</span>"
tmpKaraokeStr+="\n"
tmpKaraokeStr+="</p>\n"
self.doc=self.doc.replace('[sections]',tmpKaraokeStr)
def __str__(self):
return self.doc
if __name__ == '__main__':
m=Maker("/web","./data")
m.make()
print (m)