将歌词处理成电子书

Posted by 橙叶 on Sun, Feb 10, 2019

后端的最后一部分。

  1. # coding=utf-8  
  2. import json  
  3. import re  
  4.   
  5.   
  6. class Lyric2Book:  
  7.     support_format = ['txt', 'html']  
  8.     support_ts = ['parallel', 'chunk']  
  9.     html_template = """ 
  10.     <section> 
  11.         <h2 class='header'>%(header)s</h2> 
  12.             <div class='info'> 
  13.                 <div class='album'>%(album)s</div> 
  14.                 <div class='artists'>%(artists)s</div> 
  15.             </div> 
  16.         <div class='content'> 
  17.             %(lyrics)s 
  18.         </div> 
  19.     </section> 
  20.         """  
  21.     html_frame = """<!DOCTYPE html> 
  22.     <html lang="zh-cn"> 
  23.     <head>  
  24.     <meta charset="utf-8"/> 
  25.     <meta name="viewport" content="width=device-width, initial-scale=1" /> 
  26.     <meta name="referrer" content="never" /> 
  27.     <title>%s</title> 
  28.     </head> 
  29.     <body> 
  30.     %s 
  31.     </body> 
  32.     </html> 
  33.     """  
  34.     lyrics_template = "<div class='content-%(ver)d' >%(content)s</div>"  
  35.     txt_template = "%(header)s\n专辑:%(album)s\n作者:%(artists)s\n\n%(lyrics)s\n\n"  
  36.     txt_frame = "%s\n\n%s"  
  37.   
  38.     def __init__(self, file_format='html', title='Lyrics', typesetting='parallel'):  
  39.         self.title = title  
  40.         if file_format in Lyric2Book.support_format:  
  41.             self.format = file_format  
  42.         else:  
  43.             raise Exception('Unsupported format: %s.' % file_format)  
  44.         self.res = ""  
  45.         self.data = ""  
  46.         if typesetting in Lyric2Book.support_ts:  
  47.             self.ts = typesetting  
  48.         else:  
  49.             raise Exception('Unsupported typesetting: %s' % typesetting)  
  50.   
  51.     def chunk(self, lyrics):  
  52.         predlyric = []  
  53.         # 传入的是多个版本的歌词  
  54.         for item in lyrics:  
  55.             lines = []  
  56.             if item is not None:  
  57.                 # 分行处理  
  58.                 for line in item.split('\n'):  
  59.                     line = re.sub('\(\d+,\d+\)', '', line)  
  60.                     time_tag = ''.join(re.findall('\[.*?\]', line))  
  61.                     line = [time_tag, line.strip(time_tag)]  
  62.                     lines.append(line)  
  63.                 predlyric.append(lines)  
  64.         output_section = ''  
  65.         if self.format == 'html':  
  66.             i = 1  
  67.             for item in predlyric:  
  68.                 output_section += "<div class='content-%d'>\n" % i  
  69.                 i += 1  
  70.                 for line in item:  
  71.                     output_section += "<p><span class='timetag'>%s</span>%s</p> \n" % (re.sub('[\[\]]', '-', line[0]), line[1])  
  72.                 output_section += "</div>"  
  73.         elif self.format == 'txt':  
  74.             for item in predlyric:  
  75.                 output_section += ""  
  76.                 for line in item:  
  77.                     format_tag = '-%s- ' % re.sub('[\[\]]', '-', line[0]) if line[0] else ''  
  78.                     output_section += format_tag + line[1] + '\n'  
  79.         return output_section  
  80.   
  81.     def parallel(self, lyrics):  
  82.         unpredlyric = []  
  83.         for item in lyrics:  
  84.             lines = {}  
  85.             if item is not None:  
  86.                 for line in item.split('\n'):  
  87.                     line = re.sub('\(\d+,\d+\)', '', line)  
  88.                     time_tag = ''.join(re.findall('\[.*?\]', line))  
  89.                     lines[time_tag] = line.strip(time_tag)  
  90.                 unpredlyric.append(lines)  
  91.         predlyric = []  
  92.         for group_tag in list(unpredlyric[0].keys()):  
  93.             predlyric += [[group_tag] + [i.get(group_tag, unpredlyric[0][group_tag]) for i in unpredlyric]]  
  94.         dparallels = predlyric  
  95.         output_section = ''  
  96.         if self.format == 'html':  
  97.             for item in dparallels:  
  98.                 output_section += "<div class='content'>\n"  
  99.                 dtimetag = item[0]  
  100.                 dcontent = item[1::]  
  101.                 item_div = "<div class='timetag'>\n<p>{timetag}</p>\n</div>\n<div class='single-lyric'>\n{content}</div>\n"  
  102.                 output_spar = ''.join("<p class='ver-%d'>%s</p>\n" % (c[0], c[1]) for c in enumerate(dcontent, 1))  
  103.                 output_section += item_div.format(timetag=re.sub('[\[\]]', ' - ', dtimetag), content=output_spar) + '</div>'  
  104.         elif self.format == 'txt':  
  105.             for item in dparallels:  
  106.                 dtimetag = item[0]  
  107.                 dcontent = item[1::]  
  108.                 item_div = "{timetag}\n{content}\n"  
  109.                 output_spar = ''.join("%s\n" % c[1] for c in enumerate(dcontent))  
  110.                 output_section += item_div.format(timetag=re.sub('[\[\]]', ' - ', dtimetag), content=output_spar)  
  111.         return output_section  
  112.   
  113.     def doconv(self, sections):  
  114.         last_album = ''  
  115.         output = ''  
  116.         for item in sections:  
  117.             header = item['name']  
  118.             album = item['album']  
  119.             artists = ','.join(item['artists'])  
  120.             a = item['lyric']  
  121.             if a is not None:  
  122.                 ly_res = {}  
  123.                 lyrics = [a['0'], a['1'], a['2']]  
  124.                 if self.ts == 'parallel':  
  125.                     ly_res = self.parallel(lyrics)  
  126.                 elif self.ts == 'chunk':  
  127.                     ly_res = self.chunk(lyrics)  
  128.                 if self.format == 'html':  
  129.                     if last_album != album:  
  130.                         last_album = album  
  131.                         output += '<h1>%s</h1>' % album  
  132.                     output += Lyric2Book.html_template % {'header': header, 'album': album, 'artists': artists, 'lyrics': ly_res}  
  133.                 elif self.format == 'txt':  
  134.                     if last_album != album:  
  135.                         last_album = album  
  136.                         output += album + '\n\n'  
  137.                     output += Lyric2Book.txt_template % {'header': header, 'album': album, 'artists': artists, 'lyrics': ly_res}  
  138.         self.res = output  
  139.   
  140.     def output(self):  
  141.         filename = self.title + '.' + self.format  
  142.         with open(filename, 'w+', encoding='utf-8') as f:  
  143.             res = self.res  
  144.             if self.format == 'html':  
  145.                 res = Lyric2Book.html_frame % (self.title, res)  
  146.             elif self.format == 'txt':  
  147.                 res = Lyric2Book.txt_frame % (self.title, res)  
  148.             f.write(res)  
  149.   
  150.   
  151. if __name__ == '__main__':  
  152.     with open('bbc.txt', 'r', encoding='utf-8') as file:  
  153.         t = Lyric2Book(file_format='thjxt', title='BBC Documentary', typesetting='parallel')  
  154.         content = json.loads(file.read())  
  155.         t.doconv(content['result'])  
  156.         t.output()  


comments powered by Disqus